0

I need to make a calculator for students grades using java. The user types in the name and score for a student on a single line separated by commas. It needs to take 10 students details. But I can not work out how to split the user input string using commas, and fill it into my array. Any help would be appreciated. Thanks

public static void main (String[] args){

    Scanner scan = new Scanner(System.in);
    String[][] student = new String[10][];

    for (int index=1; index <=10; index++){
    String userinput = scan.nextLine();

2 Answers 2

2

You want String#split, which will return a String[].

Also, arrays in Java are zero-based, so you need

for(int index = 0; index < student.length; index++)
Sign up to request clarification or add additional context in comments.

1 Comment

ahhhh Thankyou, I think I just had a break through.
0

No need for the loop. If you want to fill in your array, you should use the split() method of the String class, like so:

String userInput = scan.nextLine();
// input comma-separated grades
String[] grades  = userInput.split(",");

And now you have a grades array with each element as an individual grade

1 Comment

The array is for the 10 students.

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.