Reputation: 7
import java.util.*;
public class Student {
private String [] first;
private String [] last;
private String [] HKID;
private String[] SID;
private int []Exam;
private int num;
Scanner kb= new Scanner(System.in);
public Student (String f, String l, String h, String s, int e, int n){
System.out.println("Please enter number of students:");
n = kb.nextInt();
for(int i=0;i >n; i++){
System.out.println("First name:");
f = kb.next();
first = new String[f];
System.out.println("Last name:");
l=kb.next();
last= new String[l];
System.out.println("HKID:");
h=kb.next();
HKID=new String[h];
System.out.println("SID:");
s=kb.next();
SID=new String [s];
System.out.println("Final exam score:");
e=kb.nextInt();
Exam=new int [e];
}
public String[] getFirst(){return first;}
public String [] getLast(){return last;}
public String [] getHKID(){return HKID;}
public String [] getSID(){return SID;}
public int [] getExam(){return Exam;}
public void setFirst(String [] f){f=first;}
public void setLast(String [] l){l=last;}
public void setHKID(String [] h){h=HKID;}
public void setSID(String [] s){s= SID;}
public void setExam(int [] e){e=Exam;}
}
I am creating a code that first asks user how many students are in the class. From this it asks several details to enter for each student and stored in their respective arrays. Problem: I can't put enter a String variable into my String array. I can't think of any way around this. Please help.
Upvotes: 0
Views: 178
Reputation: 3
First , I don't konw why you create the constructor like this
public Student (String f, String l, String h, String s, int e, int n)
Meanwhile , you override each parameter in it
Second , one way to convert a String parameter into array is
char arr1[]=new char[len1];
arr1=s1.toCharArray()
String[] is an array of String , and char[] is a array of char ,which means a String.
Upvotes: 0
Reputation: 2286
Problem is :
for(int i=0;i >n; i++)
{
}
Write This :
for(int i=0;i <n; i++)
{
// your code
}
You are not initialize a String array .
import java.util.*;
class Student
{
private String [] first;
private String [] last;
private String [] HKID;
private String[] SID;
private int []Exam;
private int num;
Scanner kb= new Scanner(System.in);
int n=0;
public Student (){
System.out.println("Please enter number of students:");
n = kb.nextInt();
first = new String[n];
last= new String[n];
HKID=new String[n];
SID=new String [n];
Exam=new int [n];
for(int i=0;i <n; i++)
{
System.out.println("First name:");
first[i]= kb.next();
System.out.println("Last name:");
last[i]=kb.next();
System.out.println("HKID:");
HKID[i]=kb.next();
System.out.println("SID:");
SID[i]=kb.next();
System.out.println("Final exam score:");
Exam[i]= kb.nextInt();
}
System.out.print("Student Detail");
System.out.print("First Name \t Last Name \t HKID \t SID \t Final exam score \n " );
for(int i=0;i<n;i++)
{
System.out.print(first[i]+"\t"+last[i]+"\t"+HKID[i]+"\t"+SID[i]+"\t"+Exam[i]);
System.out.println();
}
}
public static void main (String[] args) {
new Student();
}
}
Upvotes: 4
Reputation: 3831
Firstly initialize all arrays to size 'n' which you are reading outside for loop.
Then,Inside your for loop you can directly mention
for(i=o;i<n;i++){
System.out.println("First name:");
f = kb.next();
first[i] = f;
}
kb.next() returns a String object so u can also directly do first[i]=kb.next();
The main issue with your code here is that you are passing string object to define size of array which is incorrect (i.e here new String[f]
)
Hope this helps!
All The Best!
Upvotes: 0