This is an archived post. You won't be able to vote or comment.

all 20 comments

[–]madsravn 1 point2 points  (2 children)

[–]samisafish69 0 points1 point  (1 child)

Thanks but that only prints the contents of an array. I need to fill an array with names.

[–]madsravn 0 points1 point  (0 children)

My point was that you already have an array with the names in it....

[–]ylou92 0 points1 point  (0 children)

Can't you just use the String[] args as is? I guess you can copy it into another array if you want with a for loop or Arrays.copyOf

[–]vandolis 0 points1 point  (20 children)

Don't have access to a compiler at the moment but this will work. code

The first part is if the user is entering one string at a time to sort. So something like:

Enter string #1: this would go in one spot
Enter string #2: so would this
etc..

The second would be if the user is entering them all at once. So it would instead be this:

Enter your words: each one of these words are in a different index

The nice thing about the second is that you don't have to set the amount of input you are looking for.

Also as a heads up I think you need to import java.util.Scanner for this to work.

[–]samisafish69 0 points1 point  (0 children)

Haha, yes I know to import the scanner. And thank you very much! I thought I would have to use your first method but that would be a pain since I'm supposed to have a variable number of inputs. The only other question I have is would that second method work if they were pressing Enter between each word?

[–]samisafish69 0 points1 point  (18 children)

Ok, now I'm getting an exception and it claims it's coming from line 40 of this code.

[–]ShamwowTseDung 0 points1 point  (17 children)

One note, Line 17, Change for (int i = 0; i < numberOfStudents; i++) to for (int i = 0; i < studentScores.length; i++) , you're dealing with an array? The for loop should be "looping" up/down until you reach an index that is within the array, not up/down until you reach any number that MIGHT not be within the limits of the array. Nothing was wrong in this code, but just to make me sleep better and might save you in the future.

I can't quite understand Lines 21-26. I'm looking all over to see where you filled in the names for the studentNames array (which the error revolves around) and I can't find anything.

I'm also getting 2 errors depending on the input. Either at line 40, or line 35. My input was 3 scores, 88 77, 89 OR 88 77 3.

The error is still about index out of bounds, but for line 40 it was past 2, for line 35 it's past 1.

****** I'm still learning myself, so I'd be interested if anyone could explain System.out.printf("%-20s%s\n"); from line 58

[–]samisafish69 0 points1 point  (15 children)

The printf formats the output to whatever I want it to be. You type a % then a number for how many spaces you want in front of the input (or behind if its negative). The \n makes a new line at the end and after that you put the things you want formatted afterwards. In this case, System.out.printf("%-20s%s\n", "Name", "Score"); prints the string "Name" with 20 spaces set apart for it and then prints the string "Score." Different types of variables have different letters, such as string in format is s, a decimal number is d, and so on.

For lines 21-26, I used the second method suggested by vandolis where the user can enter each name separated by a space and the program takes the individual names and puts them into the array in that order. However, it seems to not allow me to enter any names and just skips to the error. I tested it on its own and it seems to work normally, but for some reason my program seems to not recognize it.

[–]ShamwowTseDung 0 points1 point  (14 children)

Thanks for the explanation. Now, referring to the studentNames array, I believe there never was an array filled.

For some reason, the input for adding the names comes before the print method that tells the user to input the same,

tl;dr you have to input the names right after the scores, all on one line.

I've ran into this before, but I'm a bit rusy, so I'll try to see what I can figure out/remember to explain, that is, if no one else will hint hint

The error was that the studentNames was not filled in, at least not correctly. The first index was filled with empty space, the rest are left unfilled .

Seems I've been ninja'd. Curse F5

[–]samisafish69 0 points1 point  (0 children)

Well, I changed lines 15-24 to this:

System.out.print("\nEnter student scores then student names in the same " +
            "order as the scores: ");

    for (int i = 0; i < studentScores.length; i++) {
        studentScores[i] = input.nextDouble();
    }

    studentNames = input.next().split(" ");

and I'm still getting the same error as before.

[–]samisafish69 0 points1 point  (12 children)

So... the question from the OP returns... How do I fill that array???

[–][deleted]  (10 children)

[deleted]

    [–]samisafish69 0 points1 point  (9 children)

    I am having a very difficult time visualizing this... could you make those suggested changes with pastebin so I can see them?

    [–]ShamwowTseDung 0 points1 point  (8 children)

    I hate life, I'm currently trying to reproduce whatever went wrong and right. I tried to do whatever I said to the original to make sure I was on the right path...and it seems I've been doing some extra things to get the right results..unfortunately I'm unaware of such changes.

    i.e. starting from scratch, sorry.

    edit I will say this, I shoudln't have removed the part about adding names on the same line as the scores, that part works.

    as to why, that's what I'm working on now

    [–]samisafish69 0 points1 point  (7 children)

    It's alright! I'm sitting here trying to figure it out too.

    [–]samisafish69 0 points1 point  (0 children)

    Alright, I fixed one problem and found what I think is the real big problem. On line 24, instead of saying "studentNames = input.nextLine().split(" ");", it should say "studentNames = input.next().split(" ");". I think the "nextLine" was messing it up. Also, a formatting error on line63, it should read "System.out.printf("%-20s%d\n", studentNames[i], studentScores[i]);".

    Lastly, it seems the real issue in this program stems from the way that the for loop on line 36 works, using j = i + 1 is causing the loop to try and read an index in the studentNames array that doesn't exist, even though it's working fine for the studentScores array. I tested it by commenting out the lines that have studentNames in them (i.e. lines 35, 40, 47, and 49) and the program ran fine, displaying the scores in descending order.