Results 1 to 13 of 13

Thread: IO class wont work with ver 1.4

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2002
    Location
    Ireland
    Posts
    8

    IO class wont work with ver 1.4

    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..........

  2. #2

    Thread Starter
    New Member
    Join Date
    Jan 2002
    Location
    Ireland
    Posts
    8
    Here is the class (source).
    Attached Files Attached Files
    Last edited by rami3; Nov 11th, 2002 at 10:49 AM.
    If builders built buildings the way programmers wrote programs......the first woodpecker to come along would destroy civilisation..........

  3. #3
    Hyperactive Member marnitzg's Avatar
    Join Date
    Oct 2000
    Location
    South Africa
    Posts
    372
    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
    Attached Images Attached Images  

  4. #4

    Thread Starter
    New Member
    Join Date
    Jan 2002
    Location
    Ireland
    Posts
    8
    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
    Attached Images Attached Images  
    If builders built buildings the way programmers wrote programs......the first woodpecker to come along would destroy civilisation..........

  5. #5
    Hyperactive Member marnitzg's Avatar
    Join Date
    Oct 2000
    Location
    South Africa
    Posts
    372
    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?

  6. #6

    Thread Starter
    New Member
    Join Date
    Jan 2002
    Location
    Ireland
    Posts
    8
    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
    Attached Files Attached Files
    If builders built buildings the way programmers wrote programs......the first woodpecker to come along would destroy civilisation..........

  7. #7

    Thread Starter
    New Member
    Join Date
    Jan 2002
    Location
    Ireland
    Posts
    8
    ++number
    Attached Images Attached Images  
    If builders built buildings the way programmers wrote programs......the first woodpecker to come along would destroy civilisation..........

  8. #8

    Thread Starter
    New Member
    Join Date
    Jan 2002
    Location
    Ireland
    Posts
    8
    +number
    Attached Images Attached Images  
    If builders built buildings the way programmers wrote programs......the first woodpecker to come along would destroy civilisation..........

  9. #9
    Hyperactive Member marnitzg's Avatar
    Join Date
    Oct 2000
    Location
    South Africa
    Posts
    372
    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

  10. #10
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Why not use System.in directly?
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  11. #11

    Thread Starter
    New Member
    Join Date
    Jan 2002
    Location
    Ireland
    Posts
    8
    Marnitzg, thanks for taking the time i appreciate it.

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

    Thanks

    RB
    If builders built buildings the way programmers wrote programs......the first woodpecker to come along would destroy civilisation..........

  12. #12
    Hyperactive Member marnitzg's Avatar
    Join Date
    Oct 2000
    Location
    South Africa
    Posts
    372
    char a = System.in.read() or something similar

    Did you try the bufferedreader? Its a lot easier

  13. #13
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Code:
    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 + "!");
    }
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width