Question for Java Programmers

AgaBoogaBoo

Lifer
Feb 16, 2003
26,108
5
81
I know this is in the wrong forum but it would help if I got a quick answer.

I'm writing a program asking for values, processing them, and then outputting a final result based upon all those. When the program asks for values (In the command prompt) and enters a value for each including 0, it works. But when the user just presses enter not including any value, it gives me this error:

Exception in thread "main" java.lang.NumberFormatException:
at java.lang.Integer.parseInt(Integer.java:447)
at java.lang.Integer.parseInt(Integer.java:476)
at Change.main(Change.java:60)
Press any key to continue . . .

I tried setting values for the variables the user gives values for before they are asked so they don't override the user's inputs, but that doesn't seem to help. Is there some code I can use which makes the program handle things not defined by the user as 0?
 

AgaBoogaBoo

Lifer
Feb 16, 2003
26,108
5
81
Also, when I do something like this:

System.out.println("How many quarters are there?");
strQuarters = dataIn.readLine();

Is there a way so I can have the program assign a value of 0 if no value is given?
 

Crusty

Lifer
Sep 30, 2001
12,684
2
81
It would be better to post the code in your main that you are using to read in and what not...its kinda hard to get a grasp of your problem without it.
 

AgaBoogaBoo

Lifer
Feb 16, 2003
26,108
5
81
Ok, here is the code. It has comments all over because this is for a CS class:

import java.io.*;

public class Change
{
public static void main(String[] args) throws IOException
{
BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));

//Declaring Variables - Doubles
int amtQuarters; //Number of Quarters
int amtDimes; //Number of Dimes
int amtNickels; //Number of Nickels
int amtPennies; //Number of Pennies
int amtDollars; //Total Dollars without Cents
double amtCents; //Number of Cents without Dollars
double valuePennie; //Dollar Value of Pennies
double valueQuarter; //Dollar Value of Quarters
double valueDime; //Dollar Value of Dimes
double valueNickel; //Dollar Value of Nickels
double totalamount; //Total Amount in dollars
double beforeround; //Total ammount times 100 before round
double round; //Rounded version of total
double afterround; //Rounded version in correct format

//Declaring Variables - Strings
String custName; //User's Name
String strQuarters; //Inputted Quarters
String strDimes; //Inputted Dimes
String strNickels; //Inputted Nickels
String strPennies; //Inputted Pennies

//Origional Values to prevent an error if no value is entered
amtPennies = 0;
amtNickels = 0;
amtDimes = 0;
amtQuarters = 0;

//Get Inputs from User
System.out.println("How many quarters are there?");
strQuarters = dataIn.readLine(); //Number of Quarters
System.out.println("How many dimes are there?");
strDimes = dataIn.readLine(); //Number of Dimes
System.out.println("How many nickels are there?");
strNickels = dataIn.readLine(); //Number of Nickels
System.out.println("How many pennies are there?");
strPennies = dataIn.readLine(); //Number of Pennies
System.out.println("\n"); //Empty line - Spacing

//Conversion
amtPennies = Integer.parseInt(strPennies); //Converts Int to Double for Pennies
amtNickels = Integer.parseInt(strNickels); //Converts Int to Double for Nickels
amtDimes = Integer.parseInt(strDimes); //Converts Int to Double for Dimes
amtQuarters = Integer.parseInt(strQuarters); //Converts Int to Double for Quarters

//Calculations

//Convert to Value in Dollars and total it
valuePennie = amtPennies*.01; //Pennies in Dollar Value
valueNickel = amtNickels*.05; //Nickels in Dollar Value
valueDime = amtDimes*.1; //Dimes in Dollar Value
valueQuarter = amtQuarters*.25; //Quarters in Dollar Value
totalamount = valuePennie + valueNickel + valueDime + valueQuarter;

//Round to 2 decimal places (cents)
beforeround = totalamount*100; //Move decimal spot over 2 space to right
round = Math.round(beforeround); //Round number
afterround = round/100; //Move decimal place back to spaces

