PDA

View Full Version : Java question - I need help with some code...



Hayes
March 18th, 2010, 07:23 PM
Here is my code.

/*
*Brandon Hayes
*CSC 119 Midterm Exam Project
*03.18.2010
*This program finds the taxes owed based on income, filing status and number of children.
*/

import static java.lang.System.out;
import java.util.Scanner;
import java.*;

public class MidTerm{
public static void main(String args[])
throws java.io.IOException {

Scanner stndIn = new Scanner (System.in);

String status;
double gi; // gi stands for Gross Income
double str = 0.20; //str stands for single tax rate
double mtr = 0.25; //mtr stands for married tax rate
double ctrl = 0.10; // ctrl stands for cohabiting tax rate low (<$10000)
double ctrm = 0.15; // cohabiting tax rate mid ($10000 - $40000)
double ctrh = 0.30; // cohabiting tax rate high (>$40000)
int children; // initializing number of children variable
char ch;

out.println("Enter 'q' to enter: ");
do {
out.println("Do not use dollar sign or comma for GrossIncome: ");
out.println("What is your Gross Income?: " );
gi = stndIn.nextDouble();
out.println("What is your filing status?: ");
out.println("Are you Single, Married or Cohabiting?:");
status = stndIn.nextLine();
out.println("How many children do you have?: ");
children = stndIn.nextInt();
if(status.equalsIgnoreCase("single")) {
out.println("Taxes to Pay: " + (gi * str));
}
else if(status.equalsIgnoreCase("married")){
out.println("Taxes to Pay: " + (gi * mtr));
}
else if(status.equalsIgnoreCase("cohabiting")) {
if(gi < 10000){
out.println("Taxes to Pay: " + (gi - (children * 1000) * ctrl));
}
else if(gi >= 10000){
if(gi <= 40000);
out.println("Taxes to pay: " + (gi - (children * 1000) * ctrm));
}
else out.println("Taxes to pay: " + (gi - (children * 1000) * ctrh));
}
while(ch != 'q');

}
}
}

Hayes
March 18th, 2010, 07:25 PM
ok, I didn't want to screw up what I had written... I am getting 3 error codes when I try and compile and I am at a loss.

They are:
while expected line 56
illegal start of expression line 58
reached end of file while parsing (got this one twice)line 58 and 59.'



I have come a long way since the last time I had questions, but I am lost. any suggestions on where to start?

Hayes
March 18th, 2010, 07:39 PM
Here is my code.

/*
*Brandon Hayes
*CSC 119 Midterm Exam Project
*03.18.2010
*This program finds the taxes owed based on income, filing status and number of children.
*/

import static java.lang.System.out;
import java.util.Scanner;
import java.*;

public class MidTerm{
public static void main(String args[])
throws java.io.IOException {

Scanner stndIn = new Scanner (System.in);

String status;
double gi; // gi stands for Gross Income
double str = 0.20; //str stands for single tax rate
double mtr = 0.25; //mtr stands for married tax rate
double ctrl = 0.10; // ctrl stands for cohabiting tax rate low (<$10000)
double ctrm = 0.15; // cohabiting tax rate mid ($10000 - $40000)
double ctrh = 0.30; // cohabiting tax rate high (>$40000)
int children; // initializing number of children variable
char ch;

out.println("Enter 'q' to enter: ");
do {
out.println("Do not use dollar sign or comma for GrossIncome: ");
out.println("What is your Gross Income?: " );
gi = stndIn.nextDouble();
out.println("What is your filing status?: ");
out.println("Are you Single, Married or Cohabiting?:");
status = stndIn.nextLine();
out.println("How many children do you have?: ");
children = stndIn.nextInt();
if(status.equalsIgnoreCase("single")) {
out.println("Taxes to Pay: " + (gi * str));
}
else if(status.equalsIgnoreCase("married")){
out.println("Taxes to Pay: " + (gi * mtr));
}
else if(status.equalsIgnoreCase("cohabiting")) {
if(gi < 10000){
out.println("Taxes to Pay: " + (gi - (children * 1000) * ctrl));
}
else if(gi >= 10000){
if(gi <= 40000);
out.println("Taxes to pay: " + (gi - (children * 1000) * ctrm));
}
else out.println("Taxes to pay: " + (gi - (children * 1000) * ctrh));
}
}
while(ch != 'q');


}
}

ok, I made this change and no I am getting a:
variable ch might not have been initialized ( on line 54) and that is the only error I am getting.

Hayes
March 18th, 2010, 08:57 PM
anybody?

oleblue
March 18th, 2010, 09:49 PM
I'm no expert in any way shape or form, but

Where you declare the char char ch;
do you not need to assign some value to it, like char ch = ' ' or '0'

So when you do the while not equal it has some value to compare to?

Just a total wild guess and may be way off base.

Hayes
March 18th, 2010, 10:34 PM
I worked on it for awhile and forgot to repost here...




/*
*Brandon Hayes
*CSC 119 Midterm Exam Project
*03.18.2010
*This program finds the taxes owed based on income, filing status and number of children.
*/

import static java.lang.System.out;
import java.util.Scanner;
import java.*;

public class MidTerm{
public static void main(String args[])
throws java.io.IOException {

Scanner stndIn = new Scanner (System.in);

String status;
double gi; // gi stands for Gross Income
double str = 0.20; //str stands for single tax rate
double mtr = 0.25; //mtr stands for married tax rate
double ctrl = 0.10; // ctrl stands for cohabiting tax rate low (<$10000)
double ctrm = 0.15; // cohabiting tax rate mid ($10000 - $40000)
double ctrh = 0.30; // cohabiting tax rate high (>$40000)
int children; // initializing number of children variable
char exit;

out.println("Enter 'q' to enter: ");
do {
out.println("Do not use dollar sign or comma for GrossIncome: ");
out.println("What is your Gross Income?: " );
gi = stndIn.nextDouble();
out.println("What is your filing status?: ");
out.println("Are you Single, Married or Cohabiting?:");
status = stndIn.nextLine();
out.println("How many children do you have?: ");
children = stndIn.nextInt();
if(status.equalsIgnoreCase("single")) {
out.println("Taxes to Pay: " + (gi - (children * 1000)* str));
}
else if(status.equalsIgnoreCase("married")){
out.println("Taxes to Pay: " + (gi - (children * 1000)* mtr));
}
else if(status.equalsIgnoreCase("cohabiting")) {
if(gi < 10000){
out.println("Taxes to Pay: " + (gi - (children * 1000) * ctrl));
}
else if(gi >= 10000){
if(gi <= 40000);
out.println("Taxes to pay: " + (gi - (children * 1000) * ctrm));
}
else out.println("Taxes to pay: " + (gi - (children * 1000) * ctrh));
}
ch = (char) System.in.read();
} while (exit != 'q');

} }


I added the line in red, and I get the same error. That line should have initialized 'ch' and it didn't... I am still at a loss.

Hayes
March 18th, 2010, 10:35 PM
This is the other program I wrote that I am using for comparison on the do/while statement.

/*
Brandon Hayes
Test Average
This program displays what to do for different temperatures*/

import java.*;
import static java.lang.System.out;
import java.util.Scanner;


public class TempFor{
public static void main(String args[])
throws java.io.IOException {

Scanner stdnIn = new Scanner(System.in);
int Temp;
char ch;

out.print("Enter 'q' to stop program: ");
do {
out.println("What is the Temperature?");
Temp = stdnIn.nextInt();
if (Temp >= 80){
out.println("Go Swimming");
}
else if (Temp >=70) {
if (Temp <= 79);
out.println("Go Play Tennis");
}
else if (Temp >= 60) {
if (Temp <=69);
out.println ("Go Play Basketball");
}
else if (Temp >= 40) {
if (Temp <= 59);
out.println ("Go Skiing");
}
else out.println ("Stay Home");
ch = (char) System.in.read();
} while (ch != 'q');

}
}

Hayes
March 18th, 2010, 10:36 PM
ok, I'm retarded. I moved a single } and it compiled with no problems...

Now when I run it I get another error:

Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:840)
at java.util.Scanner.next(Scanner.java:1461)
at java.util.Scanner.nextInt(Scanner.java:2091)
at java.util.Scanner.nextInt(Scanner.java:2050)
at MidTerm.main(MidTerm.java:38)


I get this error when I try and input single, married or cohabiting, and it ends the program there.

Hayes
March 18th, 2010, 10:56 PM
ok, This is my final code and it compiled and worked flawlessly.

/*
*Brandon Hayes
*CSC 119 Midterm Exam Project
*03.18.2010
*This program finds the taxes owed based on income, filing status and number of children.
*/

import static java.lang.System.out;
import java.util.Scanner;
import java.*;


public class MidTerm{
public static void main(String [] args)
throws java.io.IOException {

Scanner stndIn = new Scanner (System.in);

String st;
double gi; // gi stands for Gross Income
double str = 0.20; //str stands for single tax rate
double mtr = 0.25; //mtr stands for married tax rate
double ctrl = 0.10; // ctrl stands for cohabiting tax rate low (<$10000)
double ctrm = 0.15; // cohabiting tax rate mid ($10000 - $40000)
double ctrh = 0.30; // cohabiting tax rate high (>$40000)
int children; // initializing number of children variable
char ch;

out.println("Enter 'quit' to exit: ");
do {
out.println("Do not use dollar sign or comma for GrossIncome: ");
out.println("What is your Gross Income?: " );
gi = stndIn.nextDouble();
out.println("What is your filing status?: ");
out.println("Are you Single, Married or Cohabiting?:");
st = stndIn.next();
out.println("How many children do you have?: ");
children = stndIn.nextInt();
if(st.equalsIgnoreCase("single")) {
out.println("Taxes to Pay: " + ((gi - (children * 1000))* str));
}
else if(st.equalsIgnoreCase("married")){
out.println("Taxes to Pay: " + ((gi - (children * 1000))* mtr));
}
else if(st.equalsIgnoreCase("cohabiting")) {
if(gi < 10000){
out.println("Taxes to Pay: " + ((gi - (children * 1000)) * ctrl));
}
else if(gi >= 10000){
if(gi <= 40000);
out.println("Taxes to pay: " + ((gi - (children * 1000)) * ctrm));
}
else out.println("Taxes to pay: " + ((gi - (children * 1000)) * ctrh));}
ch = (char) System.in.read();
} while (ch != 'q');

}
}



Thanks for the help OleBlue.

oleblue
March 19th, 2010, 05:47 AM
Sorry I was not any real help. I have never done coding in Java and was just, like a said a guess. Last time I did any coding at all was 1981, that was in COBOL and RPG.

Glad you got it figured out.

Hayes
March 19th, 2010, 05:55 AM
It helped to read it and just think out loud. Thanks, seriously.

OrangeCrush
March 19th, 2010, 06:10 AM
glad we could help.

oleblue
March 19th, 2010, 06:51 AM
you're welcome.

ArloGuthroJeep
March 21st, 2010, 05:45 AM
Brandon what editor are you using to write the code? If you're just using a plain old text editor, consider downloading a program called Eclipse (download the one aimed towards java programming). It'll help with things like mismatched {}'s and other syntactical problems.

Hayes
March 21st, 2010, 08:49 AM
I am using JCreator, it came with my Java for dummies book. I am using it for now, but I do plan on going to Eclipse soon though.