简体   繁体   中英

My function with scanner crashes instead of returning input number (java)

I created a function that asks user for a number. The function checks if it is a positive integer and if it is an integer at all, and should return that number.

The program crashes every time if the user puts in a positive integer. I am new to eclipse IDE and don't know where to look for a log of runtime errors.

I already tried return height.nextInt() and deleted the z variable, didn't work. (This function is for a bigger program that eventually prints out a pyramid.)

package skener;

import java.util.Scanner;

public class MyClass 
{
    public static int z = 0;
    public static int userInput()
    {
        Scanner height = new Scanner(System.in);
        System.out.println("Please choose a number: ");
        while(!height.hasNextInt() || (height.nextInt() < 0))
        {
            //height = new Scanner(System.in);
            System.out.println("Wrong number, try again: ");
            height = new Scanner(System.in);

        }
        z = height.nextInt();
        height.close();
        return z;

    }
    public static void main(String[] args)
    {
        System.out.println(userInput());
    }
}

If user input is 3, I expect to get return 3, to use in another program. Instead the program simply terminates and the terminal doesn't provide any error messages and doesn't return the input.

your program should be like below,

Scanner height = new Scanner(System.in);
        System.out.println("Please choose a number: ");
        z = height.nextInt();
        while((z < 0))
        {
            System.out.println("Wrong number, try again: ");
            z = height.nextInt();

        }

        return z;

here we are getting the integer value in z and we are checking the condition and returning if it's positive or we will ask user to enter again. Please use import scanner module in import java.util.Scanner;

I believe this is what you want.

      Scanner height = new Scanner(System.in);

      System.out.println("Please choose a number: ");
      int z = -1; // default value means no valid integer entered.
      // ensure that the next value is an int and if so. 
      // it is >= 0.

      while (height.hasNextInt() && (z = height.nextInt()) < 0) {
         System.out.println("Wrong number, try again: ");
      }
      // Display the value
      System.out.println("z = " + z);
      // and if in a method, return it.
      return z;

Your solution was very close. You should not have closed the scanner because that also closes the Console input which you would not be able to re-open and read from.

And as you specified in your original code, if you don't check for a non-numeric value, before getting an int, your code will throw an exception.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM