-3

I am suppose to make a one dimensional array until the user enters "alldone", however i dont know how to make it . This what i have and i know it is wrong .

The instruction are "Design a solution that requests and receives student names and an exam score for each. Use one-dimensional arrays to solve this.

The program should continue to accept names and scores until the user inputs a student whose name is “alldone”. After the inputs are complete determine which student has the highest score and display that student’s name and score. Finally sort the list of names and corresponding scores in ascending order."

Code so far:

String name = "";
    String highName = "";
    int highScore = 0;
    while (name != "alldone") {
        System.out.println("Enter name of student");
        name = input.nextLine();
        System.out.println("Enter grade of student.");
        int score = input.nextInt();
        if (name != "alldone" && score > highScore)
            highName = name;
        highScore = score;
        // System.out.println("Enter name"); name = input.nextLine(); }
        // System.out.println(highName + " had the highest score which was "
        // + highScore);
    }

now i have done this

     Scanner in = new Scanner(System.in);
     int size= 1;

     String[] studentNames= new String[size];

     System.out.println("Enter name of student");
     String input = in.nextLine();
     String name = input;

     if (!name.equals("alldone")) {
         for (int i = 0; i < size; i++)
             studentNames[i]= in.nextLine();
      } 
     else return;
     for (int k = 0; k<studentNames.length; k++) {
         System.out.println(studentNames[k] + " "); //so it can display names
ja1996
  • 11
  • 3
  • 1
    Can you share some code with us? Stack Overflow is not a free code-writing service, contrary to what you may have heard from other people. – Tim Biegeleisen Jan 24 '17 at 03:14
  • Please post some code written by you. – Naman Jan 24 '17 at 03:14
  • sorry i am new to coding and i thought i pasted it on to here – ja1996 Jan 24 '17 at 03:15
  • 1
    Please [edit] your post to include your code. – shmosel Jan 24 '17 at 03:19
  • Please move this from comments to the actual question. – Naman Jan 24 '17 at 03:20
  • 1
    Also, explain what problem you're having. *i know it is wrong* isn't a helpful problem description. – shmosel Jan 24 '17 at 03:20
  • My problem is that i do not know how to make the array until the word is typed "all done" is typed. I know how to sort and i know how to make it find the high score and student with the high score. @shmosel – ja1996 Jan 24 '17 at 03:26
  • http://stackoverflow.com/questions/1200621/how-to-declare-an-array?rq=1 – Naman Jan 24 '17 at 03:26
  • Possible duplicate of [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – shmosel Jan 24 '17 at 03:27
  • 1
    Delete your question (it isn't useful for any future reader) and then read: [How do I compare strings in Java?](//stackoverflow.com/q/513832) and [Scanner is skipping nextLine() after using next(), nextInt() or other nextFoo() methods](//stackoverflow.com/q/13102045). – Tom Jan 24 '17 at 03:27
  • If that makes you feel any better, using simple array to do this is mostly stupid since you don't have any upper limit on the amount of students you can get. Aaand you might also have a look at http://stackoverflow.com/documentation/java/99/arrays#t=201701240326597642234 – Deltharis Jan 24 '17 at 03:28
  • @ja1996 Again, include any relevant information in your post. They'll get lost in the comments. – shmosel Jan 24 '17 at 03:28
  • Yeah, Tom's link will fix one of your issues. Also, using !name.equals("alldone") rather than name != "alldone" will fix your original issue you asked about. – Nick Ziebert Jan 24 '17 at 04:06
  • would i do a for loop after that to begin adding names @NickZiebert – ja1996 Jan 24 '17 at 04:15

1 Answers1

1

you should use this instead:

        if (!name.equals("alldone"))
Nick Ziebert
  • 1,188
  • 1
  • 6
  • 17
  • Im sorry for the dumb question but after that would i make a For Loop to let the user enter names? or what would i do ? – ja1996 Jan 24 '17 at 04:19
  • Na, what you had at first is almost there. – Nick Ziebert Jan 24 '17 at 04:22
  • im sorry i meant to ask , how will i start the array , i have put System.out.print("Enter " + studentName.length + " values: "); for (int i = 0; i < studentName.length; i++) studentName[i] = input.nextLine(); – ja1996 Jan 24 '17 at 04:24
  • no, no for loop is needed. You need some sort of function like listofNames.add(name). I give you hint, use arraylist, not array. Google how to make arraylist. – Nick Ziebert Jan 24 '17 at 04:31
  • we havent gone into array list yet so i am only allowed to use array. – ja1996 Jan 24 '17 at 04:49
  • Ah, ok. This is what you need to do. First off, even though it's a number, you need to do String score = input.nextLine(). Then, create a String array of size 2 and put your name in the first position and score in second position. And then do that if test that you have above, grabbing the second element of that array. Google how to do that. – Nick Ziebert Jan 24 '17 at 04:57
  • im sorry to be annoying, however, i have to use one-dimensional array. Its due in a couple hours so im trying to figure it as best as i can – ja1996 Jan 24 '17 at 05:06
  • Your not being annoying. String[] myArray = new String[2] is a one dimensional array with 2 elements. I have no idea why your teacher felt the need to specify "one dimensional", it's sort of implied. – Nick Ziebert Jan 24 '17 at 05:20
  • yea, i got lost there, im a newbie to the coding world, im a visual learner, however its hard to understand. Ive changed my code and it still doesnt loop so i can enter another name into the array. – ja1996 Jan 24 '17 at 05:33
  • Alright, update your code above, lets see what you tried. – Nick Ziebert Jan 24 '17 at 05:35
  • Thats as far ive gotten and nothing seems correct. Its really stressful – ja1996 Jan 24 '17 at 06:00
  • I think your teacher is trolling you. This is a pretty complicated assignment when you are confined to only using single dimension arrays. I'm trying to figure out how this is actually posisible. You have to sort the list? What list? – Nick Ziebert Jan 24 '17 at 06:22
  • Have you studied classes? The only way you can solve this is by creating a student class. I've been assuming not if you are still on arrays. – Nick Ziebert Jan 24 '17 at 06:27
  • he may be. i am not sure. but i have to get the assignment done, could you show me how the arraylist works – ja1996 Jan 24 '17 at 06:54
  • I really can't. Just give him what you have. I'd be interested to know if anybody in your class was able to complete this assignment without using classes. – Nick Ziebert Jan 24 '17 at 07:02
  • well im doing it with setting the array to a fixed sized by asking the user "how many students will you enter " and than using that number to set the size – ja1996 Jan 24 '17 at 07:08