EA Forums - Banner

Help with java?

For the first problem, make your life easier and set your charge variable to double.

In the if statement you need to have && userinput <=

An if statement is essentially comparing or checking for a condition. It is returning a boolean(true or false) and if it is true it will execute the code within the if statement.

So by that, I mean, it checks the first condition, but when including && or || you need to again make a full comparison.

So if(userinput >= 10 && userinput <= 19)

For calculating cost after discount...

cost = cost - (cost*discount)

Replies

  • jyoung1903
    103 posts
    edited December 2015
    Oh, you don't want the userinput variable to be a string if it is just a number. Make it an int.
  • jyoung1903
    103 posts
    edited December 2015
    You can't perform math on a string. You need them to be into or doubles etc. a string is just an array of characters. You parsed the first string into an integer...not needed, but works. You didn't with the 2nd string. But you don't actually even need it. Just display the variable cost when you need to. You can display integers and doubles. You don't need them to be strings to display.

    Hope that all helps you. Been awhile since I have messed with Java, but the basics are the same as any language.
  • jyoung1903
    103 posts
    edited December 2015
    You are also using swing, which I don't know if you need to or not, but typically you could just use...

    System.out("input how many you need");
    System.in(input);

    cost = input * 99;

    If([your if statement here])
    {
    Discount = .2
    cost -= cost*discount;
    )
    Else if...
  • NOVA_POLARI
    1010 posts
    edited December 2015
    jyoung1903 wrote:
    You can't perform math on a string. You need them to be into or doubles etc. a string is just an array of characters. You parsed the first string into an integer...not needed, but works. You didn't with the 2nd string. But you don't actually even need it. Just display the variable cost when you need to. You can display integers and doubles. You don't need them to be strings to display.

    Hope that all helps you. Been awhile since I have messed with Java, but the basics are the same as any language.

    Okay so I changed the second string into a double. Now there are two lines that are confusing me. The first one is "static int parseInt(String userinput, int radix)" It says I have three errors that are
    *** Errors ***

    Error: Syntax error on token "(", ; expected

    Error: Syntax error on token ",", ; expected

    Error: Syntax error on token ")", ; expected

    When I try deleting the line it says I can't use any of the math symbols. So yeah sorry to bother you but I am really confused.
  • jyoung1903
    103 posts
    edited December 2015
    I am not entirely sure what you are trying to do with userinput2. But where you declared it you spelled it wrong. You have it as uDerinput. So when you are initializing it, it doesn't know the variable type because the variable isn't declared properly. But quite honestly, you don't need userinput2.
  • jyoung1903
    103 posts
    edited December 2015
      public static void main(String[] args)   
       {  
        double cost = 0.0; //need to declare cost variable;
        double discount = 0.0;  
        double charge = 99.0  //changed to double because represents dollar amount
        String userinput2 = JOptionPane.showInputDialog(&quot;Please enter company name: &quot;);//added company name prompt
        double userinput = JOptionPane.showInputDialog(&quot;Please input the amount of packages&quot;);  
        //Integer.parseInt(userinput); //no longer needed
        cost = userinput * charge;  
       if (userinout &gt;=10 && &lt;=19)   
          discount = .20
          cost -= cost*discount
       //all you else if statements need to follow
        JOptionPane.showMessageDialog(null, &quot;Thank you for you order &quot; + input2 + &quot;! You have ordered &quot; + userinput + &quot; packages, at a &quot; + discount*100 + &quot;% discount. Your final cost will be $&quot; + cost + &quot;.&quot;);
      }  
    
    
  • jyoung1903
    103 posts
    edited December 2015
    Btw, is this homework? I probably should have helped more than give you the answer. I can help you out and explain more. I just quickly put that together, and did not compile or run it. But it should work (if I remembered how to append a message together correctly). If it doesn't work let me know. I left comments to help explain what changes I made to help you out.
  • jyoung1903
    103 posts
    edited December 2015
    jyoung1903 wrote:
    You can't perform math on a string. You need them to be into or doubles etc. a string is just an array of characters. You parsed the first string into an integer...not needed, but works. You didn't with the 2nd string. But you don't actually even need it. Just display the variable cost when you need to. You can display integers and doubles. You don't need them to be strings to display.

    Hope that all helps you. Been awhile since I have messed with Java, but the basics are the same as any language.

    Okay so I changed the second string into a double. Now there are two lines that are confusing me. The first one is "static int parseInt(String userinput, int radix)" It says I have three errors that are
    *** Errors ***

    Error: Syntax error on token "(", ; expected

    Error: Syntax error on token ",", ; expected

    Error: Syntax error on token ")", ; expected

    When I try deleting the line it says I can't use any of the math symbols. So yeah sorry to bother you but I am really confused.

    This might be because you forgot a semicolon? Not sure though because I did not see that line in your code.
This discussion has been closed.

Howdy, Stranger!

It looks like you're new here. Sign in or register to get started.