1

This is the part where I ask the user to input the following.

Enter the item name: Shirt

Original price of the item: 700

Marked-up Percentage: 20

Sales Tax Rate: 7

Output:

Item to be sold : Shirt
Original price of the item: 700.00
Price after mark-up: 840.00
Sales tax: 58.80
Final price of item: 898.80

so my question is how can I make that 20 and 7 input be read by the program as percentages.

    import java.util.*;
    import javax.swing.*;

    public class lab3 {
    public static void main(String[] args) {
    JOptionPane jo=new JOptionPane();
    String item=jo.showInputDialog("Item to be sold: ");
    double Oprice=Double.parseDouble(
            jo.showInputDialog("Original price of the item: "));
    double mup=Double.parseDouble(
            jo.showInputDialog("Marked-up percentage: "));   
    double str=Double.parseDouble(
            jo.showInputDialog("Sales Tax Rate: "));


    double pamu=(Oprice*mup)+Oprice;
    double ST=pamu*str;
    double result=pamu+ST;

    String hold= "\n| Item to be sold \t: "+item+"\t |"
                +"\n| Original price of the item \t: "+Oprice+"\t |"
                +"\n| Price after mark-up \t: "+pamu+"\t |"
                +"\n| Sales Tax \t: "+ST+"\t |"
                +"\n| Final price of the item \t: "+result+"\t |";
    jo.showMessageDialog(null, new JTextArea(hold));
    }
    }

That is my actual code. sorry if its messy. like I said still new to this

john felix
  • 21
  • 3
  • 1
    Hint: what happens if you divide by 100? (Making sure you're performing floating point arithmetic...) – Jon Skeet Jul 03 '17 at 08:10
  • don't use floats or doubles though https://stackoverflow.com/questions/3730019/why-not-use-double-or-float-to-represent-currency – Tschallacka Jul 03 '17 at 08:12
  • Where's your actual code? – cнŝdk Jul 03 '17 at 08:18
  • I really don't know how to add my actual code.. I'm still new to this. sorry – john felix Jul 03 '17 at 08:29
  • I do not understand the question, what exactly is going wrong? I mean 700 + 20% = 840, as already being calculated/output by the code - same for rest of calculations. – user85421 Jul 03 '17 at 08:37
  • I already got the my answer thank you all – john felix Jul 03 '17 at 08:47
  • @johnfelix there is a "?" when entering/editing question for help which also has a link to [advanced help](https://stackoverflow.com/editing-help)... and **thank you** for answering my question [:-( – user85421 Jul 03 '17 at 08:50
  • The output does not match the code - why have you removed part of the code??? Accepting a non-working answer ?!?!!!! are you trying to get reputation ?? – user85421 Jul 03 '17 at 09:11

3 Answers3

0

Let's use Scanner for reading from user:

Scanner sc = new Scanner(System.in);
double percentage = 0.01 * sc.nextInt();

will do the trick.

xenteros
  • 15,586
  • 12
  • 56
  • 91
-2
double pamu = (Oprice*(mup/100)) + Oprice; // enter code here
double ST = pamu*(str/100) ;
double result = pamu+ST ;

I think this should work.

user85421
  • 28,957
  • 10
  • 64
  • 87
Akshay
  • 1
  • 2
-3
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter item name:");
String item;
keyboard.nextLine();
System.out.println("Original Price of item:");
double originalPrice;
keyboard.nextDouble();
System.out.println("Marked up percentage:");
double markedUpPercentage;
keyboard.nextDouble();
markedUpPercentage = markedUpPercentage/100; //this is what you want
System.out.println("Sales tax rate:");
double salesTaxRate;
keyboard.nextDouble();
salesTaxRate = salesTaxRate/100; //same thing here

//now output
System.out.println("Item to be sold: " + item);
System.out.println("Original price of the item: " + originalPrice);
System.out.println("Price after mark up: " + (originalPrice + originalPrice * markedUpPrice));
//similarly, do for sales tax
Arvind Sasikumar
  • 482
  • 3
  • 15
  • My bad, didn't notice that. There was no code in the question itself so I assumed wrong. – Arvind Sasikumar Jul 03 '17 at 08:25
  • @johnfelix just copy paste it into the editor. Leave 4 spaces before you paste for the editor to understand what you are pasting is code. – Arvind Sasikumar Jul 03 '17 at 08:29
  • 1
    does that really work? compile? at least this is **not Java**! e.g. `nextLine` does not take any argument, neither does `nextDouble`!! and Java is pass-by-value, I expected something like `item = keyboard.nextLine()`... and since you already toke time to edit the answer, why not delete the "Since you haven't specified..." part at all? – user85421 Jul 03 '17 at 09:01