1

I am working on a dice game in a java program (jGRASP specifically) and it is kicking my can. I need to return a name and the number of dice per hand from the user input. Here is my (relevant) code so far:

import java.util.*; 

public class DiceGame {
   public static Scanner input = new Scanner(System.in);

   public static void main(String[] args) {
      System.out.println("Welcom to the Lab 7 Dice Game.");
      name();
      dice();
   }
   public static String name(){
      System.out.print("What is you name? ");
      String name = input.next();
      return name;
   }
   public static int dice(){
      System.out.print("How many rolls per round? ");
      int dice = input.nextInt();
      return dice;
   }
}

The method gives my entry line and asks the user for an String input as a name. That works just fine, it will print that out as intended. But when it moves on to calling the dice method, I get an "InputMismatchException" at "dice();" in main and at "int dice = input.nextInt();" in my dice method. Really, I am just looking for an explanation. I looked it up in the textbook I have, and looking it up elsewhere, but I cannot find an explanation.

Jon Akers
  • 71
  • 9
  • 1
    Related: https://stackoverflow.com/a/13102066/3699139 – Jorn Vernee Feb 19 '18 at 17:47
  • 3
    Possible duplicate of [Scanner is skipping nextLine() after using next() or nextFoo()?](https://stackoverflow.com/questions/13102045/scanner-is-skipping-nextline-after-using-next-or-nextfoo) – Frakcool Feb 19 '18 at 17:48
  • Just call `input.nextLine()` after this line: `int dice = input.nextInt();` – Frakcool Feb 19 '18 at 17:50
  • Or you could also do `Scanner input = new Scanner(System.in).useDelimiter("\\s+");` Which will ignore any white space between tokens (including newlines). – Jorn Vernee Feb 19 '18 at 17:54
  • @Frakcool I looked at the one that I may have duplicated, but I find that one confusing (that may be because I am a major java noob...) and your second reply also doesn't make a whole lot of sense. I thought that using "input.nextInt" would call for user input, as my scanner connected to the System.in is named "input" Could you possibly explain that a bit more? – Jon Akers Feb 19 '18 at 17:54
  • @JornVernee part of my problem is that I can only use fairly basic java code, only being allowed to use what I have learned in my java class. the ".useDelimiter" code is like complete Greek to me, but thanks for trying to help out. – Jon Akers Feb 19 '18 at 17:56
  • 1
    My bad, that line was after `input.next` in `name()` method... (And you should be using better names for your methods as `name()` sounds more like a variable than a method, method names should start with a verb). Whenever you call `next()` the `Scanner` class is still waiting for another parameter which is never read because it expects it to be on the same line. When you add that line `input.nextLine()` you explicitly say to the `Scanner` to read the next token in the next line after you've pressed `Enter` – Frakcool Feb 19 '18 at 17:58
  • SO expects you to research your problem on your own and then show that research. If the linked duplicate is part of that research, but you find it confusing, please [edit] the question and explain why. – Jorn Vernee Feb 19 '18 at 18:00
  • Most of the time, you don't want `Scanner.next`, you want `Scanner.nextLine`, which consumes the newline character as well. But if it's an issue with that, you would notice that it's not waiting for input after calling `name()` (which needs refactoring, by the way). Is that happening, does the program run and error right after the first input? – Sinjai Feb 19 '18 at 18:27
  • @Sinjai `Scanner.next` is plenty useful. Mixing `nextLine` with other `next...` methods leads to mildly confusing and error-prone code. – Bernhard Barker Feb 19 '18 at 18:49
  • What input are you giving to your program? `next` reads up to the first whitespace character, so if you attempt to input e.g. `John Smith` as a name, that'll be a problem and you should use `nextLine` instead. – Bernhard Barker Feb 19 '18 at 18:54
  • @Dukeling I'm not saying `next` is never useful, but a beginner is usually better off avoiding it. Although I can't think of a case, off the top of my head, in which I would rather call `next` a bunch of times than `nextLine` followed by `String.split`. – Sinjai Feb 19 '18 at 18:54
  • Thank you to everybody who helped out. Looking at my entire code, it turned out I had an extra } thrown in near the end of the main method. I had comments throughout the code, so it was rather hidden from the section of the screen that I was working in. – Jon Akers Feb 20 '18 at 00:02
  • @JonAkers Ah, the classic "misplaced bracket causes ambiguous error" problem. Glad you figured it out. – Sinjai Feb 21 '18 at 02:36
  • So am I. That thing is a complete nuisance. And it is almost impossible to figure out unless you happen to think about those stupid brackets. Then you have to go through and figure out where you are one short or one too many. I seem to do that a lot. – Jon Akers Feb 21 '18 at 05:12
  • @JonAkers Actually, I can't think of an instance wherein the compiler is okay with an extra bracket. By all rights, you should have gotten a compile-time error, not a runtime error. Any chance you could [send me the code](https://hastebin.com) in question? You've piqued my interest. – Sinjai Feb 22 '18 at 00:44
  • @Sinjai, now that you mention it, I get what you mean. That is a bit of a curiosity. And I would be more than happy to show you the code, but I have since made many updates to the code, and I honestly can't think of the code that I had the error in. Sorry to let you down. – Jon Akers Feb 23 '18 at 04:04
  • You have disappointed me. I will never forget this day. By the way, I would recommend upvoting answers you think are well-written, regardless of the fact that your issue ended up being different, and consider writing and accepting an answer of your own. – Sinjai Feb 23 '18 at 09:01
  • @Sinjai, thanks. I will make sure to think about that on both this one and in the future. – Jon Akers Feb 24 '18 at 02:46

2 Answers2

0

From what I can tell from your question, it's because Scanner is unintuitive. You seem to be saying it doesn't wait for input the second time. If that's not the case, this answer is unhelpful.

The next() method takes in the next space-delimited string, meaning if you type
I want all these words
it will take in I, with the buffer sitting in front of want all these words. So, when you call nextInt(), it takes in the next input, which is want, and that is not an int.

So, use nextLine() instead of next() unless you truly only want the next word, and call nextLine() after nextInt() to force the Scanner to consume the newline character.

import java.util.*; 

public class DiceGame {
   public static Scanner input = new Scanner(System.in);

   public static void main(String[] args) {
      System.out.println("Welcome to the Lab 7 Dice Game.");
      name();
      dice();
   }
   public static String name(){
      System.out.print("What is your name? ");
      String name = input.nextLine();
      return name;
   }
   public static int dice(){
      System.out.print("How many rolls per round? ");
      int dice = input.nextInt();
      input.nextLine();
      return dice;
   }
}
Sinjai
  • 800
  • 7
  • 30
0

The dice method is expecting to return an int value. When you are entering a value after the line "How many roles per round?" is printed ensure you are entering an int and not another type. For example, values 1, 2, 3 is what your code is expecting not one, two, three.

Alternatively try replacing input.nextInt() with input.nextLine()

Craig Martin
  • 45
  • 10