+ 1

Building a simple program with Scanner

Hi i'm learning java. I am building a program that asks the user to input a number from 1 to 10 and calculates the square and sqrt of that number, then asks the user if he wants to pick another number (very simple indeed). I cant get the second part right though, i want a simple Yes or No choice and so as to do that I created a string name = scanner.nextLine(), and an if loop so if string == "Yes" will do the same code as before with a different number. it doesnt catch my input with the string though

22nd Jan 2017, 10:43 PM
Alfred C
Alfred C - avatar
15 Respostas
+ 4
Well, here (SoloLearn), you have to input everything at once. I don't know if that's what you're doing. Also, seeing the code might help. Do you have it?
22nd Jan 2017, 10:49 PM
J.G.
J.G. - avatar
+ 4
Well, you could copy the program and paste it here, or you could create a hyperlink (copy the address of your code and paste it here) Or you can make your code public and I can look at it then.
23rd Jan 2017, 12:11 AM
J.G.
J.G. - avatar
+ 4
@Leonida17st That sounds like exactly what he was doing. It should be right though...
23rd Jan 2017, 12:14 AM
J.G.
J.G. - avatar
+ 4
Okay, so I see a few things here. One is that there should be NO SPACE between Scanner and (); It won't work if there is. So I took that out, and I think I had to add a } at the end, but I may have accidentally deleted one before. I then ran the code with no errors, but it only squared the first number and outputted the second question afterwards. I don't know what is wrong at this point, but I will keep trying!
23rd Jan 2017, 2:11 AM
J.G.
J.G. - avatar
+ 4
I tried using ints instead of strings. 1 for yes and 0 for no, and it worked just fine. I don't know what the problem is with comparing strings. I'll look into it.
24th Jan 2017, 2:53 AM
J.G.
J.G. - avatar
+ 4
@Alfred C Any more luck with it?
25th Jan 2017, 12:20 AM
J.G.
J.G. - avatar
+ 3
For booleans, it shouldn't be hard. Just have the user input be a boolean, and if true, then do something, and if not, then do something else.
25th Jan 2017, 12:33 AM
J.G.
J.G. - avatar
0
any help?
22nd Jan 2017, 10:43 PM
Alfred C
Alfred C - avatar
0
yep sorry, basically the question is how do I create an Yes or No option in java, I tried this: System.out.println("Continue?"); String answer = input.nextLine(); if (answer == "Yes") {do this code} else if (answer =="No") {Do that} But the only thing that outputs is Continue? and wont allow me to input any answer (doesnt give any error tho). is there a way to link codes from the code playground? writing it down here would take a lot
22nd Jan 2017, 11:11 PM
Alfred C
Alfred C - avatar
0
Select case
22nd Jan 2017, 11:41 PM
Carsten
0
In c when you take an input of a string, it saves in the string the enter at the end of the input. I dont think that's the problem but it wont hurt to try. I suggest for you to change the input to an int and check for 1 or 0. Hope I helped.
22nd Jan 2017, 11:45 PM
shay lempert
shay lempert - avatar
0
import java.util.Scanner; class MyClass { public static void main(String[ ] args) { String str; Scanner myVar = new Scanner(System.in); str=myVar.nextLine(); if(str=="yes") { System.out.println("yes"); } else{ System.out.println("No"); } } } so i created String and get the value of input and then i compared that string to some world in your example -> "yes" so just change this if ,else statment with thing you want to do
23rd Jan 2017, 12:12 AM
Leonida17st
Leonida17st - avatar
0
I wrote it here and made it public, with name "Yes / No"
23rd Jan 2017, 1:00 AM
Alfred C
Alfred C - avatar
0
I didnt figure it out how yet, I was told anyways to use boolean instead of strings for the purpose, but I don't know how to use them this way
25th Jan 2017, 12:25 AM
Alfred C
Alfred C - avatar
0
I tried it, but i could only make it work by inputting "True" instead of "Y" to make the loop again, i don't know if I explained myself.. maybe i should use a char input?
25th Jan 2017, 12:49 AM
Alfred C
Alfred C - avatar