0
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.

2
  • 2
    Change your for lopp with for (int i = 0;i < n; i++) and it should work. Commented Apr 18, 2014 at 9:05
  • Still can't enter a String value into my String array. It reports cannot convert String to int for f,l,h,s in the constructor respectively Commented Apr 18, 2014 at 9:11

3 Answers 3

4

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();
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Still can't enter a String value into my String array. It reports cannot convert String to int for f,l,h,s in the constructor respectively
0

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!

2 Comments

Thank you this helped a lot. If you are looking for some extra cash, I am in need of some help with the rest of my java project. Let me know if you can or are willing to help.
accepting the answer is the way of telling that answer really helped at stackoverflow :P and inbox me your requirements at [email protected]
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.