简体   繁体   English

System.out.println同时打印

[英]System.out.println print at the same time

I'm building a simple program that executes a single command to a linux server using Ganymed for SSH2 library. 我正在构建一个简单的程序,该程序使用Ganymed for SSH2库对Linux服务器执行单个命令。 Here is my code... 这是我的代码...

public class Main {

static Scanner reader = new Scanner(System.in);


public static void main(String[] args) throws IOException, InterruptedException{        


    String hostname; // = "192.168.1.241";
    int port; // = 22
    String username; // = "root";
    String password; 
    String cmd; // = "echo 'Hello World'";

    //ask user for hostname/IP
    System.out.print("Enter hostname or IP: ");
    hostname = reader.nextLine();


    //ask user for port #
    System.out.print("Enter port number: ");
    port = reader.nextInt();        

        //create connection
        Connection connect = new Connection(hostname, port);

    //ask user for username
    System.out.println("Enter Username (best to use 'root'): ");
    username = reader.nextLine();

    //ask user for password
    System.out.println("Enter Password: ");
    password = reader.nextLine();   


    System.out.println("Attempting to log in...");

    reader.close(); 

        //establish connection
        connect.connect();

        //login using password
        connect.authenticateWithPassword(username, password);

        Thread.sleep(1000);
        if (Thread.interrupted())  // Clears interrupted status!
              throw new InterruptedException();

        Boolean didConnect = connect.isAuthenticationComplete();
        if (didConnect==false){
            throw new IOException("Authentication Unsucessful \nCheck hostname and port number, then try again.");
        }

            //open session
            Session session = connect.openSession();
            System.out.println("Opened session!");              




        System.out.println("Enter a command to execute: ");
        cmd = reader.nextLine();

        //execute command
        session.execCommand(cmd);


        //get output
        InputStream stdout = new StreamGobbler(session.getStdout());

        BufferedReader br = new BufferedReader(new InputStreamReader(stdout));

        String line = br.readLine();
        if (line!=null){
            System.out.println("Command sent succesfully!" + "\nOutput: " + "\n" + "\n" + line);
        }else{
            System.out.println("Command sent succesfully!" + "\nNo Output");
        }

        //close buffered reader
        br.close();

        //close session
        session.close();

        //close connection
        connect.close();


    }



}

For some reason... 由于某些原因...

//ask user for username
    System.out.println("Enter Username (best to use 'root'): ");
    username = reader.nextLine();

    //ask user for password
    System.out.println("Enter Password: ");
    password = reader.nextLine();   

print at the same time and I don't know why! 同时打印,我也不知道为什么! It also gets the user input on a completely different line instead of right next to it when using print ln and on the same line when using print . 使用打印LN和在同一行上使用打印时,当它也得到了完全不同的路线,而不是用户输入正确的旁边。

I want them to ask for the username first and then for password, putting the user input next to the output. 我希望他们先要求用户名,然后要求密码,将用户输入放在输出旁边。 However, when I ask for the hostname and port number they print individually/in order, like I want it to. 但是,当我询问主机名和端口号时,它们会按我的意愿逐个/按顺序打印。 What am I doing wrong? 我究竟做错了什么?

After you do port = reader.nextInt(); 完成后, port = reader.nextInt(); , you need to place a reader.nextLine() to read the rest of the line(and newline character) which has the int. ,您需要放置reader.nextLine()来读取具有int的其余行(和换行符)。

//ask user for port #
System.out.print("Enter port number: ");
port = reader.nextInt();        

reader.nextInt() doesn't read newline or anything after the number. reader.nextInt()不读取换行符或数字之后的任何内容。

Add reader.nextLine() then the rest of the code should follow: 添加reader.nextLine()然后其余代码应遵循:

reader.nextLine();

//ask user for username
System.out.println("Enter Username (best to use 'root'): ");
username = reader.nextLine();

//ask user for password
System.out.println("Enter Password: ");
password = reader.nextLine();

Your problem is that you're using the same Scanner object multiple times to read a line. 您的问题是您多次使用同一Scanner对象读取一行。 It skips the code: 它跳过代码:

 //ask user for username
System.out.println("Enter Username (best to use 'root'): ");
username = reader.nextLine();

//ask user for password
System.out.println("Enter Password: ");
password = reader.nextLine();

because the object already has a value for the method you're calling. 因为该对象已经具有您正在调用的方法的值。

Solution: re-instantiate the object every time you use it: 解决方案:每次使用时都要重新实例化该对象:

reader = new Scanner(System.in);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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