It should continue to accept a price of an item and displays the current total while the user input is not equals to zero - loops

/* I need some help. I'm stuck with this code. It should continue to accept a price of an item and displays the current total while the user input is not equal to zero. However, output for the current total won't show the current amount. */
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
String customerName;
char promtSenior;
int num = 1;
double itemPrice;
double currentTotal = 0;
ArrayList<Double> amounts = new ArrayList<Double>();
System.out.print("Please enter customer name: ");
customerName = scanner.nextLine();
while (true) {
System.out.print("Please enter item " + num++ + " price or 0 to quit: ");
itemPrice = scanner.nextDouble();
amounts.add(itemPrice);
scanner.nextLine();
for (double i : amounts) {
currentTotal = currentTotal + i ;
}
System.out.println(currentTotal);
if (itemPrice == 0) {
System.out.println("Are you a Senior Citizen? [Y/N]: " + customerName);
promtSenior = scanner.next().charAt(0);
if (promtSenior == 'N') {
System.out.println("No Discount");
}
}
}
}
}

while (true) {
System.out.print("Please enter item " + num++ + " price or 0 to quit: ");
itemPrice = scanner.nextDouble();
amounts.add(itemPrice);
scanner.nextLine();
for (double i : amounts) {
currentTotal = currentTotal + i ;
System.out.println(currentTotal);
}
should be this as previously you where printing the total after the for loop not during
i think you accidentally messed up your curly brackets and indented before

Related

Have loop repeat after invalid entry

All calculating is fine. I run into a problem when I get an invalid entry and the user enters a new positive integer. It doesn't start the loop over again. Any advice is greatly appreciated!
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int start;
int i;
double squareRoot;
System.out.println("Please enter a positive integer: ");
start = in.nextInt();
if (start > 0){
do {
squareRoot = Math.sqrt(start);
start--;
System.out.printf("%.4f", squareRoot);
System.out.println();
}
while (start >= 0);
}
else {
System.out.println("The number you entered is not a postive integer.");
System.out.println("Please enter an integer greater than zero: ");
start = in.nextInt();
}
}
}
If you need to go over the same steps over and over, you need a loop. So add a while loop with a simple condition and get it going over and over.
Here is a sample code. I had to give an option to quit, so took -1 as the cue.
import java.util.Scanner;
public class HelloWorld {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int start;
int i;
double squareRoot;
System.out.println("Please enter a positive integer: ");
boolean quit = false;
while (!quit) {
System.out.println("Enter the integer");
start = in.nextInt();
if (start > 0){
do {
squareRoot = Math.sqrt(start);
start--;
System.out.printf("%.4f", squareRoot);
System.out.println();
}
while (start >= 0);
}
else if (start == -1) {
quit = true;
}
else {
System.out.println("The number you entered is not a postive integer.");
System.out.println("Please enter an integer greater than zero: ");
}
}
}
}

What should i put in the set and what should my condition be?

Compute the income tax due on a taxable income entered by the user. Be sure to include error checking to make sure the user does not enter a negative number. theres a chart as well
http://imgur.com/E5AqNxQ
What's making me stuck is what should be my condition,am i declaring the right thing and what should be the set?
i have
declare incometax, taxdue, as float
call input
call set
call output
input
write " enter taxable income"
input taxable income
end input
set
i dont know.
output
help me
I'm not sure what language you are writing this in, but here's your solution in C. Bear in mind this is using Integers, not floats. So the answer will be truncated. Your problem from the textbook says to assume integer entries, hence why I have done this.
int main()
{
printf("Please enter your income: ");
int iIncome = 0;
int iTaxAmount = 0;
char iChecking = 0;
//use a while loop with our scanf to make sure a valid number is input.
while (iChecking == 0)
{
scanf("%d", &iIncome);
if (iIncome <= 0)
{
printf("Please enter a positive, nonzero number\n");
printf("Please enter your income: ");
}
else
{
iChecking = 1;
}
}
if (iIncome < 50000)
{
iTaxAmount = iIncome * 0.05;
}
else if (iIncome < 100000 && iIncome >= 50000)
{
//set base amount
iTaxAmount = 2500;
int iTaxableOver50k = 0;
//calculate taxable amount over 50k
iTaxableOver50k = iIncome - 50000;
//add base tax to percent value
iTaxAmount = iTaxAmount + iTaxableOver50k * 0.07;
}
else if (iIncome >= 100000)
{
//set base tax amount
iTaxAmount = 6000;
int iTaxableOver100k = 0;
//calculate taxable amount over 100k
iTaxableOver100k = iIncome - 100000;
//add base tax to percent value
iTaxAmount = iTaxAmount + iTaxableOver100k*0.09;
}
printf("\nYou must pay: $%d in tax\n", iTaxAmount);
return 0;
}
Here's your solution using floating point calculations for the final answer.
#include <stdio.h>
int main()
{
printf("Please enter your income: ");
int iIncome = 0;
float iTaxAmount = 0.0f;
char iChecking = 0;
//use a while loop with our scanf to make sure a valid number is input.
while (iChecking == 0)
{
scanf("%d", &iIncome);
if (iIncome <= 0)
{
printf("Please enter a positive, nonzero number\n");
printf("Please enter your income: ");
}
else
{
iChecking = 1;
}
}
if (iIncome < 50000)
{
iTaxAmount = (float)iIncome * 0.05f;
}
else if (iIncome < 100000 && iIncome >= 50000)
{
//set base amount
iTaxAmount = 2500;
int iTaxableOver50k = 0;
//calculate taxable amount over 50k
iTaxableOver50k = iIncome - 50000;
//add base tax to percent value
iTaxAmount = iTaxAmount + (float)iTaxableOver50k * 0.07f;
}
else if (iIncome >= 100000)
{
//set base tax amount
iTaxAmount = 6000;
int iTaxableOver100k = 0;
//calculate taxable amount over 100k
iTaxableOver100k = iIncome - 100000;
//add base tax to percent value
iTaxAmount = iTaxAmount + (float)iTaxableOver100k*0.09f;
}
printf("\nYou must pay: $%.2f in tax\n", iTaxAmount);
return 0;
}

