【问题标题】:System.out.println print at the same timeSystem.out.println 同时打印
【发布时间】:2016-01-21 08:07:35
【问题描述】:

我正在构建一个简单的程序,该程序使用 Ganymed for SSH2 库对 linux 服务器执行单个命令。这是我的代码...

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


    }



}

不知为何……

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

同时打印,我不知道为什么!当使用 println 和使用 print 时,它还会在完全不同的行而不是紧挨着它的旁边获取用户输入。

我希望他们先询问用户名,然后询问密码,将用户输入放在输出旁边。但是,当我询问主机名和端口号时,它们会单独/按顺序打印,就像我想要的那样。我做错了什么?

【问题讨论】:

    标签: java eclipse ssh


    【解决方案1】:

    在您执行port = reader.nextInt(); 之后,您需要放置一个reader.nextLine() 来读取具有int 的行的其余部分(和换行符)。

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

    reader.nextInt() 不会读取换行符或数字后的任何内容。

    添加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();
    

    【讨论】:

      【解决方案2】:

      您的问题是您多次使用同一个 Scanner 对象来读取一行。它跳过了代码:

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

      因为对象已经为您正在调用的方法设置了值。

      解决方法:每次使用时重新实例化对象:

      reader = new Scanner(System.in);
      

      【讨论】:

      • 我认为因为它是一个按顺序执行代码的单线程程序(如果这有意义的话……我是编程新手)我可以重用扫描仪。上一个答案有效
      • 我也太早关闭了扫描仪,我仍然需要它来检索命令输入
      猜你喜欢
      • 2022-08-18
      • 1970-01-01
      • 1970-01-01
      • 2015-05-04
      • 2010-12-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-20
      相关资源
      最近更新 更多