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}