1

I know this code is terribly written (first day of Java and programming), but I am writing a code in Java that will take an input from the user (the dice) and produce a random number from that dice. I have added a while loop to ask if the user would like to restart the program, but everytime I run it it tells me that it is an invalid input before I have inputted anything. Please help.

import java.util.Scanner;
import java.util.Random;
public class Java {
public static void main(String args[]){
    Scanner input = new Scanner(System.in);
    String restartChoice = "y";
    while (restartChoice == "y" || restartChoice == "Y"){
        int choice;
        System.out.println("Please choose which dice you would like to                       roll. 4/6/12 ");
        choice = input.nextInt();
        while (choice != 4 && choice != 6 && choice != 12){
            System.out.println("That is not a valid input, please try again... ");
            choice = input.nextInt();   
        }
        Random rand = new Random(); 
        int value = rand.nextInt(choice) + 1;
        System.out.print("You chose to roll the ");
        System.out.print(choice);
        System.out.print(" sided dice. The number is ");
        System.out.println(value);
        System.out.println("Would you like to restart? Y/N ");
        restartChoice = input.nextLine();
        while (restartChoice != "y" && restartChoice != "n" && restartChoice != "y" && restartChoice != "n"){
            System.out.println("That is not a valid input. Please try again. ");
            restartChoice = input.nextLine();
        }
    }
}

}

project_legacy
  • 29
  • 1
  • 1
  • 4
  • See [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Reinstate Monica -- notmaynard Jun 23 '13 at 18:44
  • @ the original poster, for a beginner, your code is not bad at all, except for the error that Reimeus pointed out, and so I don't agree with this statement: `"I know this code is terribly written (first day of Java and programming)..."` – Hovercraft Full Of Eels Jun 23 '13 at 18:45
  • @project_legacy Also, watch out for that first [`Scanner#readLine()`](http://docs.oracle.com/javase/6/docs/api/java/util/Scanner.html#nextLine()) call - check the docs but I think `restartChoice` would be empty, so that's why you're seeing `That is not a valid input...`. – Jonathan Jun 23 '13 at 19:10
  • Sorry, that's assuming you fix the String equality issues. – Jonathan Jun 23 '13 at 19:21
  • Note that even after you fix the errors pointed out already, this code will fail to produce random rolls because you're creating the Random object inside the loop. Move the `new Random()` to the start of `main()`, only calling it once, and just use `nextInt()` inside the loop. – Lee Daniel Crocker Jun 23 '13 at 23:07

2 Answers2

0

Scanner#nextInt() does not consume newline characters resulting in the character being passed through to the loop

while (restartChoice != "y" && restartChoice != "n" && restartChoice != "y" && restartChoice != "n"){
            System.out.println("That is not a valid input. Please try again. ");
            restartChoice = input.nextLine();
}

Add a nextLine statement after every nextLine statement to consume this newline character.

choice = input.nextInt();
input.nextLine();

Also the == operator compares Object references. Use String#equals:

while (restartChoice.equals("y") || restartChoice.equals("Y")) {

to protect against NullPointerException you can place the String literal first. Also equalsIgnoreCase can be used to give a shorter if statement expression:

while ("y".equalsIgnoreCase(restartChoice)) {

This change is required in for while statement expression.

Reimeus
  • 158,255
  • 15
  • 216
  • 276
  • Thank you for your answer, but I am still very confused. Even after comparing the strings as objects, it still prints that the input is not correct (that the restart choice is not "Y" or "N", even though nothing has yet been inputted. – project_legacy Jun 23 '13 at 19:11
  • This is due to `Scanner#nextInt` not consuming newline characters. See update – Reimeus Jun 23 '13 at 19:22
  • Thanks very much, I have got my program to work now. The help is much appreciated :) – project_legacy Jun 23 '13 at 19:51
0

Use String.equals(otherString)

Strings are objects, not primitives. You are currently comparing the addresses of your strings.

awiebe
  • 3,758
  • 4
  • 22
  • 33