I've just started learning java, the college where im learning it has provided me with an class that handles input from the keyboard, problem is that the college is using sun's java 1.2, im using 1.4 at home, when I try and run a program calling any methods i always get 0 returned.
eg if i try and run (at home)
class Test {
public static void main (String [] args) {
System.out.println("Enter a number");
int number = UbiIO.readInt();
System.out.println(+number);
}
}
I get back
Error: Not an integer
I dont even get the chance to input the number.
At work where we use v1.2 (on linux) it lets me input the number but wont continue until i press space then enter, then i get the same error message back as above.
I asked my tutor and got the reply "yes im aware of that problem", when i asked for a way to fix the class i got the reply "havent got the time, you should try and work it out yourself etc etc"
OK fair enough, excpt i dont bloody know enough yet to do that, we dont even start IO streams until january, until then we have to use this class they provided.
We are allowed to modify, do what we like with the class, so i have attached it to this message, if somebody could explain what is wrong with it or point me in the right direction as to how i could fix it i would be very appreciative.
RB
If builders built buildings the way programmers wrote programs......the first woodpecker to come along would destroy civilisation..........
I done that anyhow and got back the same as i have been getting.
The point of class Test is a quick way to show the problem, class Test is useless.
The jpg attached is where i run class Test, at the first 2 i pessed enter, nothing, at the second i pressed space then enter and got back Error: Not an integer, 1.
Still confused
RB
If builders built buildings the way programmers wrote programs......the first woodpecker to come along would destroy civilisation..........
Yes the ++ adds one. Just remove the plusses then, you don't need them if you just want to print. Its a really strange error that you're getting there. I really can't see whats wrong, and as I've demonstrated, it actually should work. By what you're telling me it seems that its only accepting when you press enter twice. Try this, enter a number and press enter twice. The space shouldn't make a difference, it gets ignored.
The first screenshots were on linux (jdk 1.2.1), the second below are from Win XP (pro).
I've a big assignment coming up, so i need some sort of quick IO interface to use at home to practice the assignment (we have to write a date validation class and a test class for it in 4 hours!!!!!), I found a good example on IO streams and managed to build a class to do it, not sure about it though, when i use it i have import java.io.* and add throw IOException to my main method. Like i said im just starting to learn java, so if you could give me an idea on how to make this class better i would appreciate it. I have attached the source.
Thanks
RB
If builders built buildings the way programmers wrote programs......the first woodpecker to come along would destroy civilisation..........
I don't really know how to help you. As I've said before, it does actually work. What you can do instead is use a bufferedreader
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
Then use String input = in.readLine() or something to that effect.
Then to change to an integer use
try
{
int i = Integer.parseInt(input);
}
catch (NumberFormatException e)
{
System.out.println("Not an integer");
}
The reason you got the error on the screenshot is because you didn't enter anything. The one afterwards is the number returned + 1. ie 0 + 1 = 1 which was displayed.