I want to get all the input values from entered Sales amount but it only gives me the last value I input , and also to display all amount . thanks
This is the ouput looks like.
Please enter the number of salespeople to be processed: 3
Enter and ID number and Sales Amount seperated by a space: 1 150
Enter and ID number and Sales Amount seperated by a space: 2 250
Enter and ID number and Sales Amount seperated by a space: 3 350
Weekly Sales by Salesperson 0 1 2 .
the 3 amount of sales should be displayed here.
Total Sales: 1800
{
int i = 0;
System.out.print("Please enter the number of salespeople to be processed: ");
howmany = input.nextInt();
int[] array = new int[howmany];
while ( i <= array.length - 1 )
{
System.out.print("Enter and ID number and Sales Amount seperated by a space: ");
numberOfSales = input.nextInt();
Salesmoney = input.nextInt();
addwan += Salesmoney;
i++;
}
HAHA();
for (int j = 0; j < i ; j++)
{
System.out.print( + j + " ");
addwan += Salesmoney;
System.out.print(Salesmoney + " ");
}
System.out.println("\nTotal Sales: " + addwan + " ");
}
public static void HAHA()
{
System.out.println("\n\t\t\t\tWeekly Sales by Salesperson");
}
}
roughly scetched, I guess you would want to have smthg like this:
System.out.print("Please enter the number of salespeople to be processed: ");
howmany = input.nextInt();
int[][] array = new int[2][howmany];//2D array to store the amount, and worth of the item
for (int i=0; i<howmany;i++ )
{
System.out.print("Enter and ID number and Sales Amount seperated by a space: ");
array[0][i] = input.nextInt();
array[1][i] = input.nextInt();
}
System.out.println("\n\t\t\t\tWeekly Sales by Salesperson");
int addwan=0;
for (int j = 0; j < howmany ; j++)
{
System.out.println( "Eintrag: "+j+" number of sales: "+array[0][j]+" itemworth: "+array[1][j]);
addwan+=array[0][j]*array[1][j];
}
System.out.println("\nTotal Sales: " + addwan + " ");
Guessed i right that your ID-number has to be multiplied with the amount? because i coundt figure out the 1800 sales... even thou they are 1700...
import java.util.Scanner;
public class Exchange {
public static void main(String[] args){
int howmany;
int totalAmount;
int[] arrayForId;
int[] arrayForSales;
Scanner inputData = new Scanner(System.in);
totalAmount = 0;
System.out.print("Please enter the number of salespeople to be processed: ");
howmany = inputData.nextInt();
arrayForId = new int[howmany];
arrayForSales = new int[howmany];
for(int i = 0; i < arrayForId.length; i++){
System.out.print("Enter an ID number and Sales Amount seperated by a space: ");
arrayForId[i] = inputData.nextInt();
arrayForSales[i] = inputData.nextInt();
}
System.out.println("\n\t\t\t\tWeekly Sales by Salesperson");
for (int i = 0; i < arrayForId.length ; i++){
System.out.println("Id: " + arrayForId[i] + "\t Amount: " + arrayForSales[i]);
totalAmount += arrayForSales[i];
}
System.out.println("\nTotal Sales: " + totalAmount);
inputData.close();
}
}
hope this will help you.
Related
This is a Java question and which I have a problem with the while loop. The program must allow the customer to enter the number of liters of petrol they wish to purchase and the liter value is $1.75. Then, for each
liter of petrol up to the quantity that the customer has entered, the program must display a
running total.
and this is the expected output and which I am not getting
Please Enter the Litres of Petrol (a whole number): 20
Litre 0: $0.0
Litre 1: $1.75
Litre 2: $3.5
...
Litre 19: $33.25
Litre 20: $35.0
and this is my code so far.
public static void main(String[] args) {
Scanner kb = new Scanner(System. in );
int numOfLiter;
System.out.println("Please enter the liters of Petrol: ");
numOfLiter = kb.nextInt();
double pricePerLitre = 1.75;
int count = 0;
while (count <= 10) {
Double total = pricePerLitre + count;
System.out.println("Liter " + count + ": " + total);
count++;
}
}
As per your output, your while loop should loop till the input provided i.e numOfLiter
int count = 0;
while (count <= numOfLiter) {
Double total = pricePerLitre * count;
System.out.println("Liter " + count + ": " + total);
count++;
}
And, also
Double total = pricePerLitre + count
should be
Double total = pricePerLitre * count
You can use a simple for-loop to achieve the same.
public static void main(String[] args) {
Scanner kb = new Scanner(System. in );
int numOfLiter;
System.out.println("Please enter the liters of Petrol: ");
numOfLiter = kb.nextInt();
double pricePerLitre = 1.75;
for (int i = 0; i <= numOfLiter; i++) {
Double total = pricePerLitre * i;
System.out.println("Liter " + i + ": " + total);
}
}
Try
Double total = pricePerLitre * count;
Instead of :
Double total = pricePerLitre + count;
In your code.
public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
System.out.println("Please enter the liters of Petrol: ");
int numOfLiter = kb.nextInt();
double pricePerLitre = 1.75;
IntStream.range(0, numOfLiter + 1).forEach(i -> System.out.println("Liter " + i + ": " + i* pricePerLitre));
}
But in real code better use BigDecimal
Have each department's number of computers stored in variables. Have the program store the values in variables, calculate the total and average computers and display them.
example output:
Chemistry: 4
Physics: 8
Music: 2
Math lab: 12
Total: 26
Average: 6.5
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("What is the name of your first class?");
String class1 = sc.nextLine();
System.out.print("What is the name of your second class?");
String class2 = sc.nextLine();
System.out.print("What is the name of your third class?");
String class3 = sc.nextLine();
System.out.print("What is the name of your fourth class?");
String class4 = sc.nextLine();
System.out.print(" \n\n");
System.out.println("How many computers are in each class?");
System.out.print(class1 + ": \t");
int class1comp = sc.nextInt();
System.out.print(class2 + ": \t");
int class2comp = sc.nextInt();
System.out.print(class3 + ": \t");
int class3comp = sc.nextInt();
System.out.print(class4 + ": \t");
int class4comp = sc.nextInt();
int sum = class1comp + class2comp + class3comp + class4comp;
double avg = sum / 4.0;
System.out.print(" \n\n");
System.out.println("\n\n" + class1 + ":\t" + class1comp);
System.out.println(class2 + ":\t" + class2comp);
System.out.println(class3 + ":\t" + class3comp);
System.out.println(class4 + ":\t" + class4comp);
System.out.println("\n");
System.out.println("Total:\t\t" + sum);
System.out.println("Average:\t" + avg);
}
}
After unit 2: Allow the user to add more departments.
I want the user to be able to add more classes until they say stop. Then later ask how many computers each class needs. Then display them, add them to the sum and average.
This should work for your purposes , it uses an ArrayList for the class names and an array of integers for the grades. It uses the AddOrdinal method taken from this answer.
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
ArrayList<String> stringList = new ArrayList<>();
String capture;
int count =1;
System.out.println("Please enter your "+AddOrdinal(count) +" class:");
while (!((capture = scan.nextLine()).toLowerCase().equals("stop"))) {
count++;
stringList.add(capture);
System.out.println("Please enter your "+AddOrdinal(count) +" class:");
}
System.out.println("How many computers are in each class?");
int[] intList = new int[stringList.size()];
for (int i = 0; i < stringList.size(); i++) {
String className = stringList.get(i);
System.out.println(className + "\t:");
intList[i] = (scan.nextInt());
}
scan.close();
Arrays.stream(intList).sum();
int sum = Arrays.stream(intList).sum();
double average = (double)sum/intList.length;
/*
Output goes here
*/
}
For my program, I need to display the item description, quantity, and price in a chart-like format. So that means that description of item 1 would be in line with its price and quantity. So far, I've tried several methods I found on the Internet but haven't succeeded. I am using Dr. Java so please suggest something that is compatible with that compiler.
Thank you in advance!
Here's what I have so far:
public static void main(String []args){
Scanner input=new Scanner(System.in);
String sentinel = "End";
String description[] = new String[100];
int quantity[] = new int[100];
double price [] = new double[100];
int i = 0;
// do loop to get input from user until user enters sentinel to terminate data entry
do
{
System.out.println("Enter the Product Description or " + sentinel + " to stop");
description[i] = input.next();
// If user input is not the sentinal then get the quantity and price and increase the index
if (!(description[i].equalsIgnoreCase(sentinel))) {
System.out.println("Enter the Quantity");
quantity[i] = input.nextInt();
System.out.println("Enter the Product Price ");
price[i] = input.nextDouble();
}
i++;
} while (!(description[i-1].equalsIgnoreCase(sentinel)));
// companyArt();
//System.out.print(invoiceDate());
//System.out.println(randNum());
System.out.println("Item Description: ");
System.out.println("-------------------");
for(int a = 0; a <description.length; a++){
if(description[a]!=null){
System.out.println(description[a]);
}
}
System.out.println("-------------------\n");
System.out.println("Quantity:");
System.out.println("-------------------");
for(int b = 0; b <quantity.length; b++){
if(quantity[b]!=0){
System.out.println(quantity[b]);
}
}
System.out.println("-------------------\n");
System.out.println("Price:");
System.out.println("-------------------");
for(int c = 0; c <price.length; c++){
if(price[c]!=0){
System.out.println("$"+price[c]);
}
}
System.out.println("-------------------");
//This is where I multiply the price and quantity together to get the total
double total = 0.0;
for (int j = 0; j < quantity.length; j++){
total += quantity[j] * price[j];
}
if(total != 0){
System.out.println("Total: " + total);
}
}
}
Your code prints out a list of item descriptions, followed by a list of quantities, followed by a list of prices. I'm assuming this is not how you want it to look. The best solution would be to use a single for loop that prints each of these three things out per line. The following code prints out a table with three columns labelled "Item Description", "Quantity", and "Price", with each row representing an item, and dashed lines separating each row:
System.out.println("Item Description:\tQuantity:\tPrice:\t");
System.out.println("---------------------------------------------------------");
for(int a = 0; a <description.length; a++){
if (description[a].equalsIgnoreCase("end")) {
break;
}
System.out.println(description[a] + "\t\t" + quantity[a] + "\t\t" + price[a]);
System.out.println("---------------------------------------------------------\n");
}
The output will be formatted like this:
Item Description: Quantity: Price:
---------------------------------------------------------
asdfaoiew;rjlkf 248 4309.0
---------------------------------------------------------
asodifaasd 43 2323.0
---------------------------------------------------------
asdfasoif 234 2.0
---------------------------------------------------------
If the columns don't line up properly, you'll need to either add or remove one "\t" after the description, depending on how long or short your item descriptions are.
I am new to stackoverflow. First I would like the program to loop with a price, then a question(enter another price?), price, then a question and so on. Below is the output.
Please enter a price:
33
Enter another price?
y
Please enter a price:
66
Please enter a price:
99
Please enter a price:
22
However it will keep looping at the end with "Please enter a price:". I want it to do:
Please enter a price:
33
Enter another price?
y
Please enter a price:
66
Enter another price?
y
Please enter a price:
22
Can anyone help me with this? Also, sometimes the average does not update fully. Thanks :)
import java.util.Scanner;
public class ReadInPrice {
public static void main(String[] args) {
int integer = 0;
int count = 0;
double sum = 0;
double average = 0;
Scanner input = new Scanner(System.in);
String addPrice;
System.out.println("Please enter a price: ");
integer = input.nextInt();
do {
System.out.println("Enter another price? ");
addPrice = input.next();
while (addPrice.equalsIgnoreCase("Y")) { // change this line to while user response = no etc may need a enter another number?
count = count + 1;
sum = sum + integer;
System.out.println("Please enter a price: ");
integer = input.nextInt();
}
}
while (addPrice.equalsIgnoreCase("Y"));
average = sum / count;
System.out.println("Average = " + average);
input.close();
}
}
You need to replace your while with an if
if (addPrice.equalsIgnoreCase("Y")) { // change this line to while user response = no etc may need a enter another number?
count = count + 1;
sum = sum + integer;
System.out.println("Please enter a price: ");
integer = input.nextInt();
}
In fact, addPrice is not modified within your second while loop, and so you have an infinite loop.
In order to do the averaged price, you're in the right way but not in the right place :P
count = count +1 and sum = sum + integer should be done after each integer = input.nextInt(). In your current code, you don't increment the counter and don't add the integer for the last input.
System.out.println("Please enter a price: ");
integer = input.nextInt();
count++ ; // count = count +1
sum += integer ; // sum = sum + integer
do {
System.out.println("Enter another price? ");
addPrice = input.next();
while (addPrice.equalsIgnoreCase("Y")) { // change this line to while user response = no etc may need a enter another number?
System.out.println("Please enter a price: ");
integer = input.nextInt();
count++ ; // count = count +1
sum += integer ; // sum = sum + integer
}
}
while (addPrice.equalsIgnoreCase("Y"));
Finally here is a improved version which avoid the use of if.
int sum = 0;
int integer = 0;
String addPrice = "Y";
while( "Y".equalsIgnoreCase(addPrice) ) {
System.out.println("Please enter a price: ");
integer = input.next();
sum += integer ;
count++;
System.out.println("Enter another price? ");
addPrice = input.next();
}
int avg = sum / count ;
What you should do is change your logic a bit. You need to repeat two actions, entering a price and asking if the user wants to enter another price. Only one loop is required for this.
do {
System.out.println("Please enter a price: ");
integer = input.nextInt();
count = count + 1;
sum = sum + integer;
System.out.println("Enter another price? ");
addPrice = input.next();
} while (addPrice.equalsIgnoreCase("Y"));
i think you want something like this:
import java.util.Scanner;
public class ReadInPrice {
public static void main(String[] args) {
int integer = 0;
int count = 0;
double sum = 0;
double average = 0;
Scanner input = new Scanner(System.in);
String addPrice = "Y";
while (addPrice.equalsIgnoreCase("Y")){
System.out.println("Please enter a price: ");
integer = input.nextInt();
count++;
sum += integer;
System.out.println("Enter another price? ");
addPrice = input.next();
}
average = sum / count;
System.out.println("Average = " + average);
input.close();
}
}
Try this:
EDIT Added min and max.
public class ReadInPrice {
public static void main(String[] args) {
//better to use 2 scanners when dealing with both string and int
//one for string ; one for ints
Scanner strScanner = new Scanner(System.in);
Scanner intScanner = new Scanner(System.in);
boolean enter = true;
int sum = 0;
int count = 0;
int min=Integer.MAX_VALUE;
int max=0;
while (enter) { //while user wants to keep adding numbers
System.out.println("Please enter a price: ");
int price = intScanner.nextInt();
if(price < min)
min=price;
if(price > max)
max=price;
sum += price;
count++;
System.out.println("Enter another price? ");
String answer = strScanner.nextLine();
if (!answer.equalsIgnoreCase("Y"))
enter = false; //user doesn't want to keep adding numbers - exit while loop
}
double average = (double)sum / count;
System.out.println("Average = " + average);
System.out.println("Min = " + min);
System.out.println("Max = " + max);
strScanner.close();
intScanner.close();
System.exit(0);
}
}
/*
* (Sort students) Write a program that prompts the user to enter the number of students,
*the students’ names, and their scores, and prints student names in decreasing
*order of their scores.
*/
package homework6_17;
import java.util.Scanner;
public class Homework6_17 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter number of students: ");
int numberOfStudents = input.nextInt();
String[] names = new String[numberOfStudents];
for (int i = 0; i < numberOfStudents; i++) {
System.out.println("Enter the name of student: ");
names[i] = input.nextLine();
}
double[] scores = new double[numberOfStudents];
for (int i = 0; i < numberOfStudents; i++) {
System.out.println("Enter the score of student: ");
scores[i] = input.nextDouble();
}
String temps = "";
double temp = 0;
double max = scores[0];
for(int i = 0; i<(scores.length-1); i++){
if(scores[i+1]>scores[i]){
temp=scores[i+1];
scores[i]=scores[i+1];
scores[i+1]=scores[i];
temps = names[i+1];
names[i]=names[i+1];
names[i+1]=names[i];
}
}
for(int i = 0 ; i<(scores.length-1); i++)
System.out.println(names[i]+ " " + scores[i]);
}
}
When i run this program;
run:
Enter number of students: 3
Enter the name of student:
Enter the name of student:
a
Enter the name of student:
b
Enter the score of student:
c
Exception in thread "main" java.util.InputMismatchException
// i got " Enter the name of student: " twice times instead of one.
The first thing that comes to mind (not sure if it is correct here) is that you type the number of students and press "enter". It reads the first int (the 3) and reads the "enter" as the first input for the first student.
Maybe try int numberOfStudents = Integer.ParseInt(input.nextLine());?
That way the newline wont be added to the students.
You just have to remove the first System.out.print("Enter number of students: "); as you are printing the phrase in your for loop for every student. Therefore you are printing it twice for the first student (one time before your loop and one time in your loop)
It's not a good idea to answer homework question in SO. But since you have tried some code, It's OK to answer the Q. Take a look:
import java.util.Scanner;
public class Homework6_17 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter number of students: ");
int numberOfStudents = input.nextInt();
String[] names = new String[numberOfStudents];
for (int i = 0; i < numberOfStudents; i++) {
System.out.println("Enter the name of student #" + (i + 1) + ":");
names[i] = input.next();
}
double[] scores = new double[numberOfStudents];
for (int i = 0; i < numberOfStudents; i++) {
System.out.println("Enter the score of student " + names[i] + ":");
scores[i] = input.nextDouble();
}
String tempName;
double tempScore;
for (int i = 0; i < numberOfStudents; i++) {
for (int k = i + 1; k < numberOfStudents; k++) {
if (scores[k] > scores[i]) {
tempName = names[i];
tempScore = scores[i];
names[i] = names[k];
scores[i] = scores[k];
names[k] = tempName;
scores[k] = tempScore;
}
}
}
for (int i = 0; i < numberOfStudents; i++)
System.out.println(names[i] + " " + scores[i]);
}
}