Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / July 2007

Tip: Looking for answers? Try searching our database.

java newbie - basic io question

Thread view: 
Thomas - 03 Jul 2007 19:01 GMT
Hello I 'm new to java and I want just to ask about the shortest possible
way to read user input from console. I found this :

 char tmp;
 Integer liczba = new Integer (0);
 StringBuffer buffer;
 buffer=new StringBuffer();

while((tmp = (char)System.in.read())!='\n')
  buffer=buffer.append(tmp);
 liczba=Integer.parseInt(buffer.toString().trim());

Since in c++ it does take significantly less code to implement.
Manish Pandit - 03 Jul 2007 19:14 GMT
> Hello I 'm new to java and I want just to ask about the shortest possible
> way to read user input from console. I found this :
[quoted text clipped - 9 lines]
>
> Since in c++ it does take significantly less code to implement.

A shorter (in terms of lines of code) version would be :

BufferedReader reader = new BufferedReader(new
InputStreamReader(System.in));
String string = reader.readLine();

In the above fragment, 'string' has the keyboard input, which you can
trim/chop/parseInt..etc.

-cheers,
Manish
Roedy Green - 04 Jul 2007 01:46 GMT
>  char tmp;
>  Integer liczba = new Integer (0);
[quoted text clipped - 6 lines]
>
>Since in c++ it does take significantly less code to implement.

ssee http://mindprod.com/applets/fileio.html
and get it to generate your some code  readLine will get you a whole
line at a time.  You can trim that.  It effectively handles the
StringBuilder stuff for you.
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
Hal Rosser - 04 Jul 2007 01:48 GMT
> Hello I 'm new to java and I want just to ask about the shortest possible
> way to read user input from console.

Look in he API at the new Scanner class.
Arne Vajhøj - 04 Jul 2007 02:30 GMT
> Hello I 'm new to java and I want just to ask about the shortest possible
> way to read user input from console. I found this :
[quoted text clipped - 7 lines]
>    buffer=buffer.append(tmp);
>   liczba=Integer.parseInt(buffer.toString().trim());

Almost any Java version:

import java.io.*;

public class S14 {
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
        System.out.print("Enter integer: ");
        int iv = Integer.parseInt(br.readLine());
        System.out.println(iv);
        System.out.print("Enter decimal: ");
        double xv = Double.parseDouble(br.readLine());
        System.out.println(xv);
        System.out.print("Enter string: ");
        String sv = br.readLine();
        System.out.println(sv);
    }
}

Java 1.5 and newer:

import java.io.*;
import java.util.*;

public class S15 {
    public static void main(String[] args) throws Exception {
        Scanner scn = new Scanner(System.in);
        System.out.print("Enter integer: ");
        int iv = scn.nextInt();
        System.out.println(iv);
        System.out.print("Enter decimal: ");
        double xv = scn.nextDouble();
        System.out.println(xv);
        System.out.print("Enter string: ");
        String sv = scn.next();
        System.out.println(sv);
    }
}

Arne
Andrew Thompson - 04 Jul 2007 03:12 GMT
...
>Since in c++ it does take significantly less code to implement.

Boo hoo.  How many lines of C++ does it take to write
a minimal HTML/web browser?  In Java it takes less
than 60 lines of code for a self contained implementation.

And what does that mean?  Java is well optimised
for developing GUI's, and HTML rendering.  Java is
not well optimised for reading from the command line
(current thread examples notwithstanding).

What else does it mean?  Approximately nothing.

Signature

Andrew Thompson
http://www.athompson.info/andrew/



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.