• Post Reply Bookmark Topic Watch Topic
  • New Topic

Inputing 2 numeric values  RSS feed

 
Brad Dorff
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have to write a program for class that store 2 variables(numeric values) have them meet a condition and system.out.println("accept") or (reject), what am I doing wrong, see code below.
import javax.swing.*;
public class Admission
{
public static void main(String[] args) throws Exception
{
double gradePointAverage;
int admissionScore;
String see = "Accept";
String see1 = "Reject";

String response = JOptionPane.showInputDialog("Enter your Grade Point Average: ");
//At this time, the variable 'str_age' contains the input from the user in a form of string.
double grade = Integer.parseInt(response);

System.out.println("Enter your Grade Point Average and press Enter. ");
gradePointAverage = (double)System.in.read();

System.in.read();
System.in.read();
System.out.println("Enter your Admission Score and press Enter. ");
admissionScore = (int)System.in.read();

System.in.read();
System.in.read();
if (gradePointAverage >= '3')
if (admissionScore > 60)
System.out.println(see);
else
System.out.println(see1);
if(gradePointAverage < '3')
if(admissionScore > 80)
System.out.println(see);
else
System.out.println(see1);
System.out.println("Your Grade Point Average Entered is " + gradePointAverage);
System.out.println("Your Admission Score Entered is " + admissionScore);
}
}
 
jason adam
Chicken Farmer ()
Ranch Hand
Posts: 1932
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First, you're parsing the input with Integer but putting it into a double. Seem odd to you?
Also, System.in.read() takes in one character at a time, NOT the whole String. Personally, I like to wrap System.in inside a InputStreamReader, and then a BufferedReader, and use readLine(). And when you cast these inputs into ints or doubles, you're getting the ASCII representation of the input characters. It's not giving you the input value as an int. Hence why if you put in 3.0, you end up with gradePointAverage holding 51, since 51 is the numerical representation of the character '3'.
[ March 26, 2004: Message edited by: jason adam ]
 
Gowrishankar Mudaliar
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do as jason says.
BufferedReader bReader = new BufferedReader(new InputStreamReader(System.in));
use bReader.readLine(); to read lines an convert them back to the datatype that you want like an int or double.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!