Click here to Skip to main content
15,882,055 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need two options (option 1 and option 2) and i have to input them using a scanner variable so if I do option1 = sc.nextInt(); and option2 = sc.nextInt();
they basically are the same thing but I need different inputs as shown in the example below. I tried doing an integer input for option2 which would be 2, but it still wouldn't work

What I have tried:

import java.util.Scanner;
public class LessThan{
public static void main(String[]args){
Scanner keyboard = new Scanner(System.in);
int value = keyboard.nextInt();
int option1 = keyboard.nextInt();
int option2 = 2;
int num1 = 5;
int num2 = 10;
int num3 = 20;
if (option1
Posted
Updated 14-Feb-20 21:58pm

1 solution

Java.util.Scanner.nextInt() Method - Tutorialspoint[^]

You don't have to change the variable. If you need to input two distinct values, just call nextInt() method twice. It is also advised to check for the validity of the input, and react accordingly if it wasn't of expected format. As a general rule, never trust user-input strings.
Java
Scanner keyboard = new Scanner(System.in);
int option1, option2;

if (keyboard.hasNextInt())
   { option1 = keyboard.nextInt(); }
else
   { /* input was not a valid 32-bits signed integer */ }

if (keyboard.hasNextInt())
   { option2 = keyboard.nextInt(); }
else
   { /* input was not a valid 32-bits signed integer */ }
 
Share this answer
 
Comments
Hussainomer 15-Feb-20 11:42am    
I got the first option part running but how do I modify or change the scanner so option2 becomes another option.
phil.o 15-Feb-20 11:55am    
Sorry, I don't understand your question.
Hussainomer 15-Feb-20 12:01pm    
Basically, the assignment is asking me to output two different options (option1 and option2), I got the option1 part working out but idk how to make the option2 part running, so it's different than options1.

Instructions:

Option 2:
The same as Option 1, except no more than one statement is output. The statement should only mention the lowest boundary number that applies.

Restriction: You may not use compound Boolean expressions! ( && or || )

Input

Input Expected Output
2
2 2 is less than 5

2
7 7 is less than 10

2
14 14 is less than 20

the first number is the options2, and second is the value
phil.o 15-Feb-20 12:06pm    
Sorry, I still don't get it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900