How do i connect between user input(Scanner) and if condition in Java2019 Community Moderator ElectionHow do I efficiently iterate over each entry in a Java Map?What is the difference between public, protected, package-private and private in Java?How do I call one constructor from another in Java?How do I read / convert an InputStream into a String in Java?How do I generate random integers within a specific range in Java?How to get an enum value from a string value in Java?How do I determine whether an array contains a particular value in Java?How do I declare and initialize an array in Java?How to split a string in JavaHow do I convert a String to an int in Java?
If I can solve Sudoku, can I solve the Travelling Salesman Problem (TSP)? If so, how?
Brexit - No Deal Rejection
PTIJ: Who should I vote for? (21st Knesset Edition)
What options are left, if Britain cannot decide?
A single argument pattern definition applies to multiple-argument patterns?
Does this sum go infinity?
Bach's Toccata and Fugue in D minor breaks the "no parallel octaves" rule?
Happy pi day, everyone!
Is it insecure to send a password in a `curl` command?
How to write cleanly even if my character uses expletive language?
Is honey really a supersaturated solution? Does heating to un-crystalize redissolve it or melt it?
Is there a place to find the pricing for things not mentioned in the PHB? (non-magical)
Why does overlay work only on the first tcolorbox?
What are substitutions for coconut in curry?
combinatorics floor summation
Do the common programs (for example: "ls", "cat") in Linux and BSD come from the same source code?
Examples of transfinite towers
Why is the President allowed to veto a cancellation of emergency powers?
Adventure Game (text based) in C++
Describing a chess game in a novel
Can I use USB data pins as power source
Official degrees of earth’s rotation per day
Have the tides ever turned twice on any open problem?
How to make healing in an exploration game interesting
How do i connect between user input(Scanner) and if condition in Java
2019 Community Moderator ElectionHow do I efficiently iterate over each entry in a Java Map?What is the difference between public, protected, package-private and private in Java?How do I call one constructor from another in Java?How do I read / convert an InputStream into a String in Java?How do I generate random integers within a specific range in Java?How to get an enum value from a string value in Java?How do I determine whether an array contains a particular value in Java?How do I declare and initialize an array in Java?How to split a string in JavaHow do I convert a String to an int in Java?
I know i have to use scanner and if condition but i don't know how to connect them
Example:
Are you willing to convert weight or height?
(user input= weight)
Will it be for imperial unit or metric unit?
(user input=metric)
Please provide the weight information in kg
(user input=75)
Your weight in kgs 75.00 is equal to 165.38 in pounds
java conditional java.util.scanner
add a comment |
I know i have to use scanner and if condition but i don't know how to connect them
Example:
Are you willing to convert weight or height?
(user input= weight)
Will it be for imperial unit or metric unit?
(user input=metric)
Please provide the weight information in kg
(user input=75)
Your weight in kgs 75.00 is equal to 165.38 in pounds
java conditional java.util.scanner
1
Use local variables.
– Dawood ibn Kareem
Mar 5 at 19:42
add a comment |
I know i have to use scanner and if condition but i don't know how to connect them
Example:
Are you willing to convert weight or height?
(user input= weight)
Will it be for imperial unit or metric unit?
(user input=metric)
Please provide the weight information in kg
(user input=75)
Your weight in kgs 75.00 is equal to 165.38 in pounds
java conditional java.util.scanner
I know i have to use scanner and if condition but i don't know how to connect them
Example:
Are you willing to convert weight or height?
(user input= weight)
Will it be for imperial unit or metric unit?
(user input=metric)
Please provide the weight information in kg
(user input=75)
Your weight in kgs 75.00 is equal to 165.38 in pounds
java conditional java.util.scanner
java conditional java.util.scanner
asked Mar 5 at 19:40
Selim SariSelim Sari
1
1
1
Use local variables.
– Dawood ibn Kareem
Mar 5 at 19:42
add a comment |
1
Use local variables.
– Dawood ibn Kareem
Mar 5 at 19:42
1
1
Use local variables.
– Dawood ibn Kareem
Mar 5 at 19:42
Use local variables.
– Dawood ibn Kareem
Mar 5 at 19:42
add a comment |
2 Answers
2
active
oldest
votes
Scanner scanner = new Scanner(System.in);
System.out.println("Enter an input");
String input= scanner.next();
You can start by putting the user input into a variable. Depending on the flow of your program, you might need to parse the input into either integer or float.
int integerinput = Integer.parseInt(input);
float floatInput = Float.parseFloat(input);
From there, you can start doing the comparison. You have to be careful when parsing information, if the user entered a word and you try to convert that into either integer or float, you will get an exception.
add a comment |
You should get user’s input just once:String answer = ask.nextLine();
And then inspect the input with if():if (answer.equals("heght"))
// heght conversion goes here
else
// weght conversion goes here
i used this and it gives me an error called cannot find symbol
– Selim Sari
Mar 5 at 23:36
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55010332%2fhow-do-i-connect-between-user-inputscanner-and-if-condition-in-java%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Scanner scanner = new Scanner(System.in);
System.out.println("Enter an input");
String input= scanner.next();
You can start by putting the user input into a variable. Depending on the flow of your program, you might need to parse the input into either integer or float.
int integerinput = Integer.parseInt(input);
float floatInput = Float.parseFloat(input);
From there, you can start doing the comparison. You have to be careful when parsing information, if the user entered a word and you try to convert that into either integer or float, you will get an exception.
add a comment |
Scanner scanner = new Scanner(System.in);
System.out.println("Enter an input");
String input= scanner.next();
You can start by putting the user input into a variable. Depending on the flow of your program, you might need to parse the input into either integer or float.
int integerinput = Integer.parseInt(input);
float floatInput = Float.parseFloat(input);
From there, you can start doing the comparison. You have to be careful when parsing information, if the user entered a word and you try to convert that into either integer or float, you will get an exception.
add a comment |
Scanner scanner = new Scanner(System.in);
System.out.println("Enter an input");
String input= scanner.next();
You can start by putting the user input into a variable. Depending on the flow of your program, you might need to parse the input into either integer or float.
int integerinput = Integer.parseInt(input);
float floatInput = Float.parseFloat(input);
From there, you can start doing the comparison. You have to be careful when parsing information, if the user entered a word and you try to convert that into either integer or float, you will get an exception.
Scanner scanner = new Scanner(System.in);
System.out.println("Enter an input");
String input= scanner.next();
You can start by putting the user input into a variable. Depending on the flow of your program, you might need to parse the input into either integer or float.
int integerinput = Integer.parseInt(input);
float floatInput = Float.parseFloat(input);
From there, you can start doing the comparison. You have to be careful when parsing information, if the user entered a word and you try to convert that into either integer or float, you will get an exception.
answered Mar 5 at 19:57
SemjeromeSemjerome
386
386
add a comment |
add a comment |
You should get user’s input just once:String answer = ask.nextLine();
And then inspect the input with if():if (answer.equals("heght"))
// heght conversion goes here
else
// weght conversion goes here
i used this and it gives me an error called cannot find symbol
– Selim Sari
Mar 5 at 23:36
add a comment |
You should get user’s input just once:String answer = ask.nextLine();
And then inspect the input with if():if (answer.equals("heght"))
// heght conversion goes here
else
// weght conversion goes here
i used this and it gives me an error called cannot find symbol
– Selim Sari
Mar 5 at 23:36
add a comment |
You should get user’s input just once:String answer = ask.nextLine();
And then inspect the input with if():if (answer.equals("heght"))
// heght conversion goes here
else
// weght conversion goes here
You should get user’s input just once:String answer = ask.nextLine();
And then inspect the input with if():if (answer.equals("heght"))
// heght conversion goes here
else
// weght conversion goes here
answered Mar 5 at 21:05
user11156080user11156080
1
1
i used this and it gives me an error called cannot find symbol
– Selim Sari
Mar 5 at 23:36
add a comment |
i used this and it gives me an error called cannot find symbol
– Selim Sari
Mar 5 at 23:36
i used this and it gives me an error called cannot find symbol
– Selim Sari
Mar 5 at 23:36
i used this and it gives me an error called cannot find symbol
– Selim Sari
Mar 5 at 23:36
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55010332%2fhow-do-i-connect-between-user-inputscanner-and-if-condition-in-java%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Use local variables.
– Dawood ibn Kareem
Mar 5 at 19:42