| Author |
how do i input
|
Nya Chan
Greenhorn
Joined: Jun 28, 2006
Posts: 12
|
|
i think i posted at the wrong area so i copied it here. I know this sounds stupid but I just started java yesterday and the codes in my books don't match. So... One of the books told me to use type: --------------------------------------------- import java.io.*; class blah { public static void main (String [] args) { BufferedReader br = new BufferedReader ( new InputStreamReader (System.in)); //and then String s = br.readLine (); } } ---------------------------------------------- but that only work with strings my other book took me to type: --------------------------------------------- class blah { public static void main (String [] args) { System.out.println ("type a number."); int num = In.getInt (); } } -------------------------------------------- which doesn't work at all... so help?
|
 |
Keith Lynn
Ranch Hand
Joined: Feb 07, 2005
Posts: 2341
|
|
|
Is In a Scanner?
|
 |
Nya Chan
Greenhorn
Joined: Jun 28, 2006
Posts: 12
|
|
|
scanner? its from books on java....
|
 |
alex delaguardia
Greenhorn
Joined: Sep 07, 2004
Posts: 5
|
|
to get an int you will need to use the integer class. int I = (new Integer(string)).parseInt(); it looks something like this, You should look at the documetation of the integer class. This is assuming that the imput will be only int.
|
 |
Nya Chan
Greenhorn
Joined: Jun 28, 2006
Posts: 12
|
|
doesn't int I = (new Integer(string)).parseInt(); mean something like converting a string to int? just wondering since the code for reading a string is so short
|
 |
Nya Chan
Greenhorn
Joined: Jun 28, 2006
Posts: 12
|
|
didn't work... it says: error: no match was found for method "parseInt ()".
|
 |
Keith Lynn
Ranch Hand
Joined: Feb 07, 2005
Posts: 2341
|
|
parseInt works like this
|
 |
Nya Chan
Greenhorn
Joined: Jun 28, 2006
Posts: 12
|
|
oh ok it works now, thanx but still, does parseInt mean converting a string into a int? it there someway to get an int straight?
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32611
|
|
int I = (new Integer(string)).parseInt(); mean something like converting a string to int?
You are learning quickly. Yes it does. Only you have been given it spelt wrongly. It should read:-
the codes in my books don't match.
No, they don't. What you wrote first is the old way of reading a String [stated simply a String is writing of any length from completely blank to the text of "War and Peace," or longer.] This way of reading from the keyboard is awkward to set up, but still works. The second way is the newer way of doing it, which has only been available since Autumn 2004. As Keith Lynn suggests, your "in" in the 2nd example is probably a Scanner. You can read the introduction to the Scanner class in the API documentation. [If you can't see two frames on the left click "FRAMES", then scroll down the lower left frame until you find "Scanner."] It is described as "simple," but it is very effective for those situations where simplicity is acceptable.
just wondering since the code for reading a string is so short
It is suprising in Java how much you can do with a single line. The Integer.parseInt() method occupies about 3/4 of a page, and it uses another method, but you can use the whole lot for one line.
the codes in my books don't match
No, they don't. You will find in everything in computing there are several ways to do it. Often severla "right" ways, and just as frequently, some "wrong" ways.
|
 |
Ernest Friedman-Hill
author and iconoclast
Marshal
Joined: Jul 08, 2003
Posts: 24045
|
|
Hi, Welcome to JavaRanch! Please don't post the same question to more than one forum; see our explanation here. I've deleted the other copy of this thread.
|
[Jess in Action][AskingGoodQuestions]
|
 |
Nya Chan
Greenhorn
Joined: Jun 28, 2006
Posts: 12
|
|
ok sorry i kinda get it now
|
 |
Nya Chan
Greenhorn
Joined: Jun 28, 2006
Posts: 12
|
|
i tried using scanner but it says: Error: type Scanner was not found
|
 |
Nya Chan
Greenhorn
Joined: Jun 28, 2006
Posts: 12
|
|
|
btw does anyone know where i can find an easy to understand tutorial for java?
|
 |
Keith Lynn
Ranch Hand
Joined: Feb 07, 2005
Posts: 2341
|
|
|
What version of the JDK do you have? Scanner was introduced in 1.5
|
 |
Nya Chan
Greenhorn
Joined: Jun 28, 2006
Posts: 12
|
|
|
my teacher gave me Ready to Program with Java 1.5
|
 |
Stan James
(instanceof Sidekick)
Ranch Hand
Joined: Jan 29, 2003
Posts: 8791
|
|
The Sun Java Tutorial is pretty good, I think. If you have programmed in just about any other language, Thinking In Java will help you make the jump into Java. Both are free reading on the Internet. And keep coming here! The best way to use the ranch is to get something that almost works, post smallish code snippets and let us give you hints on what to try next. Just like you did this time.
|
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
|
 |
 |
|
|
subject: how do i input
|
|
|