This loop to modify a arraylist of numbers based on arraylist of strings isnt working

So, in my program you input employee information and the salary you input depends on what you input for designation. If temporary, yo input how much they make per hour, if permanent you input what they make per year. At the end, you have the option to sort by salary, so if they choose that then I must multiply the temporary's per-hour by a set number (1920) in order to convert it to what they make per year. I tried this but I got the messages
" The type of the expression must be an array type but it resolved to java.util.ArrayList"
and "The type of the expression must be an array type but it resolved to java.util.ArrayList", I got that second one twice.
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
String Continue = "y";
int Count = 0;
int SortingChoice;
ArrayList<String> Names = new ArrayList<String>();
ArrayList<String> Department = new ArrayList<String>();
ArrayList<String> Designation = new ArrayList<String>();
ArrayList<Float> Salary = new ArrayList<Float>();
//******************************************************//
do
{
System.out.println("Enter Employee Name: ");
String x = in.next();
Names.add(x);
System.out.println("Enter Employee Designation ('temporary or permanent'): ");
String y = in.next();
Designation.add(y);
System.out.println("Enter Employee Department: ");
String z = in.next();
Department.add(z);
System.out.println("Enter Employee Salary: ");
float i = in.nextFloat();
Salary.add(i);
System.out.println("Do you wish to add another employee? ('y'/'n'): ");
Continue = in.next();
Count = Count + 1;
}
while(Continue.equals("y"));
//***********************************************************************//
System.out.println("Enter sorting Criterion Number: 1. Name, 2.Department, 3. Salary. ");
SortingChoice = in.nextInt();
if(SortingChoice == 1)
{
Collections.sort(Names);
for(int i=0; i<Names.size(); i++)
{
System.out.println(Names.get(i));
}
}
if(SortingChoice == 2)
{
Collections.sort(Department);
for(int i=0; i<Department.size(); i++)
{
System.out.println(Department.get(i));
}
}
if(SortingChoice == 3)
{
for(int k=0; k<Salary.size(); k++)
{
if(Designation[k].equals("temporary"))
{
Salary[k] = Salary[k]*1920;
}
}
Collections.sort(Salary);
for(int i=0; i<Salary.size(); i++)
{
System.out.println(Salary.get(i));
}
}
}
What I am most concered about if the loop under if(SortingChoice==3)

reached end of file while parsing,

what is wrong with this , i am getting an error that states: reached end of file while parsing. what do i do to fix this?
i have tried several thing with no result
public class FindMin
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
int smallest = 9999999;
String userInput;
boolean quit = false;
System.out.println("This program finds the smallest number"
+ " in a series of numbers");
System.out.println("When you want to exit, type Q");
while(quit != true)
{
System.out.print("Enter a number: ");
userInput = keyboard.next();
if(userInput.equals("Q")) userInput.equals("q");
{
if(quit == true) {
}
else
{
int userNumber = Integer.parseInt(userInput);
if(UserNumber < smallest)
smallest = userNumber;
}
}
System.out.println("The smallest number is " + smallest);
System.exit(0);
}
Check your braces: they don't match up, which is why the compiler complains:
FindMin.java:35: reached end of file while parsing
Besides there's a typo and some other issues with that program. See other answers for reference :)
It was just too messy, try this
import java.util.Scanner;
public class FindMin{
public static void main(String[] args){
Scanner keyboard = new Scanner(System.in);
int smallest = 9999999;
String userInput;
boolean quit = false;
System.out.println("This program finds the smallest number"
+ " in a series of numbers");
System.out.println("When you want to exit, type Q");
while(true)
{
System.out.print("Enter a number: ");
userInput = keyboard.next();
if(userInput.equalsIgnoreCase("q"))
break;
else{
int userNumber = Integer.parseInt(userInput);
if(userNumber < smallest)
smallest = userNumber;
}
}
System.out.println("The smallest number is " + smallest);
}
}

