简体   繁体   中英

Have to Enter Input Twice for Scanner to Read it

So in this code, under the class "Run" in the method "run", the Scanner seems to not want to take in input from the fist attempt, only on the second line does it take input. I say second line because I enter input then press return twice, and enter input on the THIRD line, it reads the second line, which in this case would be nothing.

I have tried BufferedReader with the same result, so I believe I am doing something idiotic and overlooking something.

import java.util.Scanner;
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;

import java.io.*;

class Global {
    public static int stop = -1;
}

public class DataSort {

    public static void main(String[] args){

        Timer timer = new Timer();
        Direct swit = new Direct();
        Run mprog = new Run();
        Help hlp = new Help();

        String newline = System.getProperty("line.separator");

        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        Scanner console = new Scanner(System.in);

        System.out.println(newline);
        System.out.println("Welcome to Data Sort! This Program is designed to sort information about targets discoverd by UAV and place the data in a table." + newline);
        System.out.print("For help, press any key. To continue, please wait. ");
        timer.schedule(swit, 3000);

        try {
            Global.stop = in.read();
        } 

        catch(IOException e) {
            e.printStackTrace();
        }

        try {
            in.close();
        }

        catch(IOException e) {
            e.printStackTrace();
        }
    }
}

class Direct extends TimerTask {

    public void run() {

        Run mprog = new Run();
        Help hlp = new Help();

        if(Global.stop != -1){
            System.out.println("Help");
            hlp.run();
        }

        if(Global.stop == -1) {
            System.out.println("Main");
            mprog.run();
        }
    }
}


class Help {

    public static void run() {
        String newline = System.getProperty("line.separator");

        System.out.print(newline);
        System.out.println("Entering Help Mode!" + newline);

        System.out.println("Entered Help Class");
        //String help = console.nextLine();
    }
}

class Run {

    public static void run() {

        /*EnterAll eall = new EnterAll();
        EnterCoords ecoords = new EnterCoords();
        EnterRelation erelat = new EnterRelation();
        EnterColor ecolor = new EnterColor();
        EnterShape eshape = new EnterShape();
        Coordinates coords = new Coordinates();
        Relation relat = new Relation();
        Color color = new Color();
        Shape shape = new Shape();
        List list = new List();
        Save save = new Save();
        SaveAs saveas = new SaveAs();*/

        String newline = System.getProperty("line.separator");
        Scanner console = new Scanner(System.in);

        System.out.print(newline);
        System.out.println("Initializing Main Program." + newline);
        System.out.println("************************** MAIN MENU *************************" + newline);
        System.out.println("Enter Coords \t Enter Relat \t Enter Color \t Enter Shape"+newline);
        System.out.println("Coordinates \t Relation \t Color \t \t Shape" + newline);
        System.out.println("Help \t \t List \t \t Save \t \t Save As" + newline);
        System.out.println("**************************************************************" + newline);

        System.out.print("Enter your selection or type All to enter lines consecutively: ");
        String raw = console.nextLine();

        System.out.println(raw);

        String select = errorCheck(raw);

        if (select.equals("All")){
        }

        if (select.equals("Enter Coords")){
        }

        if (select.equals("Enter Relat")){
        }

        if (select.equals("Enter Color")){
        }

        if (select.equals("Enter Shape")){
        }

        if (select.equals("Coordinates")){
        }

        if (select.equals("Relation")){
        }

        if (select.equals("Color")){
        }

        if (select.equals("Shape")){
        }

        if (select.equals("Help")){
        }

        if (select.equals("List")){
        }

        if (select.equals("Save")){
        }

        if (select.equals("Save As")){
        }
    }

    private static String errorCheck(String raw) {

        String select = raw;
        return select;
    }
}

Your problem lies in

public class DataSort {...... Global.stop = in.read(); ......} 

Because in.read is for reading Integer input. It does not read End of Line character. Which is why it becomes clueless after you enter selection string and hit enter.

Regards, Ravi

@Sean, here is your solution

Comment out below lines

public class DataSort
{
.
.
.
//BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
//Scanner console = new Scanner(System.in);
.
.
.
//Global.stop = in.read();
.
.
}

For Global.stop = in.read(), read from the same input read buffer(may be in the same class or somewhere else) and parse the string as you want. Don't create another input read buffer.

Regards, Ravi

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