import java.util.Scanner;
import java.text.DecimalFormat;
public class FutureValues {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
DecimalFormat df = new DecimalFormat("0.00");
System.out.println("Enter the present value: ");
int value = input.nextInt();
System.out.println("Enter annual interest rate: ");
double rate = input.nextDouble();
double newRate = rate / 1200;
System.out.println("Enter the number of months: ");
int months = input.nextInt();
int i;
for (i = 1; i <= months; i++) {
double newVal = value + (value * newRate);
System.out.println("The future value after " + i + " month is " + newVal);
}
}
}
I'm trying to get this to program to update the newVal to the next monthly deposit but it won't work for anything I try.
eg. "The future value after 1 month is 1004.79"
"The future value after 2 months is 1009.61"
and so on and so forth. I just cannot get it to update to the next value.
The Calculation of newVal should be done within the loop.
import java.util.Scanner;
import java.text.DecimalFormat;
public class FutureValues {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
DecimalFormat df = new DecimalFormat("0.00");
System.out.println("Enter the present value: ");
int value = input.nextInt();
System.out.println("Enter annual interest rate: ");
double rate = input.nextDouble();
double newRate = rate / 1200;
System.out.println("Enter the number of months: ");
int months = input.nextInt();
for (int i = 1; i <= months; i++) {
value = value + (value * newRate);
System.out.println("The future value after " + i + " month is " + value);
}
}
}
Hey new guy here need assistance in my problem! i need to print previous user input data from a loop the problem is that it prints the last data the user inputs. Please shed some light my mind is getting dark. I appreciate all of you answers. Thank you!
My program: (Sorry if it's disgusting af)
package activity2;
import java.util.Scanner;
public class Activity2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String[] name = {"Milk","Juice","Energy drink","Water","Softdrink","Beer","Wine","Coffee"};
int[] pnum = {0,1,2,3,4,5,6,7};
double[] price = {300,100,220,120,200,350,400,130};
String[] list = {"Enter product #: ","Enter quantity: ","Sub-total: "};
double sum=0,q=0,v,s;
int sum2=0, w;
String z = "";
int x,c = 0;
System.out.println(" Product Information");
System.out.println("------------------------------------------");
System.out.println("Name Price");
System.out.println("------------------------------------------");
System.out.println(pnum[0]+"."+name[0]+" "+price[0]);
System.out.println(pnum[1]+"."+name[1]+" "+price[1]);
System.out.println(pnum[2]+"."+name[2]+" "+price[2]);
System.out.println(pnum[3]+"."+name[3]+" "+price[3]);
System.out.println(pnum[4]+"."+name[4]+" "+price[4]);
System.out.println(pnum[5]+"."+name[5]+" "+price[5]);
System.out.println(pnum[6]+"."+name[6]+" "+price[6]);
System.out.println(pnum[7]+"."+name[7]+" "+price[7]);
System.out.println("------------------------------------------");
do{
System.out.print("Enter number of products: ");
int a = sc.nextInt();
for (x=0;x<a;x++){
System.out.print(list[0]);
w = sc.nextInt();
sum2 =w;
System.out.print(list[1]);
s = sc.nextDouble();
q = s * price[w];
System.out.println(list[2]+q);
sum +=q;
}
System.out.println("Total: " + sum);
System.out.print("Do you want another transaction?(y/n):");
z = sc.next();
x = a;
v = q;
System.out.println("Transaction Details");
for(int t=0; t<x; t++){
System.out.println(pnum[sum2]+"."+name[sum2]+"---------"+v);
}
System.out.println("TOTAL: " + sum);
System.out.print("Enter cash amount: ");
double i = sc.nextDouble();
if(sum>i){
System.out.println("Cash is insuffecient! Please try again:");
System.out.print("Enter cash amount: ");
i = sc.nextDouble();
}
double tc = i - sum;
System.out.print("Cash change"+tc);
}
while(z.equals("y"));
}
}
One approach is to use a StringBuilder.
(direct quote below from link)
"StringBuilder objects are like String objects, except that they can be modified. Internally, these objects are treated like variable-length arrays that contain a sequence of characters. At any point, the length and content of the sequence can be changed through method invocations."
You can save data with StringBuilder, then print everything all at one time. This makes life a little easier too ;-)
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String[] name = {"Milk","Juice","Energy drink","Water","Softdrink","Beer","Wine","Coffee"};
int[] pnum = {0,1,2,3,4,5,6,7};
double[] price = {300,100,220,120,200,350,400,130};
String[] list = {"Enter product #: ","Enter quantity: ","Sub-total: "};
double sum=0,q=0,v,s;
int sum2=0, w;
String z = "";
int x,c = 0;
System.out.println(" Product Information");
System.out.println("------------------------------------------");
System.out.println("Name Price");
System.out.println("------------------------------------------");
System.out.println(pnum[0]+"."+name[0]+" "+price[0]);
System.out.println(pnum[1]+"."+name[1]+" "+price[1]);
System.out.println(pnum[2]+"."+name[2]+" "+price[2]);
System.out.println(pnum[3]+"."+name[3]+" "+price[3]);
System.out.println(pnum[4]+"."+name[4]+" "+price[4]);
System.out.println(pnum[5]+"."+name[5]+" "+price[5]);
System.out.println(pnum[6]+"."+name[6]+" "+price[6]);
System.out.println(pnum[7]+"."+name[7]+" "+price[7]);
System.out.println("------------------------------------------");
do{
System.out.print("Enter number of products: ");
int a = sc.nextInt();
StringBuilder sb = new StringBuilder();
for (x=0;x<a;x++) {
System.out.print(list[0]);
w = sc.nextInt();
sum2 =w;
System.out.print(list[1]);
s = sc.nextDouble();
q = s * price[w];
System.out.println(list[2]+q);
sb.append(pnum[sum2]+"."+name[sum2]+"---------"+q + "\n");
sum +=q;
}
System.out.println("Total: " + sum);
System.out.print("Do you want another transaction?(y/n):");
z = sc.next();
x = a;
v = q;
System.out.println("Transaction Details");
System.out.println(sb);
System.out.println("TOTAL: " + sum);
System.out.print("Enter cash amount: ");
double i = sc.nextDouble();
if(sum>i){
System.out.println("Cash is insuffecient! Please try again:");
System.out.print("Enter cash amount: ");
i = sc.nextDouble();
}
double tc = i - sum;
System.out.print("Cash change"+tc);
}
while(z.equals("y"));
}
This will give you the output you're looking for.
The issue is you're not saving each entry. If you want to be able to print it back at the end you need to do this. I'm not exactly sure what you're trying to do, but this should at least give you an idea on what you need to do in order to be able to print out transaction details. Also you really need to name your variables a lot better.
import java.util.Scanner;
public class Activity2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String[] name = {"Milk","Juice","Energy drink","Water","Softdrink","Beer","Wine","Coffee"};
int[] pnum = {0,1,2,3,4,5,6,7};
double[] price = {300,100,220,120,200,350,400,130};
String[] list = {"Enter product #: ","Enter quantity: ","Sub-total: "};
double sum=0,q=0,v,s;
int sum2=0, w;
String z = "";
int x,c = 0;
System.out.println(" Product Information");
System.out.println("------------------------------------------");
System.out.println("Name Price");
System.out.println("------------------------------------------");
System.out.println(pnum[0]+"."+name[0]+" "+price[0]);
System.out.println(pnum[1]+"."+name[1]+" "+price[1]);
System.out.println(pnum[2]+"."+name[2]+" "+price[2]);
System.out.println(pnum[3]+"."+name[3]+" "+price[3]);
System.out.println(pnum[4]+"."+name[4]+" "+price[4]);
System.out.println(pnum[5]+"."+name[5]+" "+price[5]);
System.out.println(pnum[6]+"."+name[6]+" "+price[6]);
System.out.println(pnum[7]+"."+name[7]+" "+price[7]);
System.out.println("------------------------------------------");
do{
System.out.print("Enter number of products: ");
int a = sc.nextInt();
int[] productNum = new int[a];
String[] products = new String[a];
double[] prices = new double[a];
for (x=0;x<a;x++){
System.out.print(list[0]);
w = sc.nextInt();
sum2 =w;
System.out.print(list[1]);
s = sc.nextDouble();
q = s * price[w];
System.out.println(list[2]+q);
sum +=q;
productNum[x] = w;
products[x] = name[w];
prices[x] = q;
}
System.out.println("Total: " + sum);
System.out.print("Do you want another transaction?(y/n):");
z = sc.next();
x = a;
v = q;
System.out.println("Transaction Details");
for(int t=0; t<x; t++){
System.out.println(productNum[t]+"."+products[t]+"---------"+prices[t]);
}
System.out.println("TOTAL: " + sum);
System.out.print("Enter cash amount: ");
double i = sc.nextDouble();
if(sum>i){
System.out.println("Cash is insuffecient! Please try again:");
System.out.print("Enter cash amount: ");
i = sc.nextDouble();
}
double tc = i - sum;
System.out.print("Cash change"+tc);
}
while(z.equals("y"));
}
}
I was wondering if someone could tell me
1. why, when i input weightNumber with a decimal place, weightConverted doesn't convert it to the whole number, even though I create variable for it?
2. how could i improve this "program" in any way, THANK YOU !!
here is the problem:
code:
import java.util.Scanner;
public class cofee {
public static void main (String []args){
double weightNumber = 0;
String packageType = "";
String serviceType ="";
double totalFee = 0;
double weightConverted = Math.round(weightNumber); // <- this is the problem, should i put it somewhere else?
final double LETTERCOSTP = 12.00;
final double LETTERCOSTS = 10.50;
final double BOXCOSTP = 15.75;
final double BOXCOSTS = 13.75;
final double BOXWEIGHTP = 1.25;
final double BOXWEIGHTS = 1.00;
// input
Scanner input = new Scanner(System.in);
System.out.print("Enter package type (letter/box): ");
packageType = input.nextLine().toLowerCase();
System.out.print("Enter type of service (standard/priority): ");
serviceType = input.nextLine().toLowerCase();
switch(packageType)
{
case "letter":
System.out.print("Enter the weight in ounces: ");
weightNumber = input.nextDouble();
break;
case "box":
System.out.print("Enter the weight in pounds: ");
weightNumber = input.nextDouble();
break;
default:
System.out.print("WRONG PACKAGE TYPE !!!");
}
// letter
if (packageType.equals("letter") && serviceType.equals("priority"))
{
totalFee = LETTERCOSTP;
}
if (packageType.equals("letter") && serviceType.equals("standard"))
{
totalFee = LETTERCOSTS;
}
// box
if (packageType.equals("box") && serviceType.equals("priority"))
{
totalFee = BOXCOSTP + ((weightConverted - 1.0) * BOXWEIGHTP);
}
if (packageType.equals("box") && serviceType.equals("standard"))
{
totalFee = BOXCOSTS + ((weightConverted - 1.0) * BOXWEIGHTS);
}
// display
System.out.println("The fee is € "+ totalFee + " for a package with");
System.out.println("\tType: "+packageType);
System.out.println("\tService: "+serviceType);
System.out.println("\tOunces: "+weightConverted);
}
}
The line double weightConverted = Math.round(weightNumber); will call round() with the value of weightNumber, which is 0, so it rounds 0 to... well... 0, and assigns it to weightConverted.
I am extremely new to java I am in my second week in classes or so--
I need my program to keep going or exit according to the user. It is a payroll calculation and I want the end to say "Do you want to continue (y/n)" I want Y to repeat my entire program of questions and no to end program. I am using Jgrasp and I am very very new. I am assuming it needs a loop and I am not totally sure, I just got this to run and compile correctly-- it runs correctly for me so it is a good start and I am hoping to get help on how to do this as I am seeing a ton of different ways and different programs for it. thanks.
import java.util.Scanner;
public class calculations {
public static void main(String [] args) {
Scanner reader = new Scanner(System.in);
Scanner in = new Scanner(System.in);
double Regpay;
double Payperhour;
int HoursAweek;
double Pay;
double OvertimeHours;
double OvertimePay;
double Dependants;
double SocSecTax;
double FederalTax;
double StateTax;
int UnionDues;
double AllTaxes;
double FinalPay;
String playAgain;
System.out.print("Enter your pay per hour:");
Payperhour = reader.nextDouble ();
System.out.print("Enter your regular Hours a week:");
HoursAweek = reader.nextInt();
System.out.print("Enter your overtime hours:");
OvertimeHours = reader.nextDouble();
Regpay = Payperhour * HoursAweek;
OvertimePay = OvertimeHours * 1.5 * Payperhour;
Pay = OvertimePay + Regpay;
SocSecTax = Pay * .06;
FederalTax = Pay * .14;
StateTax = Pay * .05;
UnionDues = 10;
AllTaxes = SocSecTax + FederalTax + StateTax + UnionDues;
FinalPay = Pay -= AllTaxes;
System.out.println("Your pay this week will be " +FinalPay);
{
System.out.println("How many Dependants:");
Dependants = reader.nextInt();
if (Dependants >= 3) {
Dependants = Pay + 35;
System.out.println("Your Pay is:" +Dependants);
} else if(Dependants < 3) {
System.out.println("Your Pay is:" +Pay);
}
}
}
}
Here is the basic idea with your code:
import java.util.Scanner;
public class calculations{
public static void main(String [] args) {
Scanner reader = new Scanner(System.in);
Scanner in = new Scanner(System.in);
double Regpay;
double Payperhour;
int HoursAweek;
double Pay;
double OvertimeHours;
double OvertimePay;
double Dependants;
double SocSecTax;
double FederalTax;
double StateTax;
int UnionDues;
double AllTaxes;
double FinalPay;
String playAgain;
int runAgain = 1;
while (runAgain == 1) {
System.out.print("Enter your pay per hour:");
Payperhour = reader.nextDouble();
System.out.print("Enter your regular Hours a week:");
HoursAweek = reader.nextInt();
System.out.print("Enter your overtime hours:");
OvertimeHours = reader.nextDouble();
Regpay = Payperhour * HoursAweek;
OvertimePay = OvertimeHours * 1.5 * Payperhour;
Pay = OvertimePay + Regpay;
SocSecTax = Pay * .06;
FederalTax = Pay * .14;
StateTax = Pay * .05;
UnionDues = 10;
AllTaxes = SocSecTax + FederalTax + StateTax + UnionDues;
FinalPay = Pay -= AllTaxes;
System.out.println("Your pay this week will be " + FinalPay);
{
System.out.println("How many Dependants:");
Dependants = reader.nextInt();
if (Dependants >= 3) {
Dependants = Pay + 35;
System.out.println("Your Pay is:" + Dependants);
} else if (Dependants < 3) {
System.out.println("Your Pay is:" + Pay);
}
}
System.out.println("Again??? Press 1 to run again and 0 to exit");
runAgain = reader.nextInt();
}
}
}
Here's a guide for you..
You can create a method for the transaction.
//Place this on your main method
do{
//call the method
transaction();
//ask if the user wants to repeat the program
System.out.print("Do you want to continue (y/n)");
input = reader.nextLine();
}while(input.equalsIgnoreCase("Y"))
public void transaction(){
//your transaction code here..
}
Here is a brief idea of how to do this:
String choice = "y";
while(true) {
//Take input
System.out.print("Do you want to continue (y/n)?");
choice = reader.nextLine();
if(choice.equalsIgnoreCase("n")) break;
//else do your work.
}
You can also sue do-while to get what you want. You may need to make some changes but then that is the entire idea. It is just a hint.
This is for an assignment in my class. It is to make an automatic ordering system. I'm still new to Java so everything doesn't necessarily click just yet. I think most things work for the most part but the main thing I am having trouble with is making the loop itself and making an exit for it.
import java.util.Scanner;
public class Metal {
public static void main (String[] args) {
double PRICE1 = 5.00;
double PRICE2 = 7.00;
double PRICE3 = 3.50;
double PRICE4 = 0.75;
double TAX = 0.05;
System.out.println ("Metal Down Your Mouth Menu");
System.out.println ();
System.out.println ();
System.out.println ();
System.out.println ("1. Black Sabbath Burgers (Hamburgers With Black Buns) " + PRICE1);
System.out.println ("2. Rack of Lamb of God (Rack of Lamb) " + PRICE2);
System.out.println ("3. Texas Hippie Collar Greens (Collar Greens) " + PRICE3);
System.out.println ("4. Pepsi " + PRICE4);
System.out.println ("Press any other button to stop ordering.");
Scanner userInput = new Scanner(System.in);
int itemnumber = 0;
while (itemnumber < 1 || itemnumber > 4) {
System.out.print("Enter the item number of the item you wish to order: ");
itemnumber = userInput.nextInt();
}
System.out.print ("How many?");
int amount = userInput.nextInt();
double subtotal = 0;
double total = 0;
double price = 0;
double taxes = 0;
String name = "";
switch (itemnumber){
case 1: name = "Black Sabbath Burgers"; price = PRICE1; break;
case 2: name = "Rack of Lamb of God"; price = PRICE2; break;
case 3: name = "Texas Hippie Collar Greens"; price = PRICE3; break;
case 4: name = "Pepsi"; price = PRICE4; break;
}
subtotal = price * amount;
total = subtotal + total;
System.out.print("Price for items: " + subtotal);
System.out.print("Price Total: " + total);
}
This is my first time posting on this site, but I think I found your problem. There are two large errors, indicated by the arrows:
while (itemnumber >= 1 || <-- itemnumber <= 4) {
System.out.print("Enter the item number of the item you wish to order: ");
itemnumber = userInput.nextInt();
} <--
1) This should be a '&&' not a '||'. You want it to be within the range. Right now the number it reads has to be greater than or equal to 1 OR less than or equal to 4, which is all integers.
2) You close your loop prematurely. What your code does right now (after the && switch) is it takes numbers 1-4 and keeps repeating the "Enter the item number...." line until you put a number not in the range, then it continues.
The fix: there are a few ways to fix this. My fix would be thus, and the explanation will come after:
import java.util.Scanner;
public class Metal {
public static void main (String[] args) {
double PRICE1 = 5.00;
double PRICE2 = 7.00;
double PRICE3 = 3.50;
double PRICE4 = 0.75;
double TAX = 0.05;
System.out.println ("Metal Down Your Mouth Menu");
System.out.println ();
System.out.println ();
System.out.println ();
System.out.println ("1. Black Sabbath Burgers (Hamburgers With Black Buns) " + PRICE1);
System.out.println ("2. Rack of Lamb of God (Rack of Lamb) " + PRICE2);
System.out.println ("3. Texas Hippie Collar Greens (Collar Greens) " + PRICE3);
System.out.println ("4. Pepsi " + PRICE4);
System.out.println ("Press any other button to stop ordering.");
Scanner userInput = new Scanner(System.in);
int itemnumber = 0;
System.out.print("Enter the item number of the item you wish to order: ");
itemnumber = userInput.nextInt();
double total = 0;
while (itemnumber >= 1 && itemnumber <= 4) {
System.out.print ("How many?");
int amount = userInput.nextInt();
double subtotal = 0;
double price = 0;
double taxes = 0;
String name = "";
switch (itemnumber)
{
case 1: name = "Black Sabbath Burgers"; price = PRICE1; break;
case 2: name = "Rack of Lamb of God"; price = PRICE2; break;
case 3: name = "Texas Hippie Collar Greens"; price = PRICE3; break;
case 4: name = "Pepsi"; price = PRICE4; break;
}
subtotal = price * amount;
total = subtotal + total;
System.out.print("Price for items: " + subtotal);
System.out.print("Enter the item number of the item you wish to order: ");
itemnumber = userInput.nextInt();
}
System.out.print("Price Total: " + total);
}
}
Explanation: In essence, you had like 90% of it. I moved the mentioned '}' to the end here:
itemnumber = userInput.nextInt();
} <--
That way, it loops over this code until the user ends.
Additionally, your loop does not need much fixing. It can be used with the && fix. However, you have to put that top line before the loop.
System.out.print("Enter the item number of the item you wish to order: ");
itemnumber = userInput.nextInt();
And then you put the same line at the end of the loop to reset itemnumber. What your loop does is if itemnumber is between 1 and 4, it executes the following code. Otherwise, it stops. By checking before you enter the loop, you set itemnumber so that way the loop has something to check. And you put the next input at the end of the loop so that way your program finishes totaling its first execution before moving on to the next.
Additionally, you should move the variable 'total' out of the loop as seen above. If you keep looping over it and resetting it to 0, your total will output 0 every time. Best to keep the creation of total out of the loop, and its modification inside the loop.
Small tip, use System.out.println(); instead of System.out.print(); it puts the outputs on its own line. Looks a little nicer.
I think that covers it. If you want more explanation, I'd be more than happy to give it to you. Java is pretty fun once you get used to it. It just takes time. :D