Error in code, NullPointException

I am working on a program for class due today at midnight and I just can't figure why it's giving me this Null Point Exception error. I would really appreciate if you guys can look at my code and help me out.
The goal of the project is as follows..
Program Statement: Write a Payroll class that uses the following arrays as fields:
employeeId - an array of 7 integers to hold employee id numbers. The array field should be initialized with the following numbers:
5658845
4520125
7895122
8777541
8451277
1302850
7580489
hours - An array of seven integers to hold the number of hours worked by each employee
payRate - An array of seven doubles to hold each employee's hourly rate.
wages - An array of seven doubles to hold each employee's gross wages.
The class should relate the data in each array through the subscripts. For example, the number in element 0 of the hours array should be the number of hours worked by the employee whose ID number is stored in Element 0 of the employeeId array. That same employees pay rate should be stored in Element 0 of the payRate array. In addition to the appropriate accessor and mutator methods, the class should have a method that accepts an employee's id number as an argument and returns the gross pay for that employee. Demonstrate the class in a complete program that displays each employee number and asks the user to enter the employee's hours and pay rate. It should then display each employee's id number and gross wages.
Input Validation : Do not accept negative values for numbers and numbers less than 6.00 for pay rate.
int[] employeeId;
int[] hours;
double[] payRate;
double[] wages;
Scanner kboard = new Scanner(System.in);
public ParrishPayroll(int[] ids){
employeeId = new int[ids.length];
// Copy the values in ids
for (int index = 0; index < ids.length; index++)
employeeId[index] = ids[index];
System.out.println("Employee ID's");
System.out.println(Arrays.toString(employeeId));
System.out.println("Please enter the id number you would like to edit.");
int input = kboard.nextInt();
if(input == 5658845){
int index = 0;
setHours(index);
}
else if(input == 4520125){
int index = 1;
setHours(index);
}
else if(input == 7895122){
int index = 2;
setHours(index);
}
else if(input == 8777541){
int index = 3;
setHours(index);
}
else if(input == 8451277){
int index = 4;
setHours(index);
}
else if(input == 1302850){
int index = 5;
setHours(index);
}
else if(input == 7580489){
int index = 6;
setHours(index);
}
else {
System.out.println("Invalid ID number!");
}
}//end startSequence
public void setHours(int i){
System.out.println("How many hours were worked?");
hours[i] = kboard.nextInt();
if(hours[i] < 0){
System.out.println("Please input a positive number.");
kboard.nextInt(hours[i]);
setPayRate(i);
}
else
setPayRate(i);
}
public void setPayRate(int index){
int input = index;
System.out.println("What is the employee's pay rate?");
payRate[input] = kboard.nextDouble();
if(payRate[input] < 0){
System.out.println("Please input a positive number.");
payRate[input] = kboard.nextDouble();
calcWages(input);
}
else if(payRate[index] < 6.00){
System.out.println("Wages must be higher than $6.00.");
payRate[input] = kboard.nextDouble();
calcWages(input);
}
}
public void calcWages(int index){
int input = index;
wages[input] = hours[input] * payRate[input];
}
public void getGross(int i){
int input = i;
if(input == 5658845){
System.out.print("Employee number: " + input + " wages: " + wages[0]);
}
else if(input == 4520125){
System.out.print("Employee number: " + input + " wages: " + wages[1]);
}
else if(input == 7895122){
System.out.print("Employee number: " + input + " wages: " + wages[2]);
}
else if(input == 8777541){
System.out.print("Employee number: " + input + " wages: " + wages[3]);
}
else if(input == 8451277){
System.out.print("Employee number: " + input + " wages: " + wages[4]);
}
else if(input == 1302850){
System.out.print("Employee number: " + input + " wages: " + wages[5]);
}
else if(input == 7580489){
System.out.print("Employee number: " + input + " wages: " + wages[6]);
}
else {
System.out.println("Invalid ID number!");
getGross(input);
}
}
public static void main(String[] args){
int[] idlist = {5658845, 4520125, 7895122, 8777541, 8451277, 1302850, 7580489};
ParrishPayroll user1 = new ParrishPayroll(idlist);
Scanner kboard = new Scanner(System.in);
System.out.println("What employee would you like to see the gross wages for?");
int i = kboard.nextInt();
user1.getGross(i);
}
It looks like you are not initializing the hours, payRate or wages variables, which means they are all null. When you call, say, hours[i] = kboard.nextInt() it will throw a NullPointerException because you can't set the i th element of null.
Make sure you have a line like hours = new int[whatever] before you try to use the hours variable (same for the other two). You've got it right for employeeId, so follow that pattern.

Resources