从控制台Java读取带有换行符的字符串 [英] Reading a string with new lines from console java

查看:99
本文介绍了从控制台Java读取带有换行符的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我想读取此字符串(从控制台而不是文件):

I want to read this string (from console not file) for example:

one two three
four five six
seven eight nine

因此,我想每行读取一次并将每行放入数组中. 我该怎么读?因为如果使用扫描仪,我只能读取一行或一个单词(下一行或下一行).

So I want to read it per line and put every line in an array. How can I read it? Because if I use scanner, I can only read one line or one word (nextline or next).

例如,我的意思是阅读:one two trhee \n four five six \n seven eight nine...

what I mean is to read for example : one two trhee \n four five six \n seven eight nine...

推荐答案

您应该自己做! 有一个类似的例子:

You should do by yourself! There is a similer example:

public class ReadString {

   public static void main (String[] args) {

      //  prompt the user to enter their name
      System.out.print("Enter your name: ");

      //  open up standard input
      BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

      String userName = null;

      //  read the username from the command-line; need to use try/catch with the
      //  readLine() method
      try {
         userName = br.readLine();
      } catch (IOException ioe) {
         System.out.println("IO error trying to read your name!");
         System.exit(1);
      }

      System.out.println("Thanks for the name, " + userName);

   }

}  // end of ReadString class

这篇关于从控制台Java读取带有换行符的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