Hi,

I want to read an integer value from console, but my input returns the ASCII value of integer, Please Help me.

import java.io.*;

class Test{
	
	public static void main(String args[])throws IOException{
		
		BufferedInputStream br=new BufferedInputStream(System.in);
		
		System.out.println("Enter an Integer");
		
		int i=br.read();
		//int j=(int)i;
		
		System.out.print(i);
	}
}

Thanks

Recommended Answers

All 10 Replies

it's being read as a "char" so either cast it to an int, or read an "int" instead.

Here are a few examples on alternatives how to read Input from a Console :- 1,2,3.

Considering your case you might especially like the third link.

solution one pre Java 5:

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

System.out.println("Enter an Integer");

String value = in.readLine();     // this line may throw IOException
int i = Integer.parseInt(value);

Solution two Java 5 and higher

import java.util.Scanner // import this class first

Scanner input = new Scanner(System.in);
System.out.println("Enter an Integer");
int i = input.nextInt();

make your pick

@ejosiah
Both those options were already covered in my post.

@stephen84s
If you don't want to answer the poor guys question then don't rather than posting links with little relevance to any of us

@stephen84s
posting links with little relevance to any of us

Sorry to intrude, but what do you mean by that?

@stephen84s

Sorry dude I confused your footnote for your actuall post.

@stephen84s sorry dude I confused your footnote for your actual post my bad.

I should probably look properly nextime

When you say: "little relevance to any of us".

But probably I misunderstood you, so never mind. Besides the OP already got enough help to solve his/her problem. Continuing this discussion might be meaningless.

Sorry if my attitude sound somehow aggressive.

@ajvaAddict
A little misunderstanding there buddy. I see your avater is a pic of ghost in the shell; you into animies in any way.

Hola back

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.