Cannot Convert input into integer

Discussion in 'Java' started by pandit, Dec 28, 2007.

  1. pandit

    pandit Guest

    Hello all,
    thanks for solving my Old Problem now i m posting my problem here.
    The Problem is that i m using "User Input" BufferReader and i m
    getting input fro user
    but when i m run this program this don't return me integer value here
    i m doing addition of two integer but its not return me return value
    wht's wrong with my Code

    import java.io.*;
    class inputread
    {
    public static void main(String args[]) throws IOException
    {

    BufferedReader br = new BufferedReader(new
    InputStreamReader(System.in));
    BufferedReader br1= new BufferedReader(new
    InputStreamReader(System.in));
    System.out.print("\t\tPlease Enter Value For A:- ");
    int a,b,c;
    a=(int)br.read();
    System.out.print("\t\tPlease Enter Value For B:- ");
    b=(int)br1.read();
    c=a+b;
    System.out.println("\t\tThe Sum Of A + B :- "+c);
    }

    if i m presing enter two times
    it is giving me 26
    13 for the Enter Value
    now What i do
    Thanks in Advance.
    pandit, Dec 28, 2007
    #1
    1. Advertising

  2. pandit

    James Guest

    On Fri, 28 Dec 2007 03:44:25 -0800, pandit wrote:

    > Hello all,
    > thanks for solving my Old Problem now i m posting my problem here. The
    > Problem is that i m using "User Input" BufferReader and i m getting
    > input fro user
    > but when i m run this program this don't return me integer value here i
    > m doing addition of two integer but its not return me return value
    > wht's wrong with my Code
    >
    > import java.io.*;
    > class inputread
    > {
    > public static void main(String args[]) throws IOException {
    >
    > BufferedReader br = new BufferedReader(new
    > InputStreamReader(System.in));
    > BufferedReader br1= new BufferedReader(new
    > InputStreamReader(System.in));
    > System.out.print("\t\tPlease Enter Value For A:- "); int a,b,c;
    > a=(int)br.read();
    > System.out.print("\t\tPlease Enter Value For B:- "); b=(int)

    br1.read();
    > c=a+b;
    > System.out.println("\t\tThe Sum Of A + B :- "+c); }
    >
    > if i m presing enter two times
    > it is giving me 26
    > 13 for the Enter Value
    > now What i do
    > Thanks in Advance.


    You only need the one buffered reader so br1 is unecessary.
    a= (int) br.readLine();
    ....
    b= (int) br.readLine();

    I highly suggest you use the Java API and a Java Tutorial, both available
    for free from Sun's website. There is often more than one way to do
    things and you will want to include data validation (e.g. you type
    letters or a fraction instead of an integer)....
    --
    James
    *Note: Remove every other letter for correct email address
    James, Dec 28, 2007
    #2
    1. Advertising

  3. James wrote:
    > You only need the one buffered reader so br1 is unecessary.
    > a= (int) br.readLine();
    > ...
    > b= (int) br.readLine();
    >
    > I highly suggest you use the Java API and a Java Tutorial, both available
    > for free from Sun's website.


    I think you made the wrong assumption of the posted code
    actually compiling.

    Cast from String to int does not compile.

    Arne
    Arne Vajhøj, Dec 28, 2007
    #3
  4. pandit

    Roedy Green Guest

    On Fri, 28 Dec 2007 03:44:25 -0800 (PST), pandit <>
    wrote, quoted or indirectly quoted someone who said :

    >a=(int)br.read();


    you need to readLines Strings. The convert the Strings to ints.

    Java conversion is more complicated than French irregular verbs. You
    can't just use a cast as you could in a more programmer friendly
    language. See
    http://mindprod.com/applet/converter.html
    for how to convert String to int.
    --
    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
    Roedy Green, Dec 28, 2007
    #4
  5. pandit

    Mark Space Guest

    Roedy Green wrote:

    > Java conversion is more complicated than French irregular verbs. You
    > can't just use a cast as you could in a more programmer friendly
    > language. See


    I don't think you can cast *strings* in any language and get
    intelligible integers.
    Mark Space, Dec 28, 2007
    #5
  6. pandit

    Mark Space Guest

    pandit wrote:

    > BufferedReader br = new BufferedReader(new
    > InputStreamReader(System.in));


    > System.out.print("\t\tPlease Enter Value For A:- ");
    > int a,b,c;
    > a=(int)br.read();


    Oh, and I forgot to add:

    1. Yes, learn to read the javadoc.

    2. Use readLine(), then Integer.parseInt( string ) is one way of doing it.
    Mark Space, Dec 28, 2007
    #6
  7. Mark Space wrote:
    > Roedy Green wrote:
    >
    >> Java conversion is more complicated than French irregular verbs. You
    >> can't just use a cast as you could in a more programmer friendly
    >> language. See

    >
    > I don't think you can cast *strings* in any language and get
    > intelligible integers.


    SQL
    Mark Thornton, Dec 28, 2007
    #7
  8. Mark Space <> writes:

    > I don't think you can cast *strings* in any language and get
    > intelligible integers.


    Perhaps not cast, but doing less will get the same result:
    Perl doesn't use casts at all, it just converts as it pleases:

    "14" * 3 == 42

    JavaScript is the same:

    "14" * 3 == 42

    /L
    --
    Lasse Reichstein Nielsen -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
    Lasse Reichstein Nielsen, Dec 28, 2007
    #8
  9. Mark Space wrote:
    > Roedy Green wrote:
    >> Java conversion is more complicated than French irregular verbs. You
    >> can't just use a cast as you could in a more programmer friendly
    >> language. See


    > I don't think you can cast *strings* in any language and get
    > intelligible integers.


    You can in PL/I (where, with 40 years of hindsight, it is now regarded
    as a design error), and, of course, in most languages of the REXX/Perl type.

    --
    John W. Kennedy
    "Sweet, was Christ crucified to create this chat?"
    -- Charles Williams. "Judgement at Chelmsford"
    John W. Kennedy, Dec 29, 2007
    #9
  10. pandit

    Silfax Guest

    On Sat, 29 Dec 2007 00:20:11 +0100, Lasse Reichstein Nielsen regurgitated
    the following

    > Mark Space <> writes:
    >
    >> I don't think you can cast *strings* in any language and get
    >> intelligible integers.

    >
    > Perhaps not cast, but doing less will get the same result:
    > Perl doesn't use casts at all, it just converts as it pleases:
    >
    > "14" * 3 == 42
    >
    > JavaScript is the same:
    >
    > "14" * 3 == 42
    >


    as does rexx also
    python returns a more sensible 141414 in this case.
    Silfax, Dec 29, 2007
    #10
  11. pandit

    Arne Vajhøj Guest

    Silfax wrote:
    > On Sat, 29 Dec 2007 00:20:11 +0100, Lasse Reichstein Nielsen regurgitated
    > the following
    >> Mark Space <> writes:
    >>> I don't think you can cast *strings* in any language and get
    >>> intelligible integers.

    >> Perhaps not cast, but doing less will get the same result:
    >> Perl doesn't use casts at all, it just converts as it pleases:
    >>
    >> "14" * 3 == 42
    >>
    >> JavaScript is the same:
    >>
    >> "14" * 3 == 42

    >
    > as does rexx also
    > python returns a more sensible 141414 in this case.


    I am not sure everyone agrees on that being "sensible" ...

    Arne
    Arne Vajhøj, Dec 29, 2007
    #11
  12. Arne Vajhøj wrote:
    > Silfax wrote:
    >>> "14" * 3 == 42

    >>
    >> as does rexx also
    >> python returns a more sensible 141414 in this case.

    >
    > I am not sure everyone agrees on that being "sensible" ...
    >

    Right, but they should! ;)
    (Note that the 141414 should be "141414" there.)

    Because, well, when I read

    "14" * 3

    it looks to me like someone wants the string "14" three times. OK, one
    could argue that a list or tuple of three individual "14"-strings is
    even more sensible, but I can not see much use for that.

    Probably just my yearlong exposure to python as my first real language
    speaking, of course ;).

    /W
    Wildemar Wildenburger, Dec 29, 2007
    #12
  13. pandit

    Lew Guest

    Wildemar Wildenburger wrote:
    > Arne Vajhøj wrote:
    >> Silfax wrote:
    >>>> "14" * 3 == 42
    >>>
    >>> as does rexx also
    >>> python returns a more sensible 141414 in this case.

    >>
    >> I am not sure everyone agrees on that being "sensible" ...
    >>

    > Right, but they should! ;)
    > (Note that the 141414 should be "141414" there.)
    >
    > Because, well, when I read
    >
    > "14" * 3
    >
    > it looks to me like someone wants the string "14" three times. OK, one
    > could argue that a list or tuple of three individual "14"-strings is
    > even more sensible, but I can not see much use for that.
    >
    > Probably just my yearlong exposure to python as my first real language
    > speaking, of course ;).


    The curse of dynamic typing meets the geas of the overloaded operator.

    There is no cross-language standard for the meaning of "*" where the operands
    are strings. There is no cross-language standard for which operator gets
    promoted when a binary operation involves both an int and a string.

    One of the reasons that Java is strongly typed is to make such things
    (painfully) explicit.

    Each language gets to decide about these things. Java leaves it up to the
    individual method, such as PrintStream.println(), to decide how to interpret
    its arguments, and inbuilds just a little mechanism into Object, such as
    toString(), to help things along.

    --
    Lew
    Lew, Dec 29, 2007
    #13
  14. pandit

    Wayne Guest

    Wildemar Wildenburger wrote:
    > Arne Vajhøj wrote:
    >> Silfax wrote:
    >>>> "14" * 3 == 42
    >>>
    >>> as does rexx also
    >>> python returns a more sensible 141414 in this case.

    >>
    >> I am not sure everyone agrees on that being "sensible" ...
    >>

    > Right, but they should! ;)
    > (Note that the 141414 should be "141414" there.)
    >
    > Because, well, when I read
    >
    > "14" * 3
    >
    > it looks to me like someone wants the string "14" three times. OK, one
    > could argue that a list or tuple of three individual "14"-strings is
    > even more sensible, but I can not see much use for that.
    >
    > Probably just my yearlong exposure to python as my first real language
    > speaking, of course ;).
    >
    > /W


    Perhaps this is why Perl uses a different operator for repetition ("x")
    and for multiplication, rather than a confusing overloaded "*":

    "14" x 3 == "141414"
    "14" * 3 == 42

    It's hard to argue with Perl syntax as it was developed by
    a linguist. :) Personally I find using a letter as an
    operator confusing, as I don't write a lot of Perl, and prefer:

    "14" * 3

    to cause an error. I guess the Java designers felt the same way!

    -Wayne
    Wayne, Dec 29, 2007
    #14
  15. pandit

    Mark Space Guest

    Silfax wrote:
    > On Sat, 29 Dec 2007 00:20:11 +0100, Lasse Reichstein Nielsen regurgitated
    > the following
    >
    >> Mark Space <> writes:
    >>
    >>> I don't think you can cast *strings* in any language and get
    >>> intelligible integers.

    >> Perhaps not cast, but doing less will get the same result:
    >> Perl doesn't use casts at all, it just converts as it pleases:
    >>
    >> "14" * 3 == 42
    >>
    >> JavaScript is the same:
    >>
    >> "14" * 3 == 42
    >>

    >
    > as does rexx also
    > python returns a more sensible 141414 in this case.
    >


    Right. Exactly. My point.
    Mark Space, Dec 29, 2007
    #15
  16. pandit

    Hal Rosser Guest

    "pandit" <> wrote in message
    news:...
    > Hello all,
    > thanks for solving my Old Problem now i m posting my problem here.
    > The Problem is that i m using "User Input" BufferReader and i m
    > getting input fro user
    > but when i m run this program this don't return me integer value here
    > i m doing addition of two integer but its not return me return value
    > wht's wrong with my Code
    >
    > import java.io.*;
    > class inputread
    > {
    > public static void main(String args[]) throws IOException
    > {
    >
    > BufferedReader br = new BufferedReader(new
    > InputStreamReader(System.in));
    > BufferedReader br1= new BufferedReader(new
    > InputStreamReader(System.in));
    > System.out.print("\t\tPlease Enter Value For A:- ");
    > int a,b,c;
    > a=(int)br.read();
    > System.out.print("\t\tPlease Enter Value For B:- ");
    > b=(int)br1.read();
    > c=a+b;
    > System.out.println("\t\tThe Sum Of A + B :- "+c);
    > }
    >
    > if i m presing enter two times
    > it is giving me 26
    > 13 for the Enter Value
    > now What i do
    > Thanks in Advance.


    Take a look at the Scanner class, it may be of help to you.
    Hal Rosser, Dec 29, 2007
    #16
  17. pandit

    Roedy Green Guest

    On Fri, 28 Dec 2007 20:28:01 -0500, Arne Vajhøj <>
    wrote, quoted or indirectly quoted someone who said :

    >
    >I am not sure everyone agrees on that being "sensible"


    Why have this goofy highly irregular way of invoking conversions?
    valueOf, intValueOf, new XXX( ), Integer.parseInt etc etc.


    Some you do with a simple (<type>). Why not all of them to
    interconvert all the basic types?

    Learning all those chaotically named methods is one of the most
    infuriating things when learning Java.
    ..
    --
    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
    Roedy Green, Dec 29, 2007
    #17
  18. pandit

    Arne Vajhøj Guest

    Roedy Green wrote:
    > On Fri, 28 Dec 2007 20:28:01 -0500, Arne Vajhøj <>
    > wrote, quoted or indirectly quoted someone who said :
    >> I am not sure everyone agrees on that being "

    >
    > Why have this goofy highly irregular way of invoking conversions?
    > valueOf, intValueOf, new XXX( ), Integer.parseInt etc etc.
    >
    > Some you do with a simple (<type>). Why not all of them to
    > interconvert all the basic types?


    Hm.

    That is rather unrelated to what I expressed doubts
    about whether everyone would consider "sensible".

    Arne
    Arne Vajhøj, Dec 29, 2007
    #18
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Spendius
    Replies:
    16
    Views:
    21,498
    Jon Skeet
    Sep 8, 2003
  2. Azmie
    Replies:
    12
    Views:
    1,066,445
    Azmie
    Dec 30, 2003
  3. nick
    Replies:
    0
    Views:
    881
  4. nick
    Replies:
    1
    Views:
    31,696
    Eric Sosman
    Oct 26, 2004
  5. les ander
    Replies:
    4
    Views:
    861
    wes weston
    Oct 5, 2004
Loading...

Share This Page