Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / December 2007

Tip: Looking for answers? Try searching our database.

Cannot Convert input into  integer

Thread view: 
pandit - 28 Dec 2007 11:44 GMT
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.
James - 28 Dec 2007 12:40 GMT
> Hello all,
> thanks for solving my Old Problem  now i m posting my problem here. The
[quoted text clipped - 16 lines]
>     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); }
[quoted text clipped - 4 lines]
> 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)....
Signature

James
*Note: Remove every other letter for correct email address

Arne Vajhøj - 28 Dec 2007 13:35 GMT
> You only need the one buffered reader so br1 is unecessary.
> a= (int) br.readLine();
[quoted text clipped - 3 lines]
> 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
Roedy Green - 28 Dec 2007 13:43 GMT
>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.
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Mark Space - 28 Dec 2007 16:35 GMT
> 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 Thornton - 28 Dec 2007 17:53 GMT
>> 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
Lasse Reichstein Nielsen - 28 Dec 2007 23:19 GMT
> 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
Signature

Lasse Reichstein Nielsen  -  lrn@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
 'Faith without judgement merely degrades the spirit divine.'

Silfax - 29 Dec 2007 01:13 GMT
On Sat, 29 Dec 2007 00:20:11 +0100, Lasse Reichstein Nielsen regurgitated
the following

>> I don't think you can cast *strings* in any language and get
>> intelligible integers.
[quoted text clipped - 7 lines]
>
>   "14" * 3 == 42

as does rexx also
python returns a more sensible 141414 in this case.
Arne Vajhøj - 29 Dec 2007 01:28 GMT
> On Sat, 29 Dec 2007 00:20:11 +0100, Lasse Reichstein Nielsen regurgitated
> the following
[quoted text clipped - 11 lines]
> as does rexx also
> python returns a more sensible 141414 in this case.

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

Arne
Wildemar Wildenburger - 29 Dec 2007 01:53 GMT
>>>   "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
Lew - 29 Dec 2007 02:17 GMT
>>>>   "14" * 3 == 42
>>>
[quoted text clipped - 16 lines]
> 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.

Signature

Lew

Wayne - 29 Dec 2007 02:38 GMT
>>>>   "14" * 3 == 42
>>>
[quoted text clipped - 18 lines]
>
> /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
Roedy Green - 29 Dec 2007 05:02 GMT
>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.
.
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Arne Vajhøj - 29 Dec 2007 05:06 GMT
>> I am not sure everyone agrees on that being "
>
[quoted text clipped - 3 lines]
> 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
Mark Space - 29 Dec 2007 03:40 GMT
> On Sat, 29 Dec 2007 00:20:11 +0100, Lasse Reichstein Nielsen regurgitated
> the following
[quoted text clipped - 12 lines]
> as does rexx also
> python returns a more sensible 141414 in this case.

Right. Exactly.  My point.
John W. Kennedy - 29 Dec 2007 00:00 GMT
>> 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.

Signature

John W. Kennedy
"Sweet, was Christ crucified to create this chat?"
  -- Charles Williams.  "Judgement at Chelmsford"

Mark Space - 28 Dec 2007 16:38 GMT
>         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.
Hal Rosser - 29 Dec 2007 03:57 GMT
> Hello all,
> thanks for solving my Old Problem  now i m posting my problem here.
[quoted text clipped - 28 lines]
> now What i do
> Thanks in Advance.

Take a look at the Scanner class, it may be of help to you.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.