• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

the get() line...

 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have learned some of the basics from the Dummies book and have used the program you download called DummiesIO.java to get input from the keyboard, but how do you get it without using a downloaded program?

Doesn't Java have an internal get() or something like that I can use?
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello "jbrdbr111"-

Welcome to JavaRanch.

On your way in you may have missed that we have a policy on screen names here at JavaRanch. Basically, it must consist of a first name, a space, and a last name, and not be obviously fictitious. Since yours does not conform with it, please take a moment to change it, which you can do right here.

As to your question, no, Java does not have facilities for that built into it. For a more complete solution, have a look at the jLine project on SourceForge.
 
Ranch Hand
Posts: 490
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are tons of IO options available. The most straightforward is probably the Scanner class(Java 1.5.x). More useful, IMO, are the other IO classes from the java.io package. You can make extensive use of wrapper classes, which can be confusing to people starting out.

For basic IO, I use BufferedReader read = new BufferedReader(new InputStreamReader(System.in); Which is exactly what DummiesIO uses. Look at the source, it is basically an IO and parse wrapper.

From here you can read all input as a String with str = read.readLine(); Then do what you need to do with the data.

But that is the tip of the iceberg. You might want to start with Scanner and then look at other IO later.

Scanner:

http://java.sun.com/j2se/1.5.0/docs/api/java/util/Scanner.html

IO:
http://java.sun.com/j2se/1.5.0/docs/api/java/io/package-summary.html

The entire 1.5 SE specs
http://java.sun.com/j2se/1.5.0/docs/api/index.html

Java tutorials from Sun
http://java.sun.com/developer/onlineTraining/new2java/index.html

[ April 22, 2006: Message edited by: Rusty Shackleford ]
[ April 22, 2006: Message edited by: Rusty Shackleford ]
 
Jeremy Parsons
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so say the following is a program...

class whatEver
{
public static void main (String [] args)
{

int amount;

System.out.print("Type something");
str = read.readLine();
}
}
--------------------------------------------
or am I completely missing something?..lol
 
Rusty Shackleford
Ranch Hand
Posts: 490
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have to declare and instatiate BufferedReader.

BufferedReader read = new BufferedReader(new InputStreamReader(System.in);

It is no different then any other object. It has to be instatiated to be used.

What is going on here is System.in (low level input) is being passed to InputStreamReader, which is not all that useful of a class on its own. It only has 5 methods in that class(not including inherited methods). The only read facilities are for char or char[]. BufferedReader requires a Reader object which InputStreamReader is, so is passed to BufferedReader, and is where the readLine() method is.

This way every method in InputStreamReader, and its parent(Reader) is available to you as well as the methods in BufferedReader. The really cool thing about Java IO is that it doesn't matter where the data comes from it works the same. It could be taking in input from the keyboard, writing to the monitor or a file, reading to a file, or even reading data from a socket stream. It doesn't care.
[ April 22, 2006: Message edited by: Rusty Shackleford ]
 
Jeremy Parsons
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have this as my full code to try..

class test
{

public static void main (String [] args)

{
BufferedReader read = new BufferedReader(new InputStreamReader(System.in);
int amount;

System.out.print("Type something");
amount = str = read.readLine();
System.out.print("You typed" + amount);

}

}

I have no idea as to what I may be doing wrong.. I'm so new I'm extremely green, and don't know what to do here..lol
 
Rusty Shackleford
Ranch Hand
Posts: 490
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The first error is my bad. Sorry.

Did you read the compiler errors? It really is helpful, it just takes a bit to get used to it.

I forget to add another closing parenthesis at the end.

BufferedReader read = new BufferedReader(new InputStreamReader(System.in));

The other problem is that you can not directly get an int from a String. A String is an object and int is a primitive value. To convert one to the other, you need the help of methods.

amount = str = read.readLine();

read the data in as a string and then use Integer.parseInt(str) to extract the int value, although in this program it is completely unnecessary, since println() will print it out correctly either as a String or integer. parseInt() is static, so you do not need a Integer object to use this method. Take a few minutes to look up the Integer class in the API link above.

You can handle this in basically 2 ways:

str = read.readLine();
amount = Integer.parseInt(str);

or

amount = Integer.parseInt(read.ReadLine());

The first is easier to read, but they are equivilent.

Be aware that if anything other then an integer is entered, the program will crash. You don't have to worry about it right now, but keep it in the back of your mind until you get to exceptions.
 
Jeremy Parsons
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I get the following error when I compile it..

C:\WINDOWS\Desktop\test.java:7: cannot find symbol
symbol : class BufferedReader
location: class test
BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
^
C:\WINDOWS\Desktop\test.java:7: cannot find symbol
symbol : class BufferedReader
location: class test
BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
^
C:\WINDOWS\Desktop\test.java:7: cannot find symbol
symbol : class InputStreamReader
location: class test
BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
^
3 errors

Tool completed with exit code 1

Did I use capitalization wrong, or what?
 
Rusty Shackleford
Ranch Hand
Posts: 490
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you need to import those classes. Anytime you use any class from the API(other then classes from java.lang, you must import them.

For now just add this line at the top of the source file:

import java.io.*;

That will make every class in the io package known.

You can do

import java.io.BufferedReader;
import java.io.InputStreamReader;

and only those classes will be imported.

The second way is preferable, in my opinion, for two reasons.

Using the wildcard(*) makes compiling take a bit longer, not an issue with smaller programs though. It doesn't effect the size of your .class file.

The most important reason for me, is that it help document your code.

Don't worry about all that right now. Just more stuff to think about down the road.

Has your book covered concepts like import yet? Generally those dummies books aren't the greatest, you might want to consider a second book. There is even a solid book available online for free, I don't have the link, but is probably somewhere on this site. Check out the book review section. As you know learning programming can be tough, and a poor book makes it worse, while a good book can ease your burden and help you on your way better.
 
Jeremy Parsons
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you happen to know the name of that book? I need all the help I can get.. I first started off with a Java book that I didn't know was meant for people who had programming experience.. Then I got the Dummies one and it was great for some basics, then the rest it told me to use their program DummiesIO, but I wanted to do it all via the java and stuff that's there so I can make everything my own..

When I compile the new program I get this..

C:\WINDOWS\Desktop\test.java:15: cannot find symbol
symbol : method ReadLine()
location: class java.io.BufferedReader
amount = Integer.parseInt(read.ReadLine());
^
1 error

Tool completed with exit code 1

Sorry for asking so much, but I really want to learn Java, and I'm really getting into what I do know, but it's all so new, etc..
 
Jeremy Parsons
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
that arrow is supposed to be under the period here (read.ReadLine());
 
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java is case-sensitive so you type:

amount = Integer.parseInt(read.readLine());//note the lower case r in readLine()
 
Jeremy Parsons
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now it's pointing to the ( after (read.readLine( .......Seems like it's telling me something is wrong with the bracket.. maybe I missed something there?..

C:\WINDOWS\Desktop\test.java:15: unreported exception java.io.IOException; must be caught or declared to be thrown
amount = Integer.parseInt(read.readLine());
^
1 error

Tool completed with exit code 1
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You will find details of the book on this bookshop website:which works in Britain; they have the book for 20% off the recommended price. I have a copy of the previous edition.
As you say, Burd is good for the basics of logic and syntax, giving good explanations of basic method use, but he restricts himself to beginner's work. He is weak on the object-oriented paradigm.
Because he uses his IO program, he never gets around to mentioning imports in the 1st edition; the 2nd edition I think does use imports. He is actually one of the first people I have seen to make really extensive use of the static imports.

CR
 
Rusty Shackleford
Ranch Hand
Posts: 490
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jeremy Bernard:
Now it's pointing to the ( after (read.readLine( .......Seems like it's telling me something is wrong with the bracket.. maybe I missed something there?..

C:\WINDOWS\Desktop\test.java:15: unreported exception java.io.IOException; must be caught or declared to be thrown
amount = Integer.parseInt(read.readLine());
^
1 error

Tool completed with exit code 1



Sorry about the ReadLine() error. That is what I get for trying to think late at night.



You need to catch or throw IOException whenever there is something in that method that can cause it. Exception handling is not something you need to worry about right now. So just declare that the method throws it. Doing that will cause the program to crash if an exception is thrown, but get the basics down before jumping into exceptions.

public static void main(String[] args) throws IOException
{
...
}

Here is the free book I mentioned. I haven't read it, but many people here give it high marks.

http://www.mindview.net/Books/TIJ/
[ April 22, 2006: Message edited by: Rusty Shackleford ]
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1. You need a 2nd closing brace at the end of bufferedReader. You had:



You need:



2. You need throws IOException after your public static void main...

3. You need to import java.io.*;

4. You need to declare amount as a String and parse it to an integer.

5. You should use System.out.println at the end to skip a line and make the program more readable.

6. Use spaces in you System.out.print("Put a space after this "); <--- in order to format things properly.

Here is what I came up with:


[ April 24, 2006: Message edited by: Daniel Lucas ]
 
Jeremy Parsons
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks.. I just wanted to be able to get a feel for the way that works, and I couldn't get anything.. Now I just need to learn more Java..
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do people insist you use J1.4? If you are using J5.0, find out about the java.util.Scanner class, which is much easier to use for keyboard input.

CR
 
Jeremy Parsons
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The only tutorials I've come across are for 1.3, 1.4.. I've heard java 5 or 1.5 is the best version yet..

If you came across any for the java 5 (1.5) please let me know..
 
Campbell Ritchie
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look at the sun website; it is all there. Just remember there are several names for all the Java versions. Try Java5.0 or J2 5.0 or jdk1.5.0 . . .
I don't think they update the tutorial with each release, but the API specification does. Try here.
Look for Scanner.

CR
 
reply
    Bookmark Topic Watch Topic
  • New Topic