Results 1 to 15 of 15

Thread: Java question - I need help with some code...

  1. #1
    Hayes's Avatar
    Join Date
    Feb 2006
    Location
    Aurora, CO
    Member #
    8575

    Java question - I need help with some code...

    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');

    }
    }
    }
    “Laws that forbid the carrying of arms… disarm only those who are neither inclined nor determined to commit crimes… Such laws make things worse for the assaulted and better for the assailants…” — Thomas Jefferson in “Commonplace Book,” 1774-1776

  2. #2
    Hayes's Avatar
    Join Date
    Feb 2006
    Location
    Aurora, CO
    Member #
    8575
    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?

  3. #3
    Hayes's Avatar
    Join Date
    Feb 2006
    Location
    Aurora, CO
    Member #
    8575
    Quote Originally Posted by Hayes View Post
    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.

  4. #4
    Hayes's Avatar
    Join Date
    Feb 2006
    Location
    Aurora, CO
    Member #
    8575
    anybody?

  5. #5
    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.
    It is not the end. It is not the beginning of the end. It is the end of the beginning.

  6. #6
    Hayes's Avatar
    Join Date
    Feb 2006
    Location
    Aurora, CO
    Member #
    8575
    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.

  7. #7
    Hayes's Avatar
    Join Date
    Feb 2006
    Location
    Aurora, CO
    Member #
    8575
    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');

    }
    }

  8. #8
    Hayes's Avatar
    Join Date
    Feb 2006
    Location
    Aurora, CO
    Member #
    8575
    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.

  9. #9
    Hayes's Avatar
    Join Date
    Feb 2006
    Location
    Aurora, CO
    Member #
    8575
    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.

  10. #10
    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.

  11. #11
    Hayes's Avatar
    Join Date
    Feb 2006
    Location
    Aurora, CO
    Member #
    8575
    It helped to read it and just think out loud. Thanks, seriously.

  12. #12

    Join Date
    Jul 2006
    Location
    Pensacola, FL
    Member #
    9903
    glad we could help.

  13. #13
    you're welcome.

  14. #14
    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.
    '04 WJ 4.0L @ 6" on Clayton LA's & 35" BFG MT KM2's - [center][url=www.rockfrogs.org][COLOR=DarkGreen][B]Rock Frogs![/B][/COLOR][/url] - [url=http://www.treadlightly.org][COLOR="DarkGreen"][B]Tread Lightly[/B][/COLOR][/url] - [url=http://www.staythetrail.org][COLOR="DarkGreen"][B]Stay The Trail[/B][/COLOR][/url][/center]

  15. #15
    Hayes's Avatar
    Join Date
    Feb 2006
    Location
    Aurora, CO
    Member #
    8575
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •