在java中读取字符串

时间:2011-04-04 04:55:53

标签: java

我一直在为学校编写这个代码来创建一个课程,用户,学生和老师,但我在创建课程时遇到了麻烦。

我想要做的是(在终端中)读取输入流,例如“CSC 110 Into to java”,并将其全部输入到字符串中。我正在使用Scanner进行输入。除了next()之外,我还尝试过hasNext()和nextLine()。我想写一个函数来使用char数组获取整个字符串,然后将char数组转换为字符串。

首先,我想知道是否有更简单的解决方案?

编辑:对不起,我不清楚。没有进入我的所有代码。

当前代码:

        System.out.print("Enter the Course Title: ");
        title = keyboard.nextLine();
        System.out.print("Enter a Brief Course Description: ");
        desc = keyboard.nextLine();

        if (t != null)
        {   
            do
            {
                System.out.println("\nPick a teacher\n");

                for (int j = 0; j < t.size(); j++)
                {
                    nTeacher = t.get(j);
                    s += "\t(" + (j+1) + ") " + nTeacher + "\n";
                }

                System.out.print(s + "\nEnter the Teacher ID: ");
                int tId = keyboard.nextInt(); // My error

当前错误:

Enter the Course Title: CSC 110 Into to java

Enter a Breif Course Description: 
Pick a teacher

Enter the Teacher ID: 
Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Scanner.java:857)
    at java.util.Scanner.next(Scanner.java:1478)
    at java.util.Scanner.nextInt(Scanner.java:2108)
    at java.util.Scanner.nextInt(Scanner.java:2067)
    at Prototype.classCreator(Prototype.java:530)
    at Prototype.mainMenu(Prototype.java:181)
    at Prototype.login(Prototype.java:121)
    at Prototype.main(Prototype.java:48)

当我使用nextLine()

Create Class
Enter the courseID: 27222
Enter the Course Title: Enter a Brief Course Description: 

预期结果:

Create Class
Enter the courseID: 27299
Enter the Course Title: CSC 110 Into to Java
Enter a Brief Course Description: Introduction to programming
Pick a teacher
(1) ID: 1111    Name: Bob
Enter the Teacher ID: 1
Class Created

据我所知,当我有三个字符串“CSC 110 Intro”时,它会进入nextInt()的输入,但我不明白为什么nextLine()正在写这样的println。

4 个答案:

答案 0 :(得分:6)

好吧,你还没有准确地告诉我们错误是什么,所以我只是给出一些示例扫描程序代码并让你使用它:

//create the Scanner
Scanner terminalInput = new Scanner(System.in);

//read input
String s = terminalInput.nextLine();

那应该有用;扫描仪是最简单的类之一。也许你不小心使用了

new Scanner(); //no input source!

而不是

new Scanner(System.in); //links to terminal

修改

我猜测你输出的是

keyboard.nextInt();

以后

keyboard.nextLine();

这就是弄乱你的程序。 nextInt()方法留下“\ n”结束符号,并由nextLine()立即拾取,跳过下一个输入。你想要做的是使用nextLine来处理所有事情,并在以后解析它:

String nextIntString = keyboard.nextLine(); //get the number as a single line
int nextInt = Integer.parseInt(nextIntString); //convert the string to an int

这是迄今为止避免问题的最简单方法 - 不要混淆“下一步”方法。仅使用nextLine()然后解析整数/单独的单词。

答案 1 :(得分:4)

Scanner scan = new Scanner(System.in);
String str_input = scan.nextLine();

这会对你有用吗?

答案 2 :(得分:2)

你可以使用几个人提到的new Scanner(System.in),或者你也可以试试这个:

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

String s = br.readLine();

答案 3 :(得分:0)

BufferedReader bufferedReader = new BufferedReader(new FileReader(fileName));

这就是你如何从文件btw中读取它。

我认为BufferedReader比扫描器更好,因为它完成了它所说的,它缓冲。

但扫描仪也可以。

尝试创建配置文件,而不是从命令行读取它。

Properties configFile = new Properties();
configFile.load(new FileInputStream("conf.properties"));

myString = configFile.getProperty("MY_STRING");

配置文件将命名为conf.properties,然后它将包含在其中:

MY_STRING = blahblahbah