LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-28-2014, 11:41 AM   #1
trist007
Senior Member
 
Registered: May 2008
Distribution: Slackware
Posts: 1,052

Rep: Reputation: 70
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.
 
Old 03-28-2014, 08:59 PM   #2
joe_2000
Senior Member
 
Registered: Jul 2012
Location: Aachen, Germany
Distribution: Void, Debian
Posts: 1,016

Rep: Reputation: 308Reputation: 308Reputation: 308Reputation: 308
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
 
Old 03-28-2014, 09:09 PM   #3
trist007
Senior Member
 
Registered: May 2008
Distribution: Slackware
Posts: 1,052

Original Poster
Rep: Reputation: 70
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.

Last edited by trist007; 03-28-2014 at 09:11 PM.
 
Old 03-28-2014, 09:12 PM   #4
pliqui
Member
 
Registered: Feb 2007
Location: Caracas, Venezuela
Distribution: Debian x64
Posts: 156

Rep: Reputation: 17
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 View Post
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Use Java scanner delimiters separate artist year title etc? (Java)? mitchell7man Programming 9 04-13-2010 07:24 PM
Java: what's the difference between an inner class and a static inner class? irey Programming 6 01-28-2009 03:34 AM
why is Scanner class of java not working on fedora 7 malayletsrock Programming 13 07-16-2008 07:38 AM
Java Question on using your own class k1ll3r_x Programming 2 01-22-2005 10:11 AM
Compile Java - .class, .java, .jar ? woranl Programming 2 11-09-2004 10:12 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:16 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration