Java and numerical user input from keyboard

  • Thread starter Thread starter Matrixhasu77
  • 8 comments
  • 730 views
Messages
2,491
So I'm currently working on a Java program and I've ran into a snag. My problem is as follows... I want to take a number that has been input from the keyboard and assign it to a variable to use that variable to compute something else in another method (of double data type). What I need to do is to take keyboard input and basically convert it to Java's double data type.

However, the current form of the method seems to return some number (not entered by the user/me) everytime regardless of what is input for some reason. Help would be greatly appreciated as I am not very skilled at doing any sort of user/file input right now.

This is what I have so far (modified from another function in my program that does work):

Code:
import java.io.*;
class User {
public double inputAmt() {
        DataInputStream inputCommand = new DataInputStream(System.in);
        double input = 0;
            try {
                input =  (double) inputCommand.read();
            } catch(IOException ioe) {
                System.out.println(ioe.getMessage());
            }
            return input;
        }
}
 
Hmm .. Its been quite a while since I actually programmed Java, but I think its got something to do with using stream instead of just directly reading from System.in.

Other than that I'm not sure what to tell you.
 
I solved this issue on my own. Thanks to anyone who did happen to look at the thread though. I came up with the following solution to the problem.
Code:
public double inputAmt() {
	double input = 0;
	Scanner keyboard = new Scanner(System.in);
	try {
	input = keyboard.nextDouble();
	} catch (InputMismatchException ime){
	System.out.println("Invalid Input. Please enter another amount.");
	input = this.inputAmt();
	}
	return input;
	}
 
Ah .. good one. Never thought about using the Scanner class.

👍
 
VTGT07
Ah .. good one. Never thought about using the Scanner class.

👍

Yeah, this works much more effectively than a stream. I have one more problem with my program, but it's down to the logic that I've written. I'm not quite sure I have it right. I'll get it eventually though.
 
I wish I had cought this thread earlier.

The best way to do this is to use a BufferedReader:
Code:
//  open up standard input 
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Then store the input in a string, and use the double static method parseDouble(String s) to geta double value.
Code:
String s = br.readLine();
double d = Double.parseDouble(s);
Of course, there is no need for the intermediate:
Code:
double d = Double.parseDouble(br.readLine());
I didn't address error handling here, but I'm sure oyu can take care of that :) I bit late I realize but maybe this will be helpful next time!
 
skip0110
I wish I had cought this thread earlier.

The best way to do this is to use a BufferedReader:
Code:
//  open up standard input 
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Then store the input in a string, and use the double static method parseDouble(String s) to geta double value.
Code:
String s = br.readLine();
double d = Double.parseDouble(s);
Of course, there is no need for the intermediate:
Code:
double d = Double.parseDouble(br.readLine());
I didn't address error handling here, but I'm sure oyu can take care of that :) I bit late I realize but maybe this will be helpful next time!

I was doing something similar in my first post before I discovered the Scanner class. The scanner class solution I posted actually is a lot easier, IMO. It took me no less than 5 minutes to get working an implemented in my program. Thanks for your suggestion though!
 
Back