-2

im trying to write a program that will accept input of "put name mark", "get name mark" and "quit"

upon the user entering "put name mark" the program will prompt them to enter a student name and mark and then stores it at the next available array index.

the "get name" command will accept a name input from the user and they iterate through the array and display any mark matching the name entered.

the "quit" command will end the program and return the mean mark and the highest mark in the display.

the problem im having is that it dosent seem to be entering the loop when i type the required words in. it just jumps to where it asks the question again and wont even accept input

im still a beginner and ive been working on this program for 4 weeks so any help would be greatly appreciated.

package week14;

import java.util.Scanner;
public class week {


public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    //sets number of string inputs
    {
    String[] names = new String[50];
    double[] scores = new double[50];
    // Enter student name and score

    System.out.print("please enter either: quit, put name mark, get name");
    input.next();


        if(input.next() == "put name mark" )
        {
        System.out.print("Enter Student Name");
        names[50] = input.next();
        System.out.print("Enter Score");
        scores[50] = input.nextInt();
        }

    System.out.println("please enter either: quit, quit, put name mark, get name");
    input.next();

        if(input.next() == "get name")
        {
        System.out.print("please enter the name you would like to display the score for");

        String get = input.next();
        }

    // Sort
    for (int i = 50 - 1; i >= 1; i--) {
        // Find the maximum in the scores[0..i]
        double currentMax = scores[0];
        int currentMaxIndex = 0;
        for (int j = 1; j <= i; j++) {
            if (currentMax < scores[j]) {
                currentMax = scores[j];
                currentMaxIndex = j;
            }
        }

        // Swap scores[i] with scores[currentMaxIndex];
        // Swap names[i] with names[currentMaxIndex] ;
        if (currentMaxIndex != i) {
            scores[currentMaxIndex] = scores[i];
            scores[i] = currentMax;
           String temp = names[currentMaxIndex];
            names[currentMaxIndex] = names[i];
            names[i] = temp;
        }

        if (input.equals("quit")){
        System.out.print(names[i] + scores[i]);
        System.out.println();
        System.out.print(currentMax);
        break;
        }
            }
        }
    }
}
Alex Davies
  • 11
  • 1
  • 5
  • 4
    possible duplicate of [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – njzk2 Feb 09 '15 at 17:37
  • First you can't compare Strings with == you must use .equals(object) Second you use input.next() after the question this makes the first if useless. – KriszDev Feb 09 '15 at 18:17
  • Also "names[50] = input.next();" will not work it will only set the 51th one in names (arrays begin with index 0 so the 50th element is "names[49]"). Do you know functions yet? – KriszDev Feb 09 '15 at 18:28
  • i dont know functions yet but ill start watching an online tutorial right away. so i should set the arrays to 0? – Alex Davies Feb 09 '15 at 18:29
  • Come here http://collabedit.com/jyd6a . I will rewrite it for you and also I will try to make is easy to you to understand. – KriszDev Feb 09 '15 at 18:36

1 Answers1

0

That's what i got for now maybe there are some errors if there is any problem say what's it and I'll fix it.

import java.util.Scanner;

public class Week
{
    public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in); //Scanner used to get input from the user
        String[] names = new String[50]; //The array for names remember arrays index began with 0 not 1
        int[] scores = new int[50]; //Again arrays began with 0 not 1 and the last is n-1
        int last = 0; //Used to store the last added ID
        String command; //The command from the user
        boolean running = true; //Whenever the program is running or not
        while(running)
        {
            System.out.println("please enter either: quit, put name mark, get name"); //Print the command list
            command = input.nextLine(); //The next input line (This will make the Thread pause untill it get and input)
            if(command.equals("put mark")) //If the command is "put mark"
            {
                if(last == 49) //Check because we can create and Exception by adding too much element to and array
                    System.out.println("Max number of people reached"); //So we can't add more people
                else
                {
                    System.out.println("Enter Student Name"); //Print the questin
                    names[last] = input.nextLine(); //The name
                    System.out.println("Enter Score"); //Ask for the score
                    scores[last] = input.nextInt(); //Get the score ,because score is a double we should use double so it can take numbers like 0.1
                    last++; //Increment last with 1
                }
            }else if(command.equals("get name"))
            {
                System.out.println("please enter the name you would like to display the score for");
                String name = input.nextLine(); //Get the name
                for(int i = 0; i < last; i++) //Loop untill we hit the last added name's ID
                    if(names[i].equals(name)) //Check if the names[i] is the name that we're searching for
                        System.out.println(name + " 's score is " + scores[i]); //If it's then we print it out
            }else if(command.equals("quit"))
            {
                running = false; //The loop will never run again
                //Implement sorting for youself I would use Map<K, V> but you didn't learned it so..
                //In this case you have to make 1 loop to sort both of the arrays by sorting the second array
                //and when you move anything must it in both arrays I can't help you to make this sorry

                for(int i = 0; i < last; i++) //We print the sorted arrays of the people and their scores
                    System.out.println(names[i] + " 's score is " + scores[i]); //Let's print it
            }
        }


    }
}
KriszDev
  • 86
  • 1
  • 5