How to capture non-integers in this Java program
NickName:Darren Estcourt Ask DateTime:2015-06-11T06:33:30

How to capture non-integers in this Java program

How do I add a try-catch piece of code to stop someone from entering chars or, as a matter of fact, anything other than an int from 1 - 5?

boolean valid;
int option = 0;

do {
    Scanner in = new Scanner(System.in); // need try catch
    menu();
    System.out.println("\n");
    option = in.nextInt();

    valid = option > 0 && option < 6; // try / catch needed around here?

} while(!valid); // stop chars and strings being entered
// I want to stop the user entering anything other than an int 1-5

Copyright Notice:Content Author:「Darren Estcourt」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/30768458/how-to-capture-non-integers-in-this-java-program

Answers
Skaros Ilias 2015-06-10T22:42:30

if you need that number to check which option the user entered you dont need to do that. all you have to do is change the option variable from int to String and your if statements from\nif(option==1) to if(option.equals(\"1\"))\n\nif you are going to need real ints for real mathematical equations then Sheetals answer will be more appropriate, but for a menu input, Strings are just fine.\n\n\n\ndont forget that you can have String comparison with that contain numbers\n\"1\"<\"2\" is true",


Arun Kumar 2015-06-11T14:33:38

Use string to enter the choice and then parse it using Integer.parseInt() function.\n\nScanner in = new Scanner(System.in); \nString choice = in.next();\ntry{\n int intChoice = Integer.parseInt(choice);\n if(choice > 5){\n throw new Exception(\"Can't be more than 5\");\n }\n}catch(Exception e){\n System.out.println(e.printStackTrace);\n}\n",


More about “How to capture non-integers in this Java program” related questions

how to capture stdout from embedded jython on java program

i want to use jython on a java program,so i want to capture the stdout in jython script then i can get the output on my java context,is there some way to archive this ?

Show Detail

Capture IO of spawned program in Java

I'm writing a program in Java which relies on a pre-compiled third party JAR residing in the same directory as mine. At runtime, my program checks if this file exists, then downloads it if it does...

Show Detail

Which program to use to capture Java output as video

I need to capture the output of a Java Swing program as video for the purpose of demonstration. I have tried Taksi video recorder and CamStudio, both of them are taking the video as a small rectangle

Show Detail

How to capture non-integers in this Java program

How do I add a try-catch piece of code to stop someone from entering chars or, as a matter of fact, anything other than an int from 1 - 5? boolean valid; int option = 0; do { Scanner in = new

Show Detail

How to remove all non-integers from a string? (Python)

I'm pretty new to python. I have a question. Say for example when I read a line from a file I have a string that looks like this. thestring = '000,5\r\n' How do I remove all non-integers from ...

Show Detail

Full screen capture of a DirectX program in java (Javacv ?)

[For Windows] I know it is possible to capture screen of a DirectX program running under C# language, but do you know some sample code for Java? I am actually facing this same problem than this Take

Show Detail

Accepting integers and ignoring non-integers from a file C#

Currently I have written code that properly displays an error message if the text file contains ONLY non-integers foreach (var line in System.IO.File.ReadLines(fileName)) if (fileContents.Any(char.

Show Detail

How to capture screenshot using a java program wrapped as windows service

I have a java program to capture screenshot on Windows. Here is part of the code. It is generating a black image when wrapped by YAJSW and run as a windows service. BufferedImage image = new Robo...

Show Detail

Capture Keystrokes when program is not active

I code in java. I wrote a keylistener for the frame and it prints all the keystrokes when the frame is active, but when i minimize it or deactivate it, the program obviously stops and no keystrokes...

Show Detail

Skip non-integers from text file C++

I have a program which reads integers from a text file and skips non-integers and strange symbols. Then text file looks like: # Matrix A // this line should be skipped because it contains # symb...

Show Detail