Forum Topic: Java: inputStreamReader

(455 views • 2 replies)

This topic is 1 page long.

<< < > >>
None

Togukawa

Reply To Post Reply & Quote

Posted at: 8/25/06 04:25 PM

Togukawa LIGHT LEVEL 15

Sign-Up: 06/14/03

Posts: 1,041

Java: main

//inputStreamReader??! It's what allows you to get input from the user while running your program.

import java.io.*;
//import the package necessary to use the inputStreamReader

class Lol { // initiating your class, that's basics

public static void main(String[] args) throws IOException {
// note the extra line throws IOException.
// this is because reading stuff can generate an exception, so you've got to put
// it there.
// Eclipse will tell you this automatically if you try to use inputStreamReader
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(isr);
// two lines you need to allow the user to write something until he presses
// enter
String stuff;
// initiating a variable
stuff=in.readLine();
// reading the user's input to the variable
System.out.println("LOL YOU WROTES: "+stuff);
}
}

Do note that in.readLine returns a STRING, so if you want to read a number from the user, let's say to add two numbers, and then return the answer, we'll have to tell java that what has been entered is in fact a number.

class Lol {

public static void main(String[] args) throws IOException {
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(isr);
int stuff = Integer.parseInt(in.readLine());
//note that stuff is an int, if the user enters anything but a number you'll get an error
//Integer.parseInt is a method from the Integer class, that converts a string into a number
System.out.println("LOL YOU WROTES: " + stuff);
}
}

If you enter something other than a number, you'll get an error:
java.lang.NumberFormatException.forInputSt
ring(Unknown Source)

So you'll have to catch that NumberFormatException, but that's for someone else to write a tutorial about.
hint: try{//parseInt}catch(NumberFormatException
){//user has been a fool}


None

Craige

Reply To Post Reply & Quote

Posted at: 8/25/06 04:32 PM

Craige LIGHT LEVEL 08

Sign-Up: 07/17/04

Posts: 3,072

Why would you not catche the IO exception? Why pass it off to the interperater?


None

Togukawa

Reply To Post Reply & Quote

Posted at: 8/25/06 04:37 PM

Togukawa LIGHT LEVEL 15

Sign-Up: 06/14/03

Posts: 1,041

Because that's not really a part of the isr anymore, better that somebody makes a tutorial about trying and catching in general.
Anyway WITH try and catch then:

class Lol {

public static void main(String[] args) throws IOException {
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(isr);

try {
int stuff = Integer.parseInt(in.readLine());
System.out.println("LOL YOU WROTES: " + stuff);
} catch (NumberFormatException e) {
System.out.println("FOO, THAT AINT NO INT");
}
}
}


All times are Eastern Standard Time (GMT -5) | Current Time: 01:32 AM

<< Back

This topic is 1 page long.

<< < > >>
You need a Grounds Gold Account to post on the NG BBS! If you don't have one, click here to sign up now! It's fast, free, and easy — and opens up tons of great NG features!