4 Replies - 380 Views - Last Post: 11 March 2012 - 01:52 PM Rate Topic: -----

#1 bhavin5  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 61
  • Joined: 29-November 11

.nextLine(); is not working for me... trying to read in a line of text

Posted 11 March 2012 - 01:13 PM

hello... i am trying to read in the name of a module it has spaces in it... when I run my file it skips it and goes to my switch statement...

here is my code...
private static void addModuls() throws Exception {
        SimpleDataSource.init("database.properties");

        Connection con = SimpleDataSource.getConnection();
        try {
            Scanner in = new Scanner(System.in);
            System.out.println("Enter module id");
            String moduleId = in.next();

                       
            System.out.println("Enter credits");
            String credits = in.next();
            
            System.out.println("Enter module name");
            String moduleName = in.nextLine();

            String query = "INSERT INTO ModuleT VALUES (?,?,?)";
            PreparedStatement pst = (PreparedStatement) con.prepareStatement(query);

            pst.setString(1, moduleId);
            pst.setString(2, moduleName);
            pst.setString(3, credits);

            pst.executeUpdate();

        } finally {
            con.close();
        }
    }



this is the while loop it goes to when I run the file... without reading in the module Name....
while (true) {
            System.out.println("Choose a number corresponding to the statement");

            System.out.println("1  : Add students to the database");
            System.out.println("2  : Show all students in the database");
            System.out.println("3  : Remove students from the database");
            System.out.println("4  : Add modules to the database");
            System.out.println("5  : Show all modules in the database");
            System.out.println("6  : Remove modules from the database");
            System.out.println("7  : Enroll students onto a module");
            System.out.println("8  : Remove students from a module");
            System.out.println("9  : Show registrations");
            System.out.println("10 : Quit application");

            Scanner in = new Scanner(System.in);
            int x = in.nextInt();
            cases(x);
        }
    }


Is This A Good Question/Topic? 0
  • +

Replies To: .nextLine(); is not working for me... trying to read in a line of text

#2 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: .nextLine(); is not working for me... trying to read in a line of text

Posted 11 March 2012 - 01:17 PM

What switch statement?
Was This Post Helpful? 0
  • +
  • -

#3 bhavin5  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 61
  • Joined: 29-November 11

Re: .nextLine(); is not working for me... trying to read in a line of text

Posted 11 March 2012 - 01:19 PM

View PostRyano121, on 11 March 2012 - 01:17 PM, said:

What switch statement?

sorry... i have put the switch statement in a method.called cases();
Was This Post Helpful? 0
  • +
  • -

#4 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: .nextLine(); is not working for me... trying to read in a line of text

Posted 11 March 2012 - 01:24 PM

I'm a bit confused. You say that you are reading in the module name, which appears in the first snippet, yet there is not call to cases() in it.

There is however a call in the second snippet - after the user selects an option.

Could you clarify your problem a little more?
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,858
  • Joined: 06-March 08

Re: .nextLine(); is not working for me... trying to read in a line of text

Posted 11 March 2012 - 01:52 PM

Well documented behavior of the Scanner :)

If you want to read the next line following a nextInt() or nextDouble() or nextFloat() you have to read the end of line after the basic data type.

When you do:

int x = scanner.nextInt();
int y = scanner.nextInt();

the user can enter
10
20
or 10 20 on the same line. So in case the program will do a nextInt() next, nextInt() does not read the end of line
So to read from the user:

123
This is a module name

you'll have to:

int x = scanner.nextInt(); // read 123
scanner.nextLine(); // eat the end of line after the 123
String module = scanner.nextLine(); // read the next line

Happy coding

This post has been edited by pbl: 11 March 2012 - 01:53 PM
Reason for edit:: Typo

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1