Cannot Convert input into integer

P

pandit

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

James

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

Arne Vajhøj

James said:
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
 
M

Mark Space

Roedy said:
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.
 
M

Mark Space

pandit said:
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.
 
L

Lasse Reichstein Nielsen

Mark Space said:
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
 
J

John W. Kennedy

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

Silfax

On Sat, 29 Dec 2007 00:20:11 +0100, Lasse Reichstein Nielsen regurgitated
the following
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.
 
A

Arne Vajhøj

Silfax said:
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.[/QUOTE]

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

Arne
 
W

Wildemar Wildenburger

Arne said:
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
 
L

Lew

Wildemar said:
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.
 
W

Wayne

Wildemar said:
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
 
M

Mark Space

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


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

Right. Exactly. My point.
 
H

Hal Rosser

pandit said:
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.
 
R

Roedy Green

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

Arne Vajhøj

Roedy said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Staff online

Members online

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,054
Latest member
LucyCarper

Latest Threads

Top