//Post Calculation Conversions
amtDollars = (int)afterround;
amtCents = Math.round((afterround - amtDollars)*100);

//Output
System.out.print("You have " + amtPennies + " pennie(s), ");
System.out.print(amtNickels + " nickel(s), ");
System.out.print(amtDimes + " dime(s), and, ");
System.out.println(amtQuarters + " quarter(s).");
System.out.print("The total amount of change in dollars for you is ");
System.out.println(amtDollars + " dollar(s) and " + (int)amtCents + " cent(s)");
}//End Main
}//End Class Change
 

OulOat

Diamond Member
Aug 8, 2002
5,769
0
0
strQuarters = dataIn.readLine();

if (strQuarters == null)
{
strQuarters = "0";
}

edit ----
The previous code may solve your problem with the error, if not, then you would need

while (dataIn.readLine() == null)
{
}

strQuarters = dataIn.readLine();
 

wyvrn

Lifer
Feb 15, 2000
10,074
0
0
Use an if statement that checks to see if the input is valid.

For example

if (variablename <=0) {
System.out.println("Please enter a value greater than 0") }

That will check to see that they enter a value greater than 0.
 

AgaBoogaBoo

Lifer
Feb 16, 2003
26,108
5
81
Originally posted by: wyvrn
Use an if statement that checks to see if the input is valid.

For example

if (variablename <=0) {
System.out.println("Please enter a value greater than 0") }

That will check to see that they enter a value greater than 0.

0 can be used. It should be if there is no answer at all. How can I have the program check for negative signs and remove them if there are any?
 

OulOat

Diamond Member
Aug 8, 2002
5,769
0
0
Originally posted by: AgaBooga
Originally posted by: OulOat
strQuarters = dataIn.readLine();

if (strQuarters == null)
{
strQuarters = "0";
}

Where do I put that?

Right after...
strQuarters = dataIn.readLine(); //Number of Quarters
 

AgaBoogaBoo

Lifer
Feb 16, 2003
26,108
5
81
Originally posted by: OulOat
Originally posted by: AgaBooga
Originally posted by: OulOat
strQuarters = dataIn.readLine();

if (strQuarters == null)
{
strQuarters = "0";
}

Where do I put that?

Right after...
strQuarters = dataIn.readLine(); //Number of Quarters

Thanks! How about seeing if there is a negative number, and if there is asking the user to retype it?
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
I didn't read the whole thread:

try{
number = however you're reading input goes here;
}
catch(NumberFormatException e){
number = 0;
}
 

OulOat

Diamond Member
Aug 8, 2002
5,769
0
0
Originally posted by: AgaBooga
Originally posted by: wyvrn
Use an if statement that checks to see if the input is valid.

For example

if (variablename <=0) {
System.out.println("Please enter a value greater than 0") }

That will check to see that they enter a value greater than 0.

0 can be used. It should be if there is no answer at all. How can I have the program check for negative signs and remove them if there are any?

a) In the README, specify that negative values will not be accepted.

b)

StringTokenizer token = new StringTokenizer(strQuarters);
String quarterToken = token.nextToken();

if (quarterTokn.equalsIgnoreCase("-"))
{
strQuarters = token.nextToken();
}

//this is if they put a space inbetween - and amount

//else

String negCheck = new String (strQuarters.substring(0, 1));
if (negCheck.equalsIgnoreCase("-"))
{
strQuarters = strQuarters.substring(1);
}
 

OulOat

Diamond Member
Aug 8, 2002
5,769
0
0
Originally posted by: AgaBooga
Originally posted by: OulOat
Originally posted by: AgaBooga
Originally posted by: OulOat
strQuarters = dataIn.readLine();

if (strQuarters == null)
{
strQuarters = "0";
}

Where do I put that?

Right after...
strQuarters = dataIn.readLine(); //Number of Quarters

Thanks! How about seeing if there is a negative number, and if there is asking the user to retype it?

My above code just accepts the abs value of the neg number. Change it if you like
 

RaynorWolfcastle

Diamond Member
Feb 8, 2001
8,968
16
81
Originally posted by: wyvrn
Use an if statement that checks to see if the input is valid.

For example

if (variablename <=0) {
System.out.println("Please enter a value greater than 0") }

That will check to see that they enter a value greater than 0.

actually here's a generic way to check whether the user entered a number >= 0

bool goodInput = false;
int aNumber;

while(!goodInput) {
... System.out.println("Enter a number here: ");
... try {
........... aNumber = Integer.parseInt(dataIn.readLine());
............if(aNumber < 0) {throw new Exception("enter a message here if you like");}
........... goodInput = true;
....}
....catch (Exception e) {
........... System.out.println("\n Enter a valid number you fuggin idiot\n");
....}
}

this loop keeps asking the user until he stops being an idiot and actually inputs a value that you like and you can add whatever conditions you like... so you could use it for validating strings or anything else if you want. You just have to change what is in the try/catch statement
 

Evadman

Administrator Emeritus<br>Elite Member
Feb 18, 2001
30,990
5
81
The problem is by just hitting enter the variable is being set as "Null" and this is causing the error. Null is not a number. notfred's should work just fine.
 

AgaBoogaBoo

Lifer
Feb 16, 2003
26,108
5
81
Originally posted by: OulOat
strQuarters = dataIn.readLine();

if (strQuarters == null)
{
strQuarters = "0";
}

Ok, that worked like a charm. Now, is there a simpler way to check if the number is a negative? Maybe like if the user put -9, the program just removes the negative sign? I don't want to mess with absolute value since we haven't done anything like this yet. My teacher is picky, and so I have to do things that we haven't learned to get an A in the class like this. Thanks Again!

Also, what if the user enters like "3 pennies"? Is there a way to have the program only capture the number? That could solve the problem for the negative sign and anything else they type. He mentioned this once in my class and he will probably want this before he gives out an A. Maybe a function or something so that the program scans the input, and looks for only a number. It should be able to accept double and triple and so on of digits *As long as* they are right next to each other, otherwise he could enter 3 pennies 25789546 and the program would think the number of pennies is 325789546.
 

dighn

Lifer
Aug 12, 2001
22,820
4
81
Ok, that worked like a charm. Now, is there a simpler way to check if the number is a negative? Maybe like if the user put -9, the program just removes the negative sign? I don't want to mess with absolute value since we haven't done anything like this yet. My teacher is picky, and so I have to do things that we haven't learned to get an A in the class like this. Thanks Again!

if(number < 0)number = -number;

Also, what if the user enters like "3 pennies"? Is there a way to have the program only capture the number? That could solve the problem for the negative sign and anything else they type. He mentioned this once in my class and he will probably want this before he gives out an A. Maybe a function or something so that the program scans the input, and looks for only a number. It should be able to accept double and triple and so on of digits *As long as* they are right next to each other, otherwise he could enter 3 pennies 25789546 and the program would think the number of pennies is 325789546.

there are a number of ways of doing this. the simplest way that i can think of right now (not necesarily the best) is to scan the input character by character and append any numerical characters to a new string.
 

Evadman

Administrator Emeritus<br>Elite Member
Feb 18, 2001
30,990
5
81
Originally posted by: AgaBooga
Also, what if the user enters like "3 pennies"? Is there a way to have the program only capture the number?

That would be Val in Visual basic. It returns the value of the variable. if variable = "3 pennies" val(variable) = 3
not sure on the java coding.

Just put in a further check that looks for numbers <0.
if <0 then msgbox ("you is doufus. no neg numbers")
Howver you do that in java :p
 

RaynorWolfcastle

Diamond Member
Feb 8, 2001
8,968
16
81
you'd have to tokenize the string and check the first caracter of every token to find a number. I believe Unicode is isomorphic so you could just use something along the lines of

if(yourChar >= '0' && yourChar =< '9') {
enter handling code here
}

piece of cake ;)

*edit* that would handle "3 pennies" but not "3pennies"... in that case you'd need something more sophisticated and it could get a little messy since you'd have ot check the input character by character
 

OulOat

Diamond Member
Aug 8, 2002
5,769
0
0
Originally posted by: AgaBooga
Originally posted by: OulOat
strQuarters = dataIn.readLine();

if (strQuarters == null)
{
strQuarters = "0";
}

Ok, that worked like a charm. Now, is there a simpler way to check if the number is a negative? Maybe like if the user put -9, the program just removes the negative sign? I don't want to mess with absolute value since we haven't done anything like this yet. My teacher is picky, and so I have to do things that we haven't learned to get an A in the class like this. Thanks Again!

.......................................
.......................................
.......................................
.......................................
.......................................
.......................................
I have nothing else to say besides look at my code again and try to understand how the code inside the if statement works.
.......................................
.......................................
.......................................
.......................................
.......................................
.......................................
Also, what if the user enters like "3 pennies"? Is there a way to have the program only capture the number? That could solve the problem for the negative sign and anything else they type. He mentioned this once in my class and he will probably want this before he gives out an A. Maybe a function or something so that the program scans the input, and looks for only a number. It should be able to accept double and triple and so on of digits *As long as* they are right next to each other, otherwise he could enter 3 pennies 25789546 and the program would think the number of pennies is 325789546.

Google. "java api" First link. All the classes and their methods

Look for Stringtokenizer. I already used that in my code. You should be able to parse out the int with a few modifications to my code, as long as they don't enter "pennies 3." You should specify your conditions for input in the README file.
 

AgaBoogaBoo

Lifer
Feb 16, 2003
26,108
5
81
Originally posted by: dighn
Ok, that worked like a charm. Now, is there a simpler way to check if the number is a negative? Maybe like if the user put -9, the program just removes the negative sign? I don't want to mess with absolute value since we haven't done anything like this yet. My teacher is picky, and so I have to do things that we haven't learned to get an A in the class like this. Thanks Again!

if(number < 0)number = -number;

When I added that I encountered two errors. One was:

operator < cannot be applied to java.lang.String,int

The other is:

operator - cannot be applied to java.lang.String

Thanks again guys, this help is really helping me out, hehe
 

AgaBoogaBoo

Lifer
Feb 16, 2003
26,108
5
81
Originally posted by: OulOat
Originally posted by: AgaBooga
Originally posted by: OulOat
strQuarters = dataIn.readLine();

if (strQuarters == null)
{
strQuarters = "0";
}

Ok, that worked like a charm. Now, is there a simpler way to check if the number is a negative? Maybe like if the user put -9, the program just removes the negative sign? I don't want to mess with absolute value since we haven't done anything like this yet. My teacher is picky, and so I have to do things that we haven't learned to get an A in the class like this. Thanks Again!

.......................................
.......................................
.......................................
.......................................
.......................................
.......................................
I have nothing else to say besides look at my code again and try to understand how the code inside the if statement works.
.......................................
.......................................
.......................................
.......................................
.......................................
.......................................
Also, what if the user enters like "3 pennies"? Is there a way to have the program only capture the number? That could solve the problem for the negative sign and anything else they type. He mentioned this once in my class and he will probably want this before he gives out an A. Maybe a function or something so that the program scans the input, and looks for only a number. It should be able to accept double and triple and so on of digits *As long as* they are right next to each other, otherwise he could enter 3 pennies 25789546 and the program would think the number of pennies is 325789546.

Google. "java api" First link. All the classes and their methods

Look for Stringtokenizer. I already used that in my code. You should be able to parse out the int with a few modifications to my code, as long as they don't enter "pennies 3." You should specify your conditions for input in the README file.

Ok. I'll check that out. I would do a readme, but right now we are limited to using only one file for it.