• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Inputing 2 numeric values

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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);
}
}
 
Chicken Farmer ()
Posts: 1932
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
For my next trick, I'll need the help of a tiny ad ...
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic