1 2 Previous Next 17 Replies Latest reply: Oct 1, 2007 8:20 AM by 807600 RSS

    error when trying to read integer with JCreator

    807600
      *********CODE:

      import corejava.*;
      class Reverse {
           public static void main(String[] args) {
                int number;
                number = Console.readint("Please enter an integer: ");
           }     
      }

      ***********ERROR:

      --------------------Configuration: TEST - j2sdk1.4.2_15 <Default> - <Default>--------------------
      C:\Documents and Settings\mik\My Documents\JCreator Pro\MyProjects\a\TEST\src\TEST.java:6: cannot access corejava.Console
      bad class file: C:\j2sdk1.4.2_15\jre\lib\ext\corejava.jar(corejava/Console.class)
      class file has wrong version 49.0, should be 48.0
      Please remove or make sure it appears in the correct subdirectory of the classpath.
      number = Console.readint("Please enter an integer: ");
      ^
      1 error

      Process completed.
      ************************

      Why is this happening ?
      i just cant seem to read an integer from the user no matter what i try :-(

      Edited by: miki85 on Oct 1, 2007 5:37 AM
        • 1. Re: error when trying to read integer with JCreator
          800308
          class file has wrong version 49.0, should be 48.0
          corejava (whatever it is) has been compiled use a later version of java than the one you are using.

          Does anyone know how to figure out which version of java build 48 is/was? Don't you just compile with -Xcompat or something like that and the first four bytes of the class file is the compilers build number, or something like that.
          • 2. Re: error when trying to read integer with JCreator
            807600
            well if you dont know corejava.jar then i guess it's not obligatory...
            how else can i Read a single integer to my "number" ?
            • 3. Re: error when trying to read integer with JCreator
              807600
              Possibly read a string and use Integer.parseInt (yourString);
              • 4. Re: error when trying to read integer with JCreator
                800308
                Here's one I used to use back in the 1.5 days... I take no credit for the code though.
                package krc.io;
                /**
                 * Keyboard - static methods for basic line-buffered keyboard input.
                 * Author: J. M. Morris (jmm at dcs gla ac uk).
                 * Version 1.1 J. M. Morris October 28th 1998.
                 * Version 1.2 K. R. Corlett January 27th 2007.
                 *
                 * Example of use
                 * --------------
                 * The statements:
                 *     i=Keyboard.readInt();
                 *     b=Keyboard.readBoolean();
                 *     c=Keyboard.readChar();
                 *     t=Keyboard.readToken();
                 *     s=Keyboard.readString();
                 *     System.out.println("> i='"+i+"', b='"+b+"', c='"+c+"', t='"+t+"', and s='"+s+"'");
                 * Given the Keyboard input:
                 *     "      23    TRue X   Java123   the  rest   "
                 * Produce the output:
                 *     > i=23, b=true, c='X', t="Java123", and s="  the  rest   "
                 * See: the unit tests in the main method for lots more usage examples.
                 */
                
                import java.io.*;
                import java.util.List;
                import java.util.ArrayList;
                
                public abstract class Keyboard {
                
                    static BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
                    static String bfr = "";
                    static int bfrlen = 0;
                    static int p = 1; // bfr[p..] contains next input
                
                    //
                    // BASIC METHODS - READ THE NEXT WORD AND CONVERT IT TO DESIRED TYPE
                    //
                
                    // Consume and return the remainder of current line (end-of-line discarded).
                    public static String readString() {
                        try {
                            if( bfr!=null && p>bfrlen ) readLine();
                            eofCheck();
                            int t=p; p=bfrlen+1;
                            return(bfr.substring(t));
                        } catch (IOException ex) {
                            System.err.println(ex);
                            return "";
                        }
                    }
                    public static String readString(String prompt) {
                        print(prompt + " (String) : ");
                        return readString();
                    }
                
                    // Consume and return a character (which may be an end-of-line).
                    public static char readChar() {
                        try {
                            if( bfr!=null && p>bfrlen ) readLine();
                            eofCheck();
                            char c = (p==bfrlen ? '\n' : bfr.charAt(p-1));
                            p++;
                            return(c);
                        } catch (StringIndexOutOfBoundsException ex) {
                            //just silently swallow the exception !!!
                            return((char)0);
                        } catch (IOException ex) {
                            System.err.println(ex);
                            return((char)0);
                        }
                    }
                    public static char readChar(String prompt) {
                        print(prompt +" (char) : ");
                        return readChar();
                    }
                
                    // Consume and return a boolean. Trailing delimiter consumed.
                    // Any string other than "true" (case insensitive) is false.
                    public static boolean readBoolean() {
                        try {
                            return new Boolean(getToken()).booleanValue();
                        } catch (Exception ex) {
                            System.err.println(ex);
                            return false;
                        }
                    }
                    public static boolean readBoolean(String prompt) {
                        print(prompt + " (boolean) : ");
                        return readBoolean();
                    }
                
                    // Consume and return an integer. Trailing delimiter consumed.
                    public static int readInt() {
                        try {
                            return Integer.parseInt(getToken());
                        } catch (Exception ex) {
                            System.err.println(ex);
                            return 0;
                        }
                    }
                    public static int readInt(String prompt) {
                        print(prompt + " (int) : ");
                        return readInt();
                    }
                
                    // Consume and return a double. Trailing delimiter consumed.
                    public static double readDouble() {
                        try {
                            return new Double(getToken()).doubleValue();
                        } catch (Exception ex) {
                            System.err.println(ex);
                            return 0.0;
                        }
                    }
                    public static double readDouble(String prompt) {
                        print(prompt + " (double) : ");
                        return readDouble();
                    }
                
                    //
                    // ADVANCED PUBLIC METHODS
                    //
                
                    // Consume and return the next token (word) from input. 
                    // The trailing delimiter is also consumed. A token is a maximal sequence 
                    // of non-whitespace characters.
                    public static String readToken() {
                        try {
                            return getToken();
                        } catch (IOException ex) {
                            System.err.println(ex);
                            return "";
                        }
                    }
                
                    // The next available character if any (which may be an end-of-line). The
                    // character is not consumed. If bfr is empty return null character.
                    public static char peekChar() {
                        if (bfr==null || p>bfrlen) return('\000');
                        else if (p==bfrlen) return('\n');
                        else return bfr.charAt(p);
                    }
                
                    public static int available() {
                    // Number of characters available on this line (including end-of-line,
                    // which counts as one character, ie '\n')
                        if (bfr==null) return 0;
                        else return (bfrlen+1-p);
                    }
                
                    public static boolean hasMoreTokens() {
                    // Are there more tokens on the current line?
                        if(bfr==null) return false;
                        int q;
                        for(q=p; q<bfrlen && isWhitespace(q); q++);
                        return(q<bfrlen);
                    }
                
                    public static void skipLine() {
                    // Skip any remaining input on this line.
                        if(bfr!=null) p = bfrlen+1;
                    }
                
                    public static void skipWhitespace() {
                    // Consumes whitespaces until a non-whitespace character is entered (which
                    // is not consumed).
                        try {
                            skipWhitespaces();
                        } catch (IOException ex) {
                            System.err.println(ex);
                        }
                    }
                
                    public static boolean eof() { // NOT More characters?
                    // This method is intended for use when stdin has been redirected from file
                        if(available()>0) return false;
                        try {
                            readLine();
                        } catch (IOException ex) {
                            System.err.println(ex);
                        }
                        return (bfr == null);
                    }
                
                    public static String pause() {
                        print("Press return to continue.");
                        return(Keyboard.readString());
                    }
                
                    public static boolean isWhitespace(int offset) {
                        return (Character.isWhitespace(bfr.charAt(offset)));
                    }
                
                    //
                    // PRIVATE HELPER METHODS
                    //
                
                    private static void readLine() throws IOException {
                        p = 0;
                        bfrlen = 0;
                        bfr = null;
                        bfr = stdin.readLine();
                        bfrlen = bfr.length();
                    }
                
                    private static String getToken() throws IOException {
                    // Consumes and returns the next word from input.
                        skipWhitespaces();
                        int t = p++;
                        while( p<bfrlen && !(isWhitespace(p)) ) p++;
                        return(bfr.substring(t,p++));
                    }
                
                    private static void skipWhitespaces() throws IOException {
                    // Consumes input until a non-whitespace character is entered (which
                    // is not consumed).
                        while ( bfr!=null && (p>=bfrlen||isWhitespace(p)) ) {
                            if (p>=bfrlen) {
                                readLine();
                            } else {
                                p++;
                            }
                        }
                    }
                
                    private static void eofCheck() throws IOException {
                        if(bfr==null) throw new IOException("Unexpected end of file.");
                    }
                
                    private static void print(String msg) {
                        System.out.print(msg);
                    }
                
                    private static void println(String msg) {
                        System.out.println(msg);
                    }
                
                    //
                    // test harness
                    //
                
                    public static void main(String[] args) {
                        int i;
                        double d;
                        char c;
                        boolean b;
                        String s, t, u;
                        println("TEST KEYBOARD VERSION 1.1");
                        println("-------------------------");
                        print("\nEnter a sequence of integers, terminated by -9: ");
                        do {
                            i = Keyboard.readInt();
                            println("> i=^"+i+"^");
                        } while(i!=-9);
                
                        print("\nEnter integer character double boolean string: ");
                        i = Keyboard.readInt(); println(""+i);
                        c = Keyboard.readChar(); println(""+c);
                        d = Keyboard.readDouble(); println(""+d);
                        b = Keyboard.readBoolean(); println(""+b);
                        s = Keyboard.readString(); println(s);
                        pause();
                
                        // Test for the empty string
                        print("\nTest no input: ");
                        s = pause();
                        println("> expect s=^^; s=^"+s+"^"); 
                
                        // Test readInt eats trailing spaces
                        println("\nTest readInt eats trailing spaces: Copy & paste the below line:");
                        println("3 ");
                        i = Keyboard.readInt();
                        s = Keyboard.readString();
                        println("> expect s=^^; s=^"+s+"^"); 
                
                        // Test skipWhitespace
                        println("\nTest skipWhitespace: Copy & paste the below line");
                        println("      123");
                        Keyboard.skipWhitespace();
                        c = Keyboard.peekChar();
                        println("The first non-whitespace you entered was: "+c);
                        println("There should be 3 characters available is: "+Keyboard.available());
                        Keyboard.skipLine();
                        c = Keyboard.peekChar();
                        if (c == '\000') println("Nothing available.");
                        else println("Available c=^"+c+"^");
                
                        // Test read methods
                        println("\nTest read methods: Copy & paste the below line");
                        println("      23    TRue X   Java123   the  rest   ");
                        i = Keyboard.readInt(); println("> i=^"+i+"^");
                        b = Keyboard.readBoolean();  println("> b=^"+b+"^");
                        c = Keyboard.readChar(); println("> c=^"+c+"^");
                        t = Keyboard.readToken();  println("> t=^"+t+"^");
                        s = Keyboard.readString(); println("> s=^"+s+"^");
                
                        // Test hasMoreTokens()
                        println("\nTest hasMoreTokens: Enter several words on a line : ");
                        List<String> list = new ArrayList<String>(10);
                        do {
                            list.add(Keyboard.readToken());
                        } while(Keyboard.hasMoreTokens());
                        Keyboard.skipLine();
                        for (String item : list) {
                            println("> " + item);
                        }
                        // Test available()
                        println("Test available: Enter an arbitrary line of text");
                        s = "";
                        c = Keyboard.readChar(); s += c;
                        while(Keyboard.available()>0) {
                            c = Keyboard.readChar(); s += c;
                        }
                        println(s);
                
                    }
                
                }
                Edited by: corlettk on Oct 1, 2007 1:12 PM
                • 5. Re: error when trying to read integer with JCreator
                  807600
                  you said:
                  "Possibly read a string and use Integer.parseInt (yourString); "

                  how do i read a string ?
                  • 6. Re: error when trying to read integer with JCreator
                    807600
                    The code seems to be generating some Errors in JCreator :-(

                    is there another program you can recommend that is good ?
                    JCreator seems to not like me or something ...
                    • 7. Re: error when trying to read integer with JCreator
                      807600
                      Well, depends on your circumstances. Maybe
                      BufferedReader input = new BufferedReader( new InputStreamReader( System.in ) );
                      String inputHere = input.readLine();
                      • 8. Re: error when trying to read integer with JCreator
                        807600
                        BufferedReader input = new BufferedReader( new InputStreamReader( System.in ) );
                        String inputHere = input.readLine();

                        SEEMS TO GENERATE THESE ERRORS:


                        -------------------Configuration: TEST - j2sdk1.4.2_15 <Default> - <Default>--------------------
                        C:\Documents and Settings\mik\My Documents\JCreator Pro\MyProjects\a\TEST\src\TEST.java:6: cannot resolve symbol
                        symbol : class BufferedReader
                        location: class Reverse
                        BufferedReader input = new BufferedReader( new InputStreamReader( System.in ) );
                        ^
                        C:\Documents and Settings\mik\My Documents\JCreator Pro\MyProjects\a\TEST\src\TEST.java:6: cannot resolve symbol
                        symbol : class BufferedReader
                        location: class Reverse
                        BufferedReader input = new BufferedReader( new InputStreamReader( System.in ) );
                        ^
                        C:\Documents and Settings\mik\My Documents\JCreator Pro\MyProjects\a\TEST\src\TEST.java:6: cannot resolve symbol
                        symbol : class InputStreamReader
                        location: class Reverse
                        BufferedReader input = new BufferedReader( new InputStreamReader( System.in ) );
                        ^
                        3 errors

                        Process completed.
                        • 9. Re: error when trying to read integer with JCreator
                          807600
                          miki85 wrote:
                          you said:
                          "Possibly read a string and use Integer.parseInt (yourString); "

                          how do i read a string ?
                          we're not here to hold your hand...

                          In other words, do your own homework and look it up in your courseware or some tutorial.
                          • 10. Re: error when trying to read integer with JCreator
                            807600
                            Hint: Try adding appropriate import statements for these classes or their packages.
                            • 11. Re: error when trying to read integer with JCreator
                              807600
                              I DID!
                              i tried reading in so many places..
                              read some tutorials and everytime i get to reading a line \ int \ string\ whatever... i get errors...

                              or that i'm not doing it right and this is why i asked "how do i read string"... in order to see how programmers do it and then i will see if it's working or not because the tutorials and the google searches didnt help me...

                              and the other reason... the one i'm starting to think about is that JCreator is not setted up right or that something there isnt working for reading something from the user...

                              and no metter what if you dont want to help me you dont have to...
                              *and about holding your hands... that wasnt nice!, people ask questions not so they can take the easy way.. i hope that most people do search for answares them selves before asking in a forum...

                              Edited by: miki85 on Oct 1, 2007 6:31 AM
                              • 12. Re: error when trying to read integer with JCreator
                                807600
                                int number;
                                System.out.println("Integer:");
                                number = System.in.read();

                                Out alone writes "Integer:"... if i add In.Read it generated an error:

                                *--------------------Configuration: TEST - j2sdk1.4.2_15 <Default> - <Default>--------------------
                                C:\Documents and Settings\mik\My Documents\JCreator Pro\MyProjects\a\TEST\src\TEST.java:6: unreported exception java.io.IOException; must be caught or declared to be thrown
                                number = System.in.read();
                                ^
                                1 error

                                Process completed.*


                                Why isnt it working ? it spose to... i see it everywhere !
                                • 13. Re: error when trying to read integer with JCreator
                                  807600
                                  The compiler is really trying to give you some help here:
                                  java.io.IOException; must be caught or declared to be thrown
                                  Either catch it or throw it. For now, to get it working, just wrap the code in a try / catch block:
                                  try
                                  {
                                  ... code ...
                                  }
                                  catch (IOException e)
                                  {
                                  ... prnt exception or whatever ....
                                  }
                                  • 14. Re: error when trying to read integer with JCreator
                                    807600
                                    allright !!!
                                    this one working but with one problem... i type 50 and it prints out 53
                                    if i type 43 it prints 52... it switch the number somehow, why's that ?
                                    (i'm a few years visual-basic programmer and i'm not familiar with these kind of things ?)

                                    import java.io.*;
                                    class Reverse {
                                         public static void main(String[] args) {
                                    int number;
                                    try
                                    {
                                    BufferedReader input = new BufferedReader( new InputStreamReader( System.in ) );
                                    number = System.in.read();
                                    System.out.println(number);
                                    }
                                    catch (IOException e)
                                    {
                                    }
                                    }
                                    }
                                    1 2 Previous Next