I am creating a simple commissions calculator, whereas, one can input the final sales price of an infinite amount of sales; then at the end it prints out the total amount of the commission plus a base pay rate ($200).
Here is my initial code:
import java.util.Scanner;
public class AssignmentsModule2_Program2 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// Intialization Phase
double sold = 0;
double soldCounter = 0;
double baseRate = 200.00;
double commissionRate = 0.09;
System.out.print("Enter total of sold item or -1 if done: ");
int value = input.nextInt();
while (sold != -1)
{
sold = sold + value;
soldCounter = soldCounter + 1;
System.out.print("Enter price of sold item or -1 if done: ");
value = input.nextInt();
}
double totalCommission = sold * commissionRate + baseRate;
System.out.printf("%nTotal pay for the week is: %d%n", sold);
}
}
Unfortunately, when I run the program an error code appears.
Here is the error code:
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:864)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
at assignmentsmodule2_program2.AssignmentsModule2_Program2.main(AssignmentsModule2_Program2.java:31)
Java Result: 1
Is there anyone out there who can lend some help?
Thanks.
You program is expecting an integer as input. The exception is thrown when a float or a char is used as input. I would use double variable:
import java.util.Scanner;
public class AssignmentsModule2_Program2 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// Intialization Phase
double sold = 0.0;
int soldCounter = 0;
double baseRate = 200.00;
double commissionRate = 0.09;
System.out.print("Enter total of sold item or -1 if done: ");
double value = input.nextDouble();
while (value > 0)
{
sold = sold + value;
soldCounter = soldCounter + 1;
System.out.print("Enter price of sold item or -1 if done: ");
value = input.nextDouble();
}
double totalCommission = sold * commissionRate + baseRate;
System.out.printf("%nTotal pay for the week is: %f%n", sold);
}
}
Related
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);
}
}
}
I wrote the code to calculate the total based on the number of seats chosen by the user. The problem is when I enter a negative number for one of the seatings, the total is still calculated. Instead, when a negative number is inputted I want an error message to pop up and not calculate the total.
package javatheatreseating;
import java.util.Scanner;
public class JavaTheatreSeating {
public static final double PREMIUM_PRICE = 45.00;
public static final double STANDARD_PRICE = 30.00;
public static final double ECONOMY_PRICE = 21.00;
public static final double TAX_RATE = 0.0825;
public static final double SURCHARGE = 5.00;
public static void main(String[] args) {
int premiumSeats;
int standardSeats;
int economySeats;
double subTotal;
double salesTax;
double surCharge;
double total;
Scanner stdin = new Scanner(System.in);
//INPUT: number of seats sold
System.out.print ("Enter the number of Premium Seats Sold: ");
premiumSeats = stdin.nextInt();
System.out.print ("Enter the number of Standard Seats Sold: ");
standardSeats = stdin.nextInt();
System.out.print ("Enter the number of Economy Seats Sold: ");
economySeats = stdin.nextInt();
//PROCESS: i calculate the total and add the percent of tax based on the seats added
subTotal = premiumSeats * PREMIUM_PRICE + standardSeats * STANDARD_PRICE + economySeats * ECONOMY_PRICE;
salesTax = TAX_RATE * subTotal;
total = subTotal + salesTax + SURCHARGE;
//OUTPUT:
System.out.println();
System.out.println("Subtotal: " + subTotal);
System.out.println("Tax: " + salesTax);
System.out.println("surCharge: " + SURCHARGE);
System.out.println("Total: " + total);
}
}
put a while loop around each variable input and keep looping until the user gets it right. I didn't check if this compiles though.
while (true) {
try {
System.out.print ("Enter the number of Premium Seats Sold: ");
premiumSeats = stdin.nextInt();
if (premiumSeats >= 0){
break;
} else {
System.out.print ("Please Enter a positive integer.\n");
}
}
catch (Exception e){
System.out.print ("Please Enter a number.\n");
}
}
When I run this code and input the double value for the first variable i.e
miles It shows an error on the read.nextDouble() line as Exception in thread "main" java.util.InputMismatchException.
/**
* Created by Ranjan Yadav on 1.10.2016.
*/
public class GasMileage {
public static void main(String[] args){
java.util.Scanner read = new java.util.Scanner(System.in);
int counter = 0;
System.out.println("Miles Driven(press 1 to quit): ");
double miles = read.nextDouble();
double totalGalon = 0;
double totalMiles = 0;
double milesPerGalon = 0;
double totalMilesPerGalon = 0;
totalMiles += miles;
while(miles != 1){
System.out.println("Gallon used: ");
double galon = read.nextDouble();
counter++;
milesPerGalon = miles / galon;
totalMilesPerGalon += milesPerGalon;
System.out.println("Miles per gallon: " + milesPerGalon);
System.out.println("Miles Driven(press 1 to quit); ");
miles = read.nextDouble();
totalGalon += galon;
totalMiles += miles;
}
if(counter == 0 ){
System.out.println("No values were entered.\nThanks for Using!\n\n");
}else{
double avg = totalMilesPerGalon / counter;
System.out.printf("Total miles driven: %.2f" , totalMiles);
System.out.printf("Total gallons used: %.2f" , totalGalon);
System.out.printf("Miles per gallon for all trips: %.2f" , totalMilesPerGalon);
}
}
}
From the Javadoc:
Throws:
InputMismatchException - if the next token does not match the Float regular expression, or is out of range
Basically, you're inputting something that is not a number.
Im trying to write a code, that computes CD value, for every month.
Suppose you put 10,000 dollars into a CD with an annual percentage yield of 6,15%.
After one month the CD is worth:
10000 + 10000 * 6,15 / 1200 = 10051.25
After the next month :
10051.25 + 10051.25 * 6,15 / 1200 = 10102.76
Now I need to display all the results for the specific number of months entered by the user,
So
month1 =
month2 =
But whth this code I wrote, nothing is printed.
Can you see what's wrong?
Thanks in advance!
import java.util.Scanner;
public class CDValue {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("Enter an amount");
double amount = input.nextInt();
System.out.println ("Enter the annual percentage yield");
double percentage = input.nextDouble();
System.out.println ("Enter the number of months");
int months = input.nextInt();
double worth = amount + amount * percentage / 1200;
for (int i = 1; i < months; i++) {
while (i != months) {
amount = worth;
worth = amount + amount * percentage / 1200;
}
System.out.print(worth);
You do not modify neither i nor months in
while (i != months) {
....
}
so if the (i != months) condition is satisfied, the loop runs forever, and you never get to System.out.print statement.
for (int i = 1; i < months; i++) {
while (i != months) {
//you have to modify i or to modify the while condition.
}
if you don't modify i in the while you can't exit from the loop
Corrected code-
import java.util.Scanner;
public class CDValue {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("Enter an amount");
double amount = input.nextInt();
System.out.println ("Enter the annual percentage yield");
double percentage = input.nextDouble();
System.out.println ("Enter the number of months");
int months = input.nextInt();
double worth = amount + amount * percentage / 1200;
for (int i = 1; i <= months; i++)
{
System.out.print("Month " + i + " = " + worth);
amount = worth;
worth = amount + amount * percentage / 1200;
}
Note: If you want to print values for each month then the print statement should be inside the loop. You don't need two loops for the objective that you have mentioned above.
As you have been told your code won't get out of the while loop if you don't modify it. Simply remove the while loop. Your code should be like this:
import java.util.Scanner;
public class CDValue {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("Enter an amount");
double amount = input.nextDouble();
System.out.println ("Enter the annual percentage yield");
double percentage = input.nextDouble();
System.out.println ("Enter the number of months");
int months = input.nextInt();
double worth = amount + amount * percentage / 1200;
for (int i = 1; i < months; i++) {
amount = worth;
worth = amount + amount * percentage / 1200;
}
System.out.print(worth);
}
}
Thanks! Solved it by using
{
System.out.print("Month " + i + " = " + worth);
amount = worth;
worth = amount + amount * percentage / 1200;
instead of while loop.
It works now :) Thanks so much!
I am having an issue with a method returning to the main method. It is saying that amount in "return amount" cannot be resolved to a variable. Where am I off on this??
This is the message I get:
Multiple markers at this line
- Void methods cannot return a
value
- amount cannot be resolved to a
variable
Here is the code:
import java.util.Scanner;
public class Investment {
public static void main(String[]args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the amount invested: ");
double amount = input.nextDouble();
System.out.print("Enter the annual interest rate: ");
double interest = input.nextDouble();
int years = 30;
System.out.print(futureInvestmentValue(amount, interest, years)); //Enter output for table
}
public static double futureInvestmentValue(double amount, double interest, int years) {
double monthlyInterest = interest/1200;
double temp;
double count = 1;
while (count < years)
temp = amount * (Math.pow(1 + monthlyInterest,years *12));
amount = temp;
System.out.print((count + 1) + " " + temp);
}
{
return amount;
}
}
You curly braces are not correct. The compiler - and me - was confused about that.
This should work (at least syntactically):
import java.util.Scanner;
public class Investment {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the amount invested: ");
double amount = input.nextDouble();
System.out.print("Enter the annual interest rate: ");
double interest = input.nextDouble();
int years = 30;
System.out.print(futureInvestmentValue(amount, interest, years));
}
public static double futureInvestmentValue(
double amount, double interest, int years) {
double monthlyInterest = interest / 1200;
double temp = 0;
double count = 1;
while (count < years)
temp = amount * (Math.pow(1 + monthlyInterest, years * 12));
amount = temp;
System.out.print((count + 1) + " " + temp);
return amount;
}
}
Remove amount from its own scope As a start. Also from the method futureInvestmentValue, you take in amount as an argument but the value is never modified so you're returning the same value being passed which is most likely not the desired outcome.
remove return amount from its own scope
the method futureInvestmentValue... You can't modify any of the parameters inside the method so you have to declare another variable besides amount inside the method (maybe it's the temp variable you keep using) and return that instead
when you return something, the return statement is always inside the method. Never outside it while inside its own braces (never seen this before...)
import java.util.Scanner;
public class Investment {
public static void main(String[]args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the amount invested: ");
double amount = input.nextDouble();
System.out.print("Enter the annual interest rate: ");
double interest = input.nextDouble();
int years = 30;
System.out.print(futureInvestmentValue(amount, interest, years)); //Enter output for table
}
public static double futureInvestmentValue(double amount, double interest, int years) {
double monthlyInterest = interest/1200;
double temp;
double count = 1;
while (count < years) {
temp = amount * (Math.pow(1 + monthlyInterest,years *12));
System.out.print((count + 1) + " " + temp);
}
return amount;
}
}