Well you can tell me any inside information from apple *wink* *wink*
Everyone aspires to be something, we just happen to share the same goal - to be better at programming. Our age and and amount of completed projects varies probably to a great deal, but I was an aspiring new coder too - and still am, there's always so much more to do.
Anyhow, I intended to read som uni math, but got bored.
here is an input class for java:
Code:
import java.io.*;
class inPut {
Object item;
private InputStreamReader isr = new InputStreamReader( System.in );
private BufferedReader stdin = new BufferedReader( isr );
private boolean debug;
private String lastLine;
private int lastInt = 0;
public inPut() {
this.debug = false;
}
public inPut(boolean d) {
this.debug = d;
}
/**
* reads the next line of text, if IO error it will run another set of this
*/
public String nextLine() {
try {
String input = stdin.readLine();
// if input isn't null or shorter than String.length = 1 returns string
if(input != null || input.length() > 0) {
this.lastLine = input;
return this.lastLine;
}
} catch(IOException e) {
// if debug is true and error is reached application tells why and dies
// otherwise business as usual (retries)
if (this.debug) {
System.out.println("IOException error");
System.exit(1);
}
this.nextLine();
}
// if
return null;
}
public int nextInt() {
try {
String input = stdin.readLine();
// if input isn't null or shorter than String.length = 1 returns string
if(input != null || input.length() > 0) {
this.lastInt = Integer.valueOf(input);
return this.lastInt;
}
} catch(IOException e) {
// if debug is true and error is reached application tells why and dies
// otherwise business as usual (retries)
if (this.debug) {
System.out.println("IOException error");
System.exit(1);
}
this.nextInt();
}
return 0;
}
// same as the two methods above, only prints out some text too before input is needed
public int nextInt(String str) {
System.out.println(str);
return this.nextInt();
}
public String nextString(String str) {
System.out.println(str);
return this.nextLine();
}
public int getLastInt() {
return this.lastInt;
}
public String getLastLine() {
return this.lastLine;
}
}
(not on my main comp atm so no tar ball
Anyhow you declare this using:
Code:
inPut myInput = new inPut();
standard way
Code:
inPut myInput = new inPut(boolean debug);
this also allows some minor debugging - it will kill the application if you can't read anything
the following commands are available:
(if you myInput can be anything - it's the "name" you declare it as)
myInput.nextLine()
// returns the next line (end with [ENTER]) as a String
// if you only press enter return will be "null"
myInput.nextInt()
// returns the next int inserted
// if nothing then you get a "0" as return
myInput.nextLine(String str)
// returns same as nextLine() only prints the String str first
myInput.nextInt(String str)
// same as nextInt() only prints out the String str first
myInput.getLastInt()
// returns the latest int read with myInput.nextInt() / nextInt(String str)
myInput.getLastLine()
// returns the latest String read with myInput.nextLine() / nextLine(String str)
I will make the code more secure than this tomorrow though, but this should be enough to start implementing stuff.
And rather than just posting untested code I've actually compiled the stuff this time (but no test class - too late
)
Remember, this is terminal only - don't mix this with your GUI-thingies =P