PDA

Click to See Complete Forum and Search --> : IO class wont work with ver 1.4


rami3
Nov 11th, 2002, 06:29 AM
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 :)

rami3
Nov 11th, 2002, 06:31 AM
Here is the class (source).

marnitzg
Nov 11th, 2002, 03:51 PM
Works for me. All I did was change your
System.out.println(+number);
to
System.out.println(++number);

I think that made quite some difference though :)

rami3
Nov 12th, 2002, 02:51 AM
Thanks for takling the time. :)

But ++number? wont that increment the variable.

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

marnitzg
Nov 12th, 2002, 12:19 PM
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.

BTW what os are you using?

rami3
Nov 12th, 2002, 04:06 PM
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 :)

rami3
Nov 12th, 2002, 04:09 PM
++number

rami3
Nov 12th, 2002, 04:10 PM
+number

marnitzg
Nov 13th, 2002, 11:53 AM
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.

Don't know what more to say honestly

CornedBee
Nov 13th, 2002, 12:30 PM
Why not use System.in directly?

rami3
Nov 14th, 2002, 04:42 AM
Marnitzg, thanks for taking the time i appreciate it. :)

CornedBee, sorry for sounding ignorant but how do i use System.in directly.

Thanks

RB :)

marnitzg
Nov 14th, 2002, 02:30 PM
char a = System.in.read() or something similar

Did you try the bufferedreader? Its a lot easier

parksie
Nov 23rd, 2002, 09:37 AM
public static void main(String[] args) {
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
System.out.print("What's your name? ");
String nm = input.readLine();

System.out.println("\nHello " + nm + "!");
}