简体   繁体   中英

Scanner needs/is requesting input twice

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.

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

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();
        }
    }
}

Remove input.next(); in code and use. The input.next(); gets an input and has.next() get other input

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")) ?

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;
}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM