Error in coding


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 7 of 7

Thread: Error in coding

  1. #1
    Join Date
    Jan 2008
    Posts
    29

    Error in coding

    Hi, I am taking my first course in java. could you help me to find the problem here.... thanks


    Code:
    import javax.swing.JOptionPane;
    public class FixDebugTwo4
    {
      public static void main(String[] args)
      {
        String costString;
        double cost = JOptionPane.showInputDialog(null, "Enter price of item you are buying");
        double tax = 0.06;
        JOptionPane.showMessageDialog(null, "With tax your purchase is " + (cost * tax) + cost);
    
        System.exit(0);
      }
    }

  2. #2
    Join Date
    Apr 2007
    Location
    Sterling Heights, Michigan
    Posts
    8,666
    Welcome to DevX

    What am I looking for?

    What is the error?
    I don't answer coding questions via PM or Email. Please post a thread in the appropriate forum section.
    Please use [Code]your code goes in here[/Code] tags when posting code.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    Modifications Required For VB6 Apps To Work On Vista

  3. #3
    Join Date
    Jan 2008
    Posts
    29

    error in coding

    Thanks Hack:

    the first one is: if you do the math and the product cost $2 +tax the price will be $2.12 and not like 26. the second is when you try to compile it:
    DebugTwo4.java:15: ')' expected
    System.exit(0);

    thanks

  4. #4
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    Think about your math - and enclose your calculation inside parenthesis at the end of the error generating statement so that only the result is included in your output string.

    Wouldn't it be better to calculate the final cost as (1 + tax) * cost?

  5. #5
    Join Date
    Jan 2008
    Posts
    29

    error in coding

    Thanks Hack,

    Good idea. I have to review my math, yes. Now sorry to bother you again but the same idea with this one (I am debugging) but the final result is "With 6.0 tax, purchase is $1.00.06" but shouldn't be the answer $1.06? how I limit or erase that 00 extra. will it be in the declaration of variables?

    Thanks again, I am learning a lot with this.
    Code:
    import javax.swing.JOptionPane;
    public class DebugTwo4
    {
      public static void main(String[] args)
      {
        String costString;
        double cost;
        double tax = 0.06;
        costString = JOptionPane.showInputDialog(null,
          "Enter price of item you are buying", "Purchases",
          JOptionPane.INFORMATION_MESSAGE);
        cost = Double.parseDouble(costString);
        JOptionPane.showMessageDialog(null,"With " + tax * 100 +
            "% tax,  purchase  is $" + cost + cost * tax);
        System.exit(0);
      }
    }

  6. #6
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    You are seeing the results of passing the string representation of the cost and then concatenating the result of cost*tax; therefore you are getting 1.00.06.

    If, however, you put your entire calculation inside of a parenthesis, the compiler knows that you want to do the math and THEN pass the string representation of the RESULT to the output. Replace the existing end of the statement to

    ...+ (cost + (cost * tax)) );

    and the math will be performed and only the result will be displayed.

  7. #7
    Join Date
    Jan 2008
    Posts
    29

    error in coding

    Thanks Nspils,

    It worked wonderfully. since this is my first java course and on line, it has been a little hard but I think I am starting to understand all this things about strings, class, concatenation, etc.

    thanks again for supporting people like me, beginners. no more for now. bye.

Similar Threads

  1. VB Coding which im finding hard
    By united_1987 in forum VB Classic
    Replies: 2
    Last Post: 11-27-2006, 03:04 AM
  2. Replies: 1
    Last Post: 12-01-2005, 11:09 PM
  3. Convert applet coding to CSS
    By LogicsHere in forum Java
    Replies: 1
    Last Post: 09-23-2005, 10:13 AM
  4. Replies: 4
    Last Post: 05-24-2005, 06:31 AM
  5. VB Friend 2.0 Speeds Up Coding
    By Tuomas Salste in forum vb.announcements
    Replies: 0
    Last Post: 01-24-2001, 12:22 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center