0

Yes this is an assignment... I've got 2 arrays; one for student names and one for their scores. I've asked the user to input the number of students to initialize the sizes of both, and then loop through the input process to fill the elements. But the weirdest thing happens that hasn't happened before. It seems that the student array is cut short by one element when the code is run (after 4 entries the program jumps to the next input loop), but even weirder is that the truncation seems to be at the front of the array, because the scores loop starts with a blank where a name should be but allows for 5 inputs.

    import java.util.Scanner;

    public class Ex6_17SortStudents {
    public static void main(String[] args) {           
            Scanner input = new Scanner(System.in);

    int numOfStu;
      String[] students;
      double[] scores;

    System.out.println("Enter the number of students being recorded: ");
    numOfStu = input.nextInt();

    students = new String[numOfStu];

    System.out.println("Enter students' names: ");
    for (int i = 0; i < students.length; i++)
        students[i] = input.nextLine();


    scores = new double[numOfStu];
    for (int i = 0; i < students.length; i++) {
        System.out.print("Enter score for " + students[i] + ": ");
        scores[i] = input.nextDouble();
            }
         }
     }

Any ideas why this happens?
There's eventually a sort but that's a mess i think i have a handle on. Sorry if the format for the post is wrong -- first time posting; trying my best.

thanks

ashape
  • 3
  • 2
  • 1
    Two warnings: always use curly braces, and ```System.out``` only gets flushed on newlines, so your "Enter score for...." might not be getting flushed. You should do ```System.out.flush()``` to do that. – David Ehrmann Apr 02 '14 at 23:32
  • @DavidEhrmann guess i forgot to mention that i'm a 2.5 month old programmer, so as much as i appreciate the help all that flushing might as well be greek. – ashape Apr 02 '14 at 23:39
  • @ashape give this a read: https://stackoverflow.com/questions/7166328/when-why-to-call-system-out-flush-in-java – David Ehrmann Apr 02 '14 at 23:42
  • That fact that you're programming at 2.5 months old is...just amazing. Talk about a child prodigy! :) – aliteralmind Apr 02 '14 at 23:47

2 Answers2

0

This debugging output should give you a clue to your problem:

System.out.println("Enter students' names: ");
for (int i = 0; i < students.length; i++)  {
   System.out.print("Name index " + i + ": ");
   students[i] = input.nextLine();
}

And this answer to this question is exactly the answer you need.

Community
  • 1
  • 1
aliteralmind
  • 19,847
  • 17
  • 77
  • 108
-1

Use students[i] = input.next();

Just checked it, and it works now.

nextLine() advances your scanner past the current line and returns the input that was skipped -- so you were pretty much skipping a line. The first time it enters the loop, you lose an i value, that is i is now 1, yet your scanner does not record user input. The second time around, when i is 1, it takes input, and so forth.

New code:

public static void main(String[] args) {           
    Scanner input = new Scanner(System.in);
    int numOfStu;
    String[] students;
    double[] scores;

    System.out.println("Enter the number of students being recorded: ");
    numOfStu = input.nextInt();
    students = new String[numOfStu];
    scores = new double[numOfStu];

    System.out.println("Enter students' names: ");
    for (int i = 0; i < students.length; i++) {
           students[i] = input.next();
    }
    for (int i = 0; i < students.length; i++) {
          System.out.print("Enter score for " + students[i] + ": ");
          scores[i] = input.nextDouble();
    }
 }
  • Oh i had no idea! thanks! I see you left the nextDouble alone. Is it safe to assume then, that the nextLine is the only one (so far in new programming) that does this? – ashape Apr 02 '14 at 23:44
  • @ashape next is for strings, i.e you expect the user to pass you a string, whereas nextDouble would be if you expect a double. – StonyBrook Apr 03 '14 at 00:15
  • I thnk you got the downvote for doing the OP's homework. – aliteralmind Apr 03 '14 at 03:24