0
import java.util.*;

public class java
{
private String m_name;
private String m_game;
private int m_score;
private int m_played;

public java( String name, String game, int score, int played)
{
    m_name = name;
    m_game = game;
    m_score = score;
    m_played = played;
}

public String getName()
{
    return m_name;
}

public String getgame()
{
    return m_game;
}

public int getscore()
{
    return m_score;
}

public int getplayed()
{
    return m_played;
}

public String toString()
{
    return "name: " + m_name + ", game: " + m_game + 
           ", Score: " + m_score + ", played: " + m_played;
}

public static void main(String[] args) 
{
   ArrayList<java> students = new ArrayList<java>();
   Scanner input = new Scanner(System.in);

   int menuChoice = 4;
   do {
       System.out.println("\t\t\tStudnent's Gaming Records");
       System.out.println("\t1. Add Student\t\t2. View Students \t3. Averages\t4. Exit");
       try {
           System.out.println("Enter a choice: ");
           menuChoice = Integer.parseInt(input.nextLine());
       } catch (NumberFormatException e) {
           continue;
       }

       if (menuChoice==1)
       {
           System.out.println("Player Name");
           String name = input.nextLine();

           System.out.println("Game:");
           String game = input.nextLine();

           int played = -1;
           do {
               try {
                   System.out.println("Time Played:");
                   played = Integer.parseInt(input.nextLine());
               } catch (NumberFormatException e) {
                   System.out.println("Enter a number!");
                   continue;
               }
           } while (played <= 0);

           int score = -1;
           do {
               try {
                   System.out.println("Score:");
                   score = Integer.parseInt(input.nextLine());
               } catch (NumberFormatException e) {
                   System.out.println("Enter a number!");
                   continue;
               }
           } while (score <= 0);


           java student = new java(name, game, score, played);
           students.add(student);
           System.out.println(student);

       } else if (menuChoice==2) {
           System.out.println("Students:");
           for (java student : students)
           {
               System.out.println(student);
           }
       }
   } while (menuChoice<3);
   System.out.println("swag");
}
}

This is my code so far, it works excellent but I'm having a hard time trying to figure out a few things firstly, when a user press's 1 I want the game entry, score entry and played entry to loop indefinitely until the user types "quit" when done so in the "2" section of my menu it will displayed all the games, scores and times the user has entered under one username. My second question is how can i check for empty strings? Thanks all in advance.

Adam
  • 33
  • 6
  • I mean pressing space bar or pressing return, it gives a blank entry in the arraylist. – Adam May 05 '16 at 15:21
  • 1
    I recognize that you're a beginner, so take this as a teachable moment, not a criticism. `null` is a term with a very specific meaning in Java. Empty strings are not `null`. – Ian McLaird May 05 '16 at 15:23
  • That's an empty string, not a null. – The Head Rush May 05 '16 at 15:23
  • space, newline, and carriage return (and tab, etc.) are not null. They are characters that can be present in a String object. There is also a character called NUL, which is also not the same as a null reference (which is no String object at all). You probably want to look at the [`trim()`](https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#trim--) and [`isEmpty()`](https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#isEmpty--) methods. – dsh May 05 '16 at 15:25
  • Thank you that's helped alot. – Adam May 05 '16 at 15:29

1 Answers1

0

I believe this is a homework, so I should not give the exact answer.

For the first question use while loop, something like this:

String userEntry = "1";
while(userEntry.equals("1")){
    //yourCode
}

For second question use .equals like the following:

if ("".equals(scannedString)){
   //do something
}else{
   // your code
}
Pritam Banerjee
  • 17,953
  • 10
  • 93
  • 108
  • Thank you for the insight, I just can't figure it out i've been at it for ages hence why I posted it. Well and truly baffled. – Adam May 05 '16 at 15:34