Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading String in java

Tags:

java

I've been working on this code for school to create a course, users, students, and teachers but I ran into a snag while creating a course.

What I want to do is (in the terminal) read an input stream such as "CSC 110 Into to java" and enter all of it into a string. I am using Scanner for input. and have tried hasNext() and nextLine() besides next(). I thought about writing a function to use a char array get the entire string and then convert the char array into a string.

First I wanted to know if there was an easier solution?

Edit: Sorry I wasn't clear. Without going into all of my code.

current Code:

        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

Current 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)

When I use nextLine()

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

Intended result:

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

I understand that when I have three strings "CSC 110 Intro" it get to the input for nextInt() but I don't understand why nextLine() is writing my println's like this.

like image 535
Jeremy Avatar asked Apr 04 '11 04:04

Jeremy


2 Answers

Well, you haven't told us exactly what the error is, so I'll just give some sample Scanner code and let you work with that:

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

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

That should work; Scanner is one of the simplest classes to use. Perhaps you accidentally used

new Scanner(); //no input source!

rather than

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

EDIT:

I've surmised from your output that you are calling

keyboard.nextInt();

and later

keyboard.nextLine();

This is what is messing up your program. The nextInt() method leaves the "\n" endline symbol and is picked up immediately by nextLine(), skipping over the next input. What you want to do is use nextLine for everything, and parse it later:

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

This is by far the easiest way to avoid problems--don't mix your "next" methods. Use only nextLine() and then parse ints/separate words afterwards.

like image 144
donnyton Avatar answered Oct 24 '22 07:10

donnyton


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

Would that work for you?

like image 40
Cory G. Avatar answered Oct 24 '22 06:10

Cory G.