Reputation: 333
I'm just writing a small program that receives input from the user then prints it back to them. However, when I run the program it asks for input twice (it prints the initial statement, then once you type and press enter, nothing happens, but if you do it again it works and prints.) The top value apparently has no weight or meaning, as it does not show up, but the second value does get printed well.
package kek;
import java.util.Scanner;
public class Kek {
public static void main (String[] args){
Scanner input = new Scanner(System.in);
System.out.println("What is kek? (Top, bottom, etc.)");
String s1 = input.next();
if (input.hasNext("kek")) {
System.out.println("No.");
System.exit(0);
} else {
System.out.println(s1 + "kek");
input.close();
}
}
}
Here's the console:
What is kek? (Top, bottom, etc.)
top
top
topkek
and
What is kek? (Top, bottom, etc.)
kek
kek
No.
I'm using eclipse kepler.
Upvotes: 1
Views: 5995
Reputation: 1
@Julien. It's like 5 years late but I fixed your code.
import java.util.Scanner;
public class Methods2 {
static Scanner userInput = new Scanner(System.in);
static String answer;
static int yes1no2;
static int loopStage = 1;
public static void main(String[] args)
{
System.out.println("Welcome to the bank.\nWould you like to open an account?");
answer = userInput.nextLine();
while (loopStage == 1)
{
if (yesOrNo() == 1)
{
loopStage = 2;
System.out.println("Great.");
}
else if (yesOrNo() == 2)
{
loopStage = 2;
System.out.println("Ok, please come again.");
}
}
}
public static int yesOrNo()
{
if (answer.equalsIgnoreCase("yes")) {
yes1no2 = 1;
System.out.println(yes1no2);
}
else if (answer.equalsIgnoreCase("no")) {
System.out.println(yes1no2);
yes1no2 = 2;
} else {
yes1no2 = 3;
System.out.println("Please say 'Yes' or 'No'.");
answer = userInput.nextLine();
}
return yes1no2;
}
}
so.. answer = userInput.nextLine(); has been the real deal here again. see how I put: System.out.println(yes1no2); in the section where you ask the system for yes1no2? doing so will show what actually happens with your input when going through the Scanner.
what you will find is that if you do that on your code it will show that: Yes prints.. 1
No prints.. 0 (then it will ask for a new value which you say No again) No prints.. 2
putting: answer = userInput.nextLine(); way above will make the input value not leave the buffer before it's checked again. so what is happening here? -the programm asks the user a value -checks if the if statement is true? process code inside statement : move on to next line and.. (here's the catchy part) it will drop the value inside the Scanner buffer because it was used to check if Yes == true (Scanner value drops back to 0) [p.s. don't try to change the No value to 0 because Scanner value != 0, Scanner value == not there at all] then since Scanner = 0 and 0 is not a value inside the bracket it will ask you again. you enter No again and it will then register No = 2 I also had to fix the else statement (yes1no2) because it was looping endlessly: basically just asking for a new input to put inside the buffer did the job.
putting the Scanner buffer outside the brackets it will check the whole bracket to see if anything connects to it before dropping the value. try it out yourself
I am sorry for the bad formating it's like my first post and I actually wanted to look this up because I've only been doing Scanners since yesterday so if I am wrong here please correct me I am glad to learn more
Upvotes: 0
Reputation: 1
I'm having the exact same problem, although it only requires input twice when I type in 'no' or 'No'... I'm not sure why only the 'no' area is affected.
import java.util.Scanner;
public class Methods2 {
static Scanner userInput = new Scanner(System.in);
static String answer;
static int yes1no2;
static int loopStage = 1;
public static void main(String[] args)
{
System.out.println("Welcome to the bank.\nWould you like to open an account?");
while (loopStage == 1)
{
if (yesOrNo() == 1)
{
loopStage = 2;
System.out.println("Great.");
}
else if (yesOrNo() == 2)
{
System.out.println("Ok, please come again.");
}
}
}
public static int yesOrNo()
{
answer = userInput.nextLine();
if (answer.equalsIgnoreCase("yes"))
{
yes1no2 = 1;
}
else if (answer.equalsIgnoreCase("no"))
{
yes1no2 = 2;
}
else
{
yes1no2 = 3;
System.out.println("Please say 'Yes' or 'No'.");
}
return yes1no2;
}
}
Upvotes: 0
Reputation: 14276
You're asking for the input twice with next()
and hasNext()
. Just do the following instead:
package kek;
import java.util.Scanner;
public class Kek {
public static void main (String[] args){
Scanner input = new Scanner(System.in);
System.out.println("What is kek? (Top, bottom, etc.)");
String s1 = input.next();
//just compare strings using the .equals() method instead
//of using the hasNext() which will ask for another input
if ("kek".equals(s1)) {
System.out.println("No.");
System.exit(0);
}
else{
System.out.println(s1 + "kek");
input.close();
}
}
}
Upvotes: 1
Reputation: 5471
Not sure but is this what you meant ??
public static void main(String[] args) {
System.out.println("What is kek? (Top, bottom, etc.)");
Scanner input = new Scanner(System.in);
String string = input.nextLine();
if(string.equals("kek")){
System.out.println("No.");
System.exit(0);
}else{
System.out.println(string + "kek");
input.close();
}
}
If not then you need to be more specific what you are trying to achieve. As from what you have written now its kind of unclear what you really want
Upvotes: 1
Reputation: 77177
You're first reading in a value into s1
, and then telling the Scanner
to look for another line with the value "kek"
somewhere in it. Perhaps you meant to say something like if(input.equals("kek"))
?
Upvotes: 0
Reputation: 997
Remove input.next();
in code and use. The input.next();
gets an input and has.next()
get other input
Upvotes: 0