9 Replies Latest reply: May 22, 2010 6:11 PM by 796440 RSS

    Reading signed interger from a text file:

    807580
      Is this possible with some construct in java? Attempting to read a signed number using .readInt() throws an exception before the first token is a malformed int. i,e, the sign + or -.

      I would like to read a signed integer from a text file but I am clueless.

      Any help is appreciated.
        • 1. Re: Reading signed interger from a text file:
          jschellSomeoneStoleMyAlias
          First step read text.
          Second step convert text to a number.
          • 2. Re: Reading signed interger from a text file:
            807580
            Ebodee wrote:
            Is this possible with some construct in java? Attempting to read a signed number using .readInt() throws an exception before the first token is a malformed int. i,e, the sign + or -.

            I would like to read a signed integer from a text file but I am clueless.

            Any help is appreciated.
            I don't know what you're doing but readInt shouldn't give you an exception. It just reads 4 bytes whatever they may hold and interpret the content as an int.
            • 3. Re: Reading signed interger from a text file:
              jschellSomeoneStoleMyAlias
              whoopee wrote:
              I don't know what you're doing but readInt shouldn't give you an exception. It just reads 4 bytes whatever they may hold and interpret the content as an int.
              Of course if the file doesn't contain 4 bytes then it would throw an exception. For example if the following was the only thing in the file.

              -2
              • 4. Re: Reading signed interger from a text file:
                796440
                Ebodee wrote:
                Is this possible with some construct in java? Attempting to read a signed number using .readInt() throws an exception before the first token is a malformed int. i,e, the sign + or -.

                I would like to read a signed integer from a text file but I am clueless.

                Any help is appreciated.
                DataInputStream.readInt? It's not meant for reading textual representations of integers. It's meant for reading an int in binary format.

                Use Scanner.nextInt(), or BufferedReader.readLine() and Integer.parseInt().

                And note that it's integer, not inte[b]Rger.
                • 5. Re: Reading signed interger from a text file:
                  807580
                  First step read text.
                  Done! Read into a string variable "plus 8";
                  Second step convert text to a number.
                  NumberFormatMismatch error. Apparently "+8" will not be interpreted at a positive 8 in Java, so I continue to have difficulty.

                  I also add that I am using Scanner and nextInt(); It is interesting to note that althought that syntax of an Integer is complete with the following lexemes: {+. -. e (empty character)} <digit>, the writer of java did not consider a leading + in a text file followed by a digit to be a properly formatted positive integer.

                  I removed all leading positives from my integers and strangely java did not complain when it had to Integer.decode(String variable).

                  Edited by: Ebodee on May 22, 2010 4:39 PM
                  • 6. Re: Reading signed interger from a text file:
                    796440
                    Provide an SSCCE and a sample of your input data so we can see exactly what you're doing and exactly what you're operating on.
                    • 7. Re: Reading signed interger from a text file:
                      807580
                      jschell wrote:
                      whoopee wrote:
                      I don't know what you're doing but readInt shouldn't give you an exception. It just reads 4 bytes whatever they may hold and interpret the content as an int.
                      Of course if the file doesn't contain 4 bytes then it would throw an exception. For example if the following was the only thing in the file.

                      -2
                      Well, my guess is that the OP reads a text file in binary format.
                      • 8. Re: Reading signed interger from a text file:
                        807580
                        It can be a bit of a dilemma for lexical analysers. Say you read
                        "a + 12"
                        then you'd generally want to process that as three separate lexemes. The monadic form of plus is less common than the monadic minus.

                        A quick test shows that
                        "+12"
                        and
                        "+ 12"
                        are both rejected by Integer.parseInt() (which surprised me).

                        I'd suggest if you want to read positive numbers then first analyse your input using a regular expression, for example
                        "\\s*([-+]?)\\s*(\\d+)"
                        followed by
                        (matcher.match(1).equals("-") ? -1 : 1) * Integer.parseInt(matcher.match(2));
                        Edited by: malcolmmc on May 24, 2010 2:10 PM
                        • 9. Re: Reading signed interger from a text file:
                          807580
                          It allows the - sign, just not the + sign. My guess is since its unnecessary.