Question

This line does not work: input=scan.nextLine();

You should give the program the input for the while loop, but it ignores completely this line. I don't know the problem and I am just a beginner. Could you help?

import java.util.*;


public class Fantasy2
{

  public static void main ( String[] args )
  {
    String name;
    String input="y";
    int strength;
    int health;
    int luck;
    int stats;
    Scanner scan = new Scanner(System.in);

    System.out.println("Welcome!");
    System.out.println("Please enter the name of your character:");
    name=scan.nextLine();

    while(input.equals("y"))
    {
    System.out.println("Strength (1-10):");
    strength=scan.nextInt();
    System.out.println("Health (1-10):");
    health=scan.nextInt();
    System.out.println("Luck (1-10):");
    luck=scan.nextInt();
    stats=strength+health+luck;

    if(stats>15)
        {
        System.out.println("You gave your character too much points! Do you want to change that?");
        input=scan.nextLine();
        } 
    else
        {
        System.out.println(name + " has these stats now: Strength:" + strength + ", Health: " + health + ", Luck: " + luck);
        input="n";
        }
    }
    System.out.println("Congratulation! You created successful your character!");
  }
}
Was it helpful?

Solution

Sorry the statement posted below does not help your original problem but is another problem the line input = scan.nextLine(); works for me fine.

When using Scanner you must call another scan.nextLine(); after you scan.nextInt(); to get the line feed that the method scan.nextInt(); does not get

do this

System.out.println("Strength (1-10):");
strength=scan.nextInt();
System.out.println("Health (1-10):");
health=scan.nextInt();
System.out.println("Luck (1-10):");
luck=scan.nextInt();
stats=strength+health+luck;
scan.nextLine();    <--------- added code

OTHER TIPS

You have to consume the extra new line that nextInt does not consume. Your options are to call another newLine after the last call to nextInt or better yet, read the integer input through nextLine and parse it:

private int readInteger()
{
   int option = 0;
   try 
   {
      option = Integer.parseInt(input.nextLine());
   } 
   catch (NumberFormatException e) 
   {
      e.printStackTrace();
   }
   return option;
}

...
System.out.println("Strength (1-10):");
strength = readInteger();
System.out.println("Health (1-10):");
health = readInteger();
System.out.println("Luck (1-10):");
luck = readInteger();

With the Scanner

Scanner scan = new Scanner(System.in);

and the methods ( read API for detail)

next() - would read next characters until it found space ( or the delimiter used. by default space) and return String.

nextInt() - would read next characters until it found space ( or the delimiter used. by default space) and convert it into int and return.

Note** if you give input as 'xyz' you will get exception and your program would terminate.

if you give 5 7 for answer (Strength (1-10):) as per your program it would read 5 as strength and 7 as health. and will ask for luck.

nextLine() would read all characters until the end of line found ( while you enter in keyboard).

So the ideal implementation to avoid issues .

You can use nextLine and convert into int if not say as error and ask to enter correct value.

System.out.println("Strength (1-10):");
            String str=scan.nextLine();
            while (!isNumeric(str)){
                System.out.println("Enter Valid Number Strength (1-10):");
                str=scan.nextLine();
            }
            strength = Integer.parseInt(str);

 public static boolean isNumeric(String str)
        {
            return str.matches("-?\\d+(\\.\\d+)?");  //match a number with optional '-' and decimal.
        }

For example , when you call luck=scan.nextInt(); users enters 6 and enter key . 6 will capture by nextInt() and the enter key would capture by input=scan.nextLine(); . So in order to get next input you have to ask scan.nextLIne() again like

 luck=scan.nextInt();
scan.nextLIne();

Try to use this code instead of your while cycle:

    boolean flag=true;
    while(flag)
    {
    System.out.println("Strength (1-10):");
    int strength=Integer.parseInt(scan.nextLine());
    System.out.println("Health (1-10):");
    int health=Integer.parseInt(scan.nextLine());
    System.out.println("Luck (1-10):");
    int luck=Integer.parseInt(scan.nextLine());
    int stats=strength+health+luck;

    if(stats>15)
        {
        System.out.println("You gave your character too much points! Do you want to change that?");
        input=scan.nextLine();
        } 
    else
        {
        System.out.println("has these stats now: Strength:" + strength + ", Health: " + health + ", Luck: " + luck);
        input="n";
        }

    if(input.equals("n"))
        flag = false;

    }


    System.out.println("Congratulation! You created successful your character!");

I don't know if this is a better way of doing it but give it a try. Hope this helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top