• 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

how do i input

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is In a Scanner?
 
Nya Chan
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
scanner? its from books on java....
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
didn't work...

it says:
error: no match was found for method "parseInt ()".
 
Keith Lynn
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
parseInt works like this

 
Nya Chan
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Nya Chan
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok sorry

i kinda get it now
 
Nya Chan
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i tried using scanner but it says:
Error: type Scanner was not found

 
Nya Chan
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
btw does anyone know where i can find an easy to understand tutorial for java?
 
Keith Lynn
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What version of the JDK do you have? Scanner was introduced in 1.5
 
Nya Chan
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
my teacher gave me Ready to Program with Java 1.5
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic