Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 13 of 13

Thread: Gas Calculator

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    29
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Gas Calculator

    I am trying to figure out if what I am doing is possible or if I have something wrong. The bolded part is where my compiler DrJava keeps giving me an error. Near as I can tell I am trying to output two different types while the decimal format is asking for only one. I am trying to get it to tell me how much the cost for how many miles. Cost being double and miles being an int. Or is something else wrong.

    Thanks

    import java.io.*;
    import java.util.*;
    import javax.swing.JOptionPane;
    import java.text.DecimalFormat;

    public class gascalculator
    {
    public static void main(String[] args) throws IOException
    {
    int travel, auto, miles;
    double fuelcost, Cost;

    travel = getMiles();
    auto = getVehicle();
    fuelcost = getFuelcost;
    output(fuelcost, miles);
    finish();
    }

    public static int getMiles()
    {
    int travel = 0;

    String answer = JOptionPane.showInputDialog(null, "Enter the amount of miles you plan to travel\n(do not use commas) or click Cancel to exit:");

    if (answer == null) finish();

    try
    {
    travel = Integer.parseInt(answer);
    if (travel <=0) throw new NumberFormatException();
    }
    catch(NumberFormatException e)
    {
    JOptionPane.showMessageDialog(null, "Your entry was not in the proper format.", "Error", JOptionPane.INFORMATION_MESSAGE);
    }

    return travel;
    }

    public static int getVehicle()
    {
    int auto = 0;
    boolean done = false;

    while(!done)
    {
    try
    {
    String message = "Enter the vehicle type:" + "\n\n1 ) Compact\n2) Sedan\n3) Suv\n\n";

    auto = Integer.parseInt(JOptionPane.showInputDialog(null, message));
    if (auto<1 || auto>3) throw new NumberFormatException();
    else done = true;
    }
    catch(NumberFormatException e)
    {
    JOptionPane.showMessageDialog(null, "Please enter a 1, 2, or 3.", "Error",JOptionPane.INFORMATION_MESSAGE);
    }
    }
    return auto;
    }
    public static double getFuelcost(int travel, int auto)
    {
    double fuelcost = 0.0;

    switch(auto)
    {
    case 1:
    fuelcost = (2.50 *(travel/15)) + (travel/3000)*(15);
    break;

    case 2:
    fuelcost = ((travel/15) * 2.70 + (travel/3000)*(15));
    break;

    case 3:
    fuelcost = ((travel/15) * 2.90 + (travel/3000)*(15));
    break;
    }
    return fuelcost;
    }
    public static void output(fuelcost, miles);
    {
    DecimalFormat twoDigits = new DecimalFormat("$#,000.00");
    JOptionPane.showMessageDialog(null,"Your cost to travel is" + twoDigits.format(fuelcost) + " for " + twoDigitsformat(miles), "you travel".JOptionPane.INFORMATION_MESSAGE);
    }


    public static void finish()
    {
    System.exit(0);
    }

    }


  2. #2
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Gas Calculator

    If your compiler is telling you that you have a compilation error, you'll probably want to share this important bit of information with us, since the error messages are usually pretty self-explanatory and usually will tell you/us just what is wrong.

  3. #3
    Junior Member
    Join Date
    Nov 2012
    Posts
    29
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Gas Calculator

    Quote Originally Posted by curmudgeon View Post
    If your compiler is telling you that you have a compilation error, you'll probably want to share this important bit of information with us, since the error messages are usually pretty self-explanatory and usually will tell you/us just what is wrong.
    2 errors found:
    File: C:\Users\James Turnham\Desktop\gascalculator.java [line: 83]
    Error: <identifier> expected
    File: C:\Users\James Turnham\Desktop\gascalculator.java [line: 83]
    Error: <identifier> expected
    Java Error.JPG

  4. #4
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Gas Calculator

    Next obvious question: which line is 83?
    Then have a look at all of the semicolons you've used near the bad line.

  5. #5
    Junior Member
    Join Date
    Nov 2012
    Posts
    29
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Gas Calculator

    Quote Originally Posted by curmudgeon View Post
    Next obvious question: which line is 83?
    Then have a look at all of the semicolons you've used near the bad line.
    Line 83 is the line that i've bolded. Not sure about the semicolon's since they seem to be all correct unless i'm missing the obvious.

    Nice pic of Walter by the way.

  6. #6
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Gas Calculator

    Yes, you're missing the obvious:
    public static void output(fuelcost, miles);  // <===== *** check out this semicolon here. Does it belong?
    {
       DecimalFormat twoDigits = new DecimalFormat("$#,000.00");
       JOptionPane.showMessageDialog(null,"Your cost to travel is" + twoDigits.format(fuelcost) + " for " + twoDigitsformat(miles), "you travel".JOptionPane.INFORMATION_MESSAGE);
    }

  7. #7
    Junior Member
    Join Date
    Nov 2012
    Posts
    29
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Gas Calculator

    Quote Originally Posted by curmudgeon View Post
    Yes, you're missing the obvious:
    public static void output(fuelcost, miles);  // <===== *** check out this semicolon here. Does it belong?
    {
       DecimalFormat twoDigits = new DecimalFormat("$#,000.00");
       JOptionPane.showMessageDialog(null,"Your cost to travel is" + twoDigits.format(fuelcost) + " for " + twoDigitsformat(miles), "you travel".JOptionPane.INFORMATION_MESSAGE);
    }
    I've tried removing that and I still get the same error, the cursor though is right after fuelcost with the same error that it's expecting an identifier.

  8. #8
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Gas Calculator

    Ah, you're still not defining the method correctly. You will want to read up on how to declare methods that take parameters since the parameter variables need types as well as variable names. In other words, it should be something like,
    public static void output(double fuelcost, double miles) {  // or int if need be
       // .... stuff goes in here
    }

  9. #9
    Junior Member
    Join Date
    Nov 2012
    Posts
    29
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Gas Calculator

    Quote Originally Posted by curmudgeon View Post
    Ah, you're still not defining the method correctly. You will want to read up on how to declare methods that take parameters since the parameter variables need types as well as variable names. In other words, it should be something like,
    public static void output(double fuelcost, double miles) {  // or int if need be
       // .... stuff goes in here
    }
    I've done that and also turned the getFuelcost() into a method but now it's saying I can't have two (ints) for the method. I've already declared what the variables are so i'm puzzled why this isn't working.

    import java.io.*;
    import java.util.*;
    import javax.swing.JOptionPane;
    import java.text.DecimalFormat;
     
    public class gascalculator
    {
         public static void main(String[] args) throws IOException
         {
           int travel, auto, miles;
           double fuelcost, Cost;
     
           travel = getMiles();
           auto = getVehicle();
           fuelcost = getFuelcost(); // ***** error ****
           output(fuelcost, miles);
           finish();
         }
     
         public static int getMiles()
         {    
           int travel = 0;
     
           String answer = JOptionPane.showInputDialog(null, "Enter the amount of miles you plan to travel\n(do not use commas) or click Cancel to exit:");
     
           if (answer == null) finish();
     
           try 
           {
             travel = Integer.parseInt(answer);
             if (travel <=0) throw new NumberFormatException(); 
           }
            catch(NumberFormatException e)
           {
               JOptionPane.showMessageDialog(null, "Your entry was not in the proper format.", "Error", JOptionPane.INFORMATION_MESSAGE);
           }
     
           return travel;
        } 
     
         public static int getVehicle()
         {    
           int auto = 0;
           boolean done = false;
     
           while(!done)
           {  
             try 
             {
               String message = "Enter the vehicle type:" + "\n\n1 ) Compact\n2) Sedan\n3) Suv\n\n";
     
               auto = Integer.parseInt(JOptionPane.showInputDialog(null,message));
               if (auto<1 || auto>3) throw new NumberFormatException();
               else done = true;
             }
               catch(NumberFormatException e)
             {
                 JOptionPane.showMessageDialog(null, "Please enter a 1, 2, or 3.", "Error",JOptionPane.INFORMATION_MESSAGE);
             }
           }       
           return auto;
         }
         public static double getFuelcost(int travel, int auto)
         {
           double fuelcost = 0.0;
     
           switch(auto)
           {
             case 1:
               fuelcost = (2.50 *(travel/15)) + (travel/3000)*(15);
               break;
     
             case 2:
               fuelcost = ((travel/15) * 2.70 + (travel/3000)*(15));
               break;
     
             case 3:
               fuelcost = ((travel/15) * 2.90 + (travel/3000)*(15));
               break;
           }    
           return fuelcost;                   
         }     
         public static void output(double fuelcost,int miles)
         {
         DecimalFormat twoDigits = new DecimalFormat("$#,000.00");
         JOptionPane.showMessageDialog(null,"Your cost to travel is" + twoDigits.format(fuelcost) + " for " + twoDigitsformat(miles), "you travel".JOptionPane.INFORMATION_MESSAGE);
         }
     
     
         public static void finish()
         {
           System.exit(0);
         }
     
    }

    ERRORS I get.

    3 errors found:
    File: C:\Users\James Turnham\Desktop\gascalculator.java  [line: 15]
    Error: method getFuelcost in class gascalculator cannot be applied to given types;
      required: int,int
      found: no arguments
      reason: actual and formal argument lists differ in length
    File: C:\Users\James Turnham\Desktop\gascalculator.java  [line: 86]
    Error: cannot find symbol
      symbol:   method twoDigitsformat(int)
      location: class gascalculator
    File: C:\Users\James Turnham\Desktop\gascalculator.java  [line: 86]
    Error: cannot find symbol
      symbol:   variable JOptionPane
      location: class java.lang.String
    Last edited by curmudgeon; November 20th, 2012 at 09:39 PM. Reason: code tags added

  10. #10
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Gas Calculator

    I've added code tags and a comment to the code in your post above to indicate the offending line. In the future, please whenever posting code use [code] [/code] tags around your code else we'll not be able to read it.

    --- Update ---

    Quote Originally Posted by JamesdTurnham View Post
    I've done that and also turned the getFuelcost() into a method but now it's saying I can't have two (ints) for the method. I've already declared what the variables are so i'm puzzled why this isn't working.
    As is usually the case, the key is to be found in the error messages.

    File: C:\Users\James Turnham\Desktop\gascalculator.java  [line: 15]
    Error: method getFuelcost in class gascalculator cannot be applied to given types;
      required: int,int
      found: no arguments
      reason: actual and formal argument lists differ in length

    This states plainly that you're calling getFuelcost without arguments, (), when it's defined as requiring two int, int arguments. You have to decide which it shall be, and define it just as you'll use it. In other words, if you create a method that is written to take a Foo object as a parameter, you'd better pass a Foo object into the method when you call it.

    File: C:\Users\James Turnham\Desktop\gascalculator.java  [line: 86]
    Error: cannot find symbol
      symbol:   method twoDigitsformat(int)
      location: class gascalculator

    The compiler thinks that you're trying to call a method twoDigitsformat(...). You're confusing it because you forgot a very important period, so this looks like a strange method that's never been declared. If you look carefully at the symbol that it can't find, you'd discover this yourself.

    File: C:\Users\James Turnham\Desktop\gascalculator.java  [line: 86]
    Error: cannot find symbol
      symbol:   variable JOptionPane
      location: class java.lang.String

    Again, look at the line that this error is complaining about and check to make sure that it is written correctly, that you're using commas and periods and all correctly. This is nothing but a careless error that you'll catch if you look at the line with a discerning eye.

  11. #11
    Junior Member
    Join Date
    Nov 2012
    Posts
    29
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Gas Calculator

    Quote Originally Posted by curmudgeon View Post
    I've added code tags and a comment to the code in your post above to indicate the offending line. In the future, please whenever posting code use [code] [/code] tags around your code else we'll not be able to read it.

    --- Update ---



    As is usually the case, the key is to be found in the error messages.

    File: C:\Users\James Turnham\Desktop\gascalculator.java  [line: 15]
    Error: method getFuelcost in class gascalculator cannot be applied to given types;
      required: int,int
      found: no arguments
      reason: actual and formal argument lists differ in length

    This states plainly that you're calling getFuelcost without arguments, (), when it's defined as requiring two int, int arguments. You have to decide which it shall be, and define it just as you'll use it. In other words, if you create a method that is written to take a Foo object as a parameter, you'd better pass a Foo object into the method when you call it.

    File: C:\Users\James Turnham\Desktop\gascalculator.java  [line: 86]
    Error: cannot find symbol
      symbol:   method twoDigitsformat(int)
      location: class gascalculator

    The compiler thinks that you're trying to call a method twoDigitsformat(...). You're confusing it because you forgot a very important period, so this looks like a strange method that's never been declared. If you look carefully at the symbol that it can't find, you'd discover this yourself.

    File: C:\Users\James Turnham\Desktop\gascalculator.java  [line: 86]
    Error: cannot find symbol
      symbol:   variable JOptionPane
      location: class java.lang.String

    Again, look at the line that this error is complaining about and check to make sure that it is written correctly, that you're using commas and periods and all correctly. This is nothing but a careless error that you'll catch if you look at the line with a discerning eye.
    Ok. Thanks for all the tips, i've got it all figured out.

  12. #12
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: Gas Calculator

    Glad you've got it working!

  13. #13
    Junior Member
    Join Date
    Nov 2012
    Posts
    29
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Gas Calculator

    Quick question. Is it possilble to add JComboBoxes to this or should I rewrite the whole thing? The book gives an example of a content pane with the ComboBoxes but they are doing different things and what i've read so far really didn't help in how to add this to my applet.

    --- Update ---

    This is my current working code. Yes there is extra in there for my next assignment which is to add the ComboBoxes and have the Cities and fuel options in the drop down boxes.

    import java.io.*;
    import java.util.*;
    import javax.swing.*;
    import javax.swing.text.*;
    import javax.swing.JOptionPane;
    import java.text.DecimalFormat;
    import java.awt.*;
    import java.awt.event.*;
    import java.applet.*;
    import javax.swing.JComboBox;
     
     
    public class gascalculatorApplet extends Applet implements ItemListener
    {
       int auto, miles;
       double fuelcost, Cost;
       Color darkGreen = new Color(39, 77, 6);
     
       String beginning[] = {"Minneapolis", "Duluth", "Sioux Falls", "International Falls", "Fargo", "Chicago"};
       String ending[] = {"Phoenix", "Daytona", "Dallas", "Las Vegas", "Darlington", "Talladega"};
       String fuelprices[] = {"2.79", "2.89", "3.19"};
       String vehicletypes[] = {"Compact", "Midsize", "Luxury", "Suv"};
     
     
       Label promptLabel = new Label("Enter the amount of miles you plan to travel or select the city you are leaving from:");
             TextField milesField = new TextField(25);
       Label autoLabel = new Label("Select the vehicle type you will be using:");
     
       CheckboxGroup codeGroup = new CheckboxGroup();
          Checkbox compactBox = new Checkbox("Compact Vehicle",false,codeGroup);
          Checkbox midsizeBox = new Checkbox("Mid-size Vehicle",false,codeGroup);
          Checkbox luxuryBox = new Checkbox("Luxury Vehicle",false,codeGroup);
          Checkbox suvBox = new Checkbox("SUV", false,codeGroup);
          Checkbox hiddenBox = new Checkbox("",true,codeGroup);
     
          Label outputLabel = new Label("Input the amount you are paying for fuel.");
     
     
      public void init()
      {
        setBackground(darkGreen);
        setForeground(Color.white);
        add(promptLabel);
        add(milesField);
        milesField.requestFocus();
        milesField.setForeground(Color.black);
        add(autoLabel);
        add(compactBox);
        compactBox.addItemListener(this);
        add(midsizeBox);
        midsizeBox.addItemListener(this);
        add(luxuryBox);
        luxuryBox.addItemListener(this);
        add(suvBox);
        suvBox.addItemListener(this);
        add(outputLabel);
     
      }
         public void itemStateChanged(ItemEvent choice)
         {
            try
            {
              miles = getMiles();
              auto = getVehicle();
              fuelcost = getFuelcost(miles,auto);
              output (fuelcost, miles);
            }
             catch(NumberFormatException e)
            {
             outputLabel.setText("Enter the amount of miles you plan to drive.");
             hiddenBox.setState(true);
             milesField.setText("");
             milesField.requestFocus();
            }
         }
     
         public int getMiles()
         {
             miles = Integer.parseInt(milesField.getText());
             if (miles <=0) throw new NumberFormatException();
     
     
             return miles;
         }
     
         public int getVehicle()
         {
           int auto = 0;
     
           if (compactBox.getState()) auto = 1;
           else
             if (midsizeBox.getState()) auto = 2;
             else
               if(luxuryBox.getState()) auto = 3;
               else 
                 if(suvBox.getState()) auto = 4;
     
             return auto;
         }
     
         public static double getFuelcost(int miles, int auto)
         {
           double fuelcost = 0.0;
           switch(auto)
           {
             case 1:
               fuelcost = (2.49) * (miles/15) + (miles/3000) * (15);
               break;
     
             case 2:
               fuelcost = (2.69) * (miles/15) + (miles/3000) * (15);
               break;
     
             case 3:
               fuelcost = (2.99) * (miles/15) + (miles/3000) * (15);
               break;
     
             case 4:
               fuelcost = (3.19) * (miles/15) + (miles/3000) * (15);
               break;
           }
            return fuelcost;
       }
        public void output(double fuelcost, int miles)
        {
          DecimalFormat twoDigits = new DecimalFormat("$#,000.00"); 
          outputLabel.setText("Your cost to travel is" + twoDigits.format(fuelcost));
        }
     
     
    }

Similar Threads

  1. Calculator
    By Vibex in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 14th, 2012, 05:23 PM
  2. Help with a calculator
    By Joshroark10 in forum Java Theory & Questions
    Replies: 5
    Last Post: September 10th, 2012, 03:59 PM
  3. Calculator
    By Andrew Wilson in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 2nd, 2011, 08:08 AM
  4. Calculator
    By javapenguin in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 22nd, 2010, 09:00 AM
  5. [SOLVED] Calculator help
    By Bradshjo in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 1st, 2010, 04:27 PM