1

I want to push values from a Scanner to an Array. I want to know if this code is right. How to do if it is wrong? This is a method I call over and over again. So I want to store that user input values in an Array.

    String[] student_id = new String[150];  
    String studentId = "";

    System.out.print("Enter Student ID   : ");
    studentId = input.nextLine();
   
    student_id = studentId;
   
    for(int i =0;i<student_id.length;i++) {
       student_id[i] = input.nextLine();
    }
2
  • 2
    This code is reading one value into studentId and then filling up the array with this value. Commented Oct 13, 2021 at 5:43
  • 1
    you are already doing that, but you should replace the line in your for loop by: student_id[i] = input.nextLine(); Commented Oct 13, 2021 at 5:44

1 Answer 1

1

easy

String[] student_id=new String[150];        
    System.out.print("Enter Student ID   : ");
    for(int i =0;i<student_id.length;i++){
    student_id[i] = input.nextLine();
}
Sign up to request clarification or add additional context in comments.

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.