LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   A question about Java's Scanner class... (https://www.linuxquestions.org/questions/programming-9/a-question-about-javas-scanner-class-4175499807/)

trist007 03-28-2014 11:41 AM

A question about Java's Scanner class...
 
Code:

import java.util.Scanner;

public class ForgetfulMachine
{
        public static void main( String [] args )
        {
                Scanner io = new Scanner(System.in);

                System.out.println("Enter comments");
                String comment = io.nextLine();

                System.out.println("The comment you entered is:" + comment);

        }
}

It appears that the line where I instantiate a Scanner object, it blocks.

Code:

rgonzale@kronos:~1012 !gcj
gcj --main=ForgetfulMachine ForgetfulMachine.java
rgonzale@kronos:~1013 ./a.out
 
Enter comments
Wow
The comment you entered is:
rgonzale@kronos:~1014 ./a.out
Wow
Enter comments

The comment you entered is:Wow

I'm wondering if there is something with the input stream in relation to Linux command line that is causing this.

joe_2000 03-28-2014 08:59 PM

I am not sure what you are doing with gcj, and what a.out is supposed to be. But for me the below works flawlessly (using your code):

Code:

user@host:~/Test$ javac ForgetfulMachine.java
user@host:~/Test$ java ForgetfulMachine
Enter comments
wow
The comment you entered is:wow


trist007 03-28-2014 09:09 PM

Interesting thank you.

gcj is the gcc plugin for compiling java.
The a.out file is a pre-compiled binary.

rgonzale@kronos:~1010 file a.out
a.out: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.18, not stripped

Yes it works like a charm with javac. Thanks a lot.

pliqui 03-28-2014 09:12 PM

As joe said, worked with your code. Using eclipse. Just added io.close(); to close the scanner resource
http://i58.tinypic.com/fyhidl.png
Quote:

Originally Posted by joe_2000 (Post 5143104)
I am not sure what you are doing with gcj, and what a.out is supposed to be. But for me the below works flawlessly (using your code):

Code:

user@host:~/Test$ javac ForgetfulMachine.java
user@host:~/Test$ java ForgetfulMachine
Enter comments
wow
The comment you entered is:wow




All times are GMT -5. The time now is 05:56 PM.