int CMC = 279;
int BDM = 326;
int CP = 177;
int CB = 228;
int CR = 190;
int PC = 43;
int CCS = 24;
int CE = 26;
int FM = 20;
int originalpricebake ;
Scanner bakeorder1 = new Scanner(System.in);
int order1;
System.out.print("\nEnter Your 1st Item Order:");
order1 = bakeorder1.nextInt();
Scanner bakeorder2 = new Scanner(System.in);
int order2;
System.out.print("\nEnter Your 2nd Item Order:");
order2 = bakeorder2.nextInt();
Scanner bakeorder3 = new Scanner(System.in);
int order3;
System.out.print("\nEnter Your 3rd Item Order:");
order3 = bakeorder3.nextInt();
Scanner bakeorder4 = new Scanner(System.in);
int order4;
System.out.print("\nEnter Your 4th Item Order:");
order4 = bakeorder4.nextInt();
Scanner bakeorder5 = new Scanner(System.in);
int order5;
System.out.print("\nEnter Your 5th Item Order:");
order5 = bakeorder5.nextInt();
originalpricebake = order1 + order2 + order3 + order4 + order5;
System.out.print("\nThe Current Price: " + originalpricebake);
how can i use the integer values on the top when i scan for an user input for his order? So that when i got the user input an equation will be made at the bottom of the code
Use Array to store the values and access it using loop.
int order;
int price=0;
Scanner scan = new Scanner(System.in);
System.out.print("Enter how many orders: ");
order = Integer.parseInt(scan.nextLine());
//Create an integer array to store the input
int bakeorder[] = new int[order];
//int originalbakeprice[] = new int[order];
for(int i=0;i<order;i++)
{
System.out.print("Enter the order " + (i+1) + " : ");
bakeorder[i] = Integer.parseInt(scan.nextLine());
price +=bakeorder[i];
}
System.out.println(price);
Related
My code breaks when the user is supposed to input a unique number for itemNumber, however there is no error displayed in NetBeans, can someone help me with why this happens?
package pre_release;
import java.util.*;
public class Pre_Release {
public static void main(String[] args) {
int numberOfItems = 0;
int number;
int bidNumber;
double highestBid = 0;
Scanner userInput = new Scanner (System.in);
Scanner userString = new Scanner (System.in);
Scanner userDouble = new Scanner (System.in);
Scanner userBuyer = new Scanner (System.in);
Scanner userInt = new Scanner (System.in);
Scanner userItem = new Scanner (System.in);
do{
System.out.println("Please enter the the amount of items for the auction. Needs to be more than or equal to 10");
numberOfItems = userInput.nextInt();
} while(numberOfItems<10);
String[] description = new String[numberOfItems];
double[] reservePrice = new double[numberOfItems];
double[] bid = new double[numberOfItems];
int[] itemNumber;
itemNumber = null;
int[] buyNumber = new int[numberOfItems];
for(int count=0;count<numberOfItems;count++){
System.out.println("Input a number for each item number");
itemNumber[count] = userInt.nextInt();
}
for(int count=0;count<numberOfItems;count++){
bidNumber=0;
System.out.println("Please give your item a reserved price");
reservePrice[count] = userDouble.nextDouble();
System.out.println("Please describe your item");
description[count] = userString.nextLine();
}
System.out.println("Please enter the desired item you wish to see");
number = userInt.nextInt();
for(int count2=0;count2<numberOfItems;count2++){
if(itemNumber[count2] == number){
System.out.println("The reserved price is" + reservePrice[numberOfItems]);
System.out.println(description[numberOfItems]);
}
System.out.println("Would you like to put a bid on this item? Needs to be more than" + reservePrice[numberOfItems]);
}
}
}
Haven't seen your error message, but probably you've got the error because your array reference is null
int[] itemNumber;
itemNumber = null
In order to fix this, replace these lines with this one:
int[] itemNumber = new int[numberOfItems];
Having trouble getting my program to output the index[1] of my array "nArray", if nArray[0] = bob, and nArray[1] = jim. When I'm trying to print the input, it will print nArray[0] bob, but when it gets to nArray[1] it does not output.
public String toString(){
return getClass().getName() + "#" + Integer.toHexString(hashCode());
}
public static double salePercent(double[] sArray, String[] nArray){
double total = 0;
for (int b =0 ; b < sArray.length; b++){ // sum calculator
total = total + sArray[b];
}
double percent = 0;
for (int k = 0; k < nArray.length; k++){
System.out.println(nArray[k]);
}
return total;
}
edit
showing how i am creating and declaring and creating my arrays
in my mainclass
System.out.println("How Many Employees :");
int size = input.nextInt();
input.nextLine(); //dummy
String[] nArray = new String[size]; // array for staff names
double[] sArray = new double[size]; // array for staff sales
for (int i = 0 ; i < size; i++){
sales.nameArray(nArray,i, input);
input.nextLine();
sales.saleArray(sArray,i,input);
}
sales.salePercent(sArray, nArray);
in my class
public String inputStaff(){
Scanner user = new Scanner(System.in);
int size;
System.out.print("How Many Employees :\n");
size = user.nextInt();
user.nextLine(); //dummy to grab \n value from nextInt so nextline can function
String[] nArray =new String[size];
double[] dArray = new double[size];
int i =0;
for(i = 0 ; i < nArray.length;i++){
System.out.print("Enter name: ");
nArray[i] = user.nextLine();
System.out.print("Enter sales ($): ");
dArray[i] = user.nextDouble();
user.nextLine();
}
return null;
}
public static String[] nameArray(String[] nArray,int i,Scanner input){
System.out.print("Enter name: ");
nArray[i] = input.nextLine();
return nArray;
}
public static double[] saleArray(double[] sArray,int i,Scanner input){
System.out.print("Enter sales ($): ");
sArray[i] = input.nextDouble();
return sArray;
}
For my university lab work we have to finish 4 tasks. I'm currently on 6 of 9 and have for the most part completed it, but I'm having difficulty in completing the final parts of it. This is the description of what we must do:
Write a program that defines two arrays - one of strings and one of integers, both of size 10.
Your program should then ask the user to enter the a string representing a persons name,
and an integer representing their age. It should continue to do this until either the user
enters ‘done’ instead of a name, or until the array is full (that is, 10 pairs of names and ages
have been entered). It should then print out the names and ages as well as the names of the
youngest and oldest.
Hint: One tricky part is making sure that once you’ve typed ‘done’ to Finish entering names,
your program does not then ask you for the age of the person with name ‘done’ - be careful
about this.
I've highlighted the issues I'm having above in bolded text. Below is the code I currently have, but I'm not sure how to properly accomplish the bolded text.
import java.util.Scanner;
import java.util.Arrays;
import java.util.Collections;
public class nameAge {
public static void main(String[] args){
String[] name = new String[10];
int[] age = new int[10];
Scanner in = new Scanner(System.in);
String NAME_REQUEST = ("Please enter name");
String AGE_REQUEST = ("Please enter age");
System.out.println("Please enter the name of a person and then their age. Do this for up to 10 people and once finished, type 'done'");
name[0] = in.nextLine();
System.out.println(AGE_REQUEST);
age[0] = in.nextInt();
System.out.println(NAME_REQUEST);
name[1] = in.next();
System.out.println(AGE_REQUEST);
age[1] = in.nextInt();
System.out.println(NAME_REQUEST);
name[2] = in.next();
System.out.println(AGE_REQUEST);
age[2] = in.nextInt();
System.out.println(NAME_REQUEST);
name[3] = in.next();
System.out.println(AGE_REQUEST);
age[3] = in.nextInt();
System.out.println(NAME_REQUEST);
name[4] = in.next();
System.out.println(AGE_REQUEST);
age[4] = in.nextInt();
System.out.println(NAME_REQUEST);
name[5] = in.next();
System.out.println(AGE_REQUEST);
age[5] = in.nextInt();
System.out.println(NAME_REQUEST);
name[6] = in.next();
System.out.println(AGE_REQUEST);
age[6] = in.nextInt();
System.out.println(NAME_REQUEST);
name[7] = in.next();
System.out.println(AGE_REQUEST);
age[7] = in.nextInt();
System.out.println(NAME_REQUEST);
name[8] = in.next();
System.out.println(AGE_REQUEST);
age[8] = in.nextInt();
System.out.println(NAME_REQUEST);
name[9]= in.next();
System.out.println(AGE_REQUEST);
age[9] = in.nextInt();
System.out.println(NAME_REQUEST);
int size = name.length;
int sizeN = age.length;
for (int i=0; i < size; i++) {
System.out.println("Name: " + name[i]);
System.out.println("Age: " + age[i]);
}
int smallest = age[0];
int largetst = age[0];
for(int i=1; i< age.length; i++)
{
if(age[i] > largetst)
largetst = age[i];
else if (age[i] < smallest)
smallest = age[i];
}
System.out.println("Largest Number is : " + largetst);
System.out.println("Smallest Number is : " + smallest);
}
}
You have to take a look on loop doc in java
this code may help you
public static void main(String[] args) {
int youngest =0,older=0;
String[] name = new String[10];
int[] age = new int[10];
String NAME_REQUEST = ("Please enter name");
String AGE_REQUEST = ("Please enter age");
for(int i=0 ; i< 10;i++){
Scanner in = new Scanner(System.in);
System.out.println(NAME_REQUEST);
String tmpName = in.nextLine();
if(tmpName.equalsIgnoreCase("done"))
break;
name[i] = tmpName;
System.out.println(AGE_REQUEST);
age[i] = in.nextInt();
if(age[i] > age[older])
older = i;
if(age[i] < age[youngest])
youngest = i;
}
System.out.println("OLDER is : " + name[older]);
System.out.println("Younger : " + name[youngest]);
}
Try this out, I have tested it and it's working fine. Hope that helps. Happy coding.
package com.pearson.nextgen.aggregatedsessionservice;
import java.util.Scanner;
public class NameAgeTest {
public static void main(String[] args) {
String[] name = new String[10];
int[] age = new int[10];
Scanner in = new Scanner(System.in);
String NAME_REQUEST = "Please enter name";
String AGE_REQUEST = "Please enter age";
int count = 0;
while (count < 10) {
System.out.println(NAME_REQUEST);
String nameInput = in.next();
if (nameInput.equalsIgnoreCase("done")) {
break;
}
name[count] = nameInput;
System.out.println(AGE_REQUEST);
age[count] = in.nextInt();
count++;
}
int[] minAndMaxIndex = findMinAndMaxIndex(age, count);
System.out.println("Youngest Person: " + name[minAndMaxIndex[0]]);
System.out.println("Oldest Person: " + name[minAndMaxIndex[1]]);
}
private static int[] findMinAndMaxIndex(int[] inputArray, int count) {
int min, max = 0;
int minIndex = 0, maxIndex = 0;
max = min = inputArray[0];
for (int i = 0; i < count; i++) {
if (inputArray[i] > max)
maxIndex = i;
else if (inputArray[i] < min)
minIndex = i;
}
return new int[] { minIndex, maxIndex };
}
}
I have to Create a RandomDataGenerator. The code I have kind of works, but I am having trouble with some parts of it. The fist issue I am having is with the integers. this is what I get when I run it:
run:
What do you want to generate?
i = integer, d = double, c = char
i
How many integers do you want?
10
Choose low value for your integer:
3
Choose high value for your integer:
7
Integers:
4
3
6
6
6
5
2
3
5
6
It gives me the 10 numbers, but it is not supposed to go lower than 3.
The other issue I'm having is with the characters this is what I get
run:
What do you want to generate?
i = integer, d = double, c = char
c
How many characters do you want?
5
Choose low limit for your character:
C
Choose high limit for your character:
S
P
false
how can I fix this problems?
package javaapplication5;
import java.util.Random;
import java.util.Scanner;
public class JavaApplication5 {
private static int a;
public static void main(String[] args) {
System.out.println("What do you want to generate? ");
System.out.println("i = integer, d = double, c = char" );
char i, d, c;
int low_int = 0,high_int = 0, quant_int = 0;
double low_double = 0, high_double = 0, quant_double = 0;
char low_char = 0, high_char = 0, quant_char = 0;
int result = 0;
String input;
Scanner in = new Scanner (System.in);
String A= in.next();
int largo = A.length();
char select []= new char [largo];
select[a]=A.charAt(a);
for(int q=0; q<largo;q++){
if (select[q]=='i') {
System.out.println( " How many integers do you want? ");
Scanner Integerswanted = new Scanner(System.in);
String B= in.next();
quant_int = Integer.parseInt(B);
System.out.println( " Choose low value for your integer: ");
Scanner LoVa = new Scanner(System.in);
String LV= in.next();
low_int = Integer.parseInt(LV);
System.out.println( " Choose high value for your integer: ");
Scanner HiVa = new Scanner(System.in);
String hV= in.next();
high_int = Integer.parseInt(hV);
System.out.println( " Integers: ");
int random_generator = JavaApplication5.random_generator(quant_int, low_int, high_int);
int s = random_generator;
if(s>low_int && s<high_int){
}
}
else if (select[q]=='d'){
System.out.println( " How many doubles do you want? ");
Scanner DoublesWanted = new Scanner(System.in);
String R= in.next();
quant_double = Double.parseDouble(R);
System.out.println(" Choose low value for your double: ");
Scanner LoDo = new Scanner(System.in);
String LD= in.next();
low_double = Double.parseDouble(LD);
System.out.println(" Choose high value for your double: ");
Scanner HiDo = new Scanner(System.in);
String HD= in.next();
high_double = Double.parseDouble(HD);
System.out.println( " Doubles: ");
double random_generator = JavaApplication5.random_generator(quant_double, low_double, high_double);
double k = random_generator;
}
else if (select[q]=='c'){
System.out.println( " How many characters do you want? ");
Scanner CharWanted = new Scanner(System.in);
String Cw= in.next();
quant_char = A.charAt(0);
System.out.println( " Choose low limit for your character: ");
Scanner LoCh = new Scanner(System.in);
String LC= in.next();
low_char = A.charAt(0);
System.out.println( " Choose high limit for your character: ");
Scanner HiCh = new Scanner(System.in);
String HC= in.next();
high_char = A.charAt(0);
char random_generator = JavaApplication5.random_generator(quant_char, low_char, high_char);
char e = random_generator;
} }}
public static int random_generator(int quant, int low, int high){
Random rand_num = new Random();
int return_int = 0;
for(int z = low; z <= quant + 2 ; z++)
System.out.println(rand_num.nextInt(high) + 1 );
return return_int;
}
public static double random_generator(double quant, double low, double high){
Random rand_num = new Random();
double return_double = 0;
for(double z = low; z <quant + 2; z++) {
System.out.println(rand_num.nextDouble() + low );
}
return return_double;
}
public static char random_generator(char quant, char low, char high){
String [] abecedario = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J",
"K", "L", "M","N","O","P","Q","R","S","T","U","V","W", "X","Y","Z" };
int Rand_num = (int) Math.round(Math.random() * 26 ) ;
System.out.println( abecedario[Rand_num] );Random
rand_num = new Random();
char return_char = 0;
for(double z = low; z < quant + 1; z++)
System.out.println(abecedario[Rand_num].equals(high));
return return_char;
}
}
For the integer, you should use:
rand_num.nextInt(high - low) + high;
For the Character, you should use the same thing except you need to typecast it to a character after you find the random integer.
(char)rand_num.nextInt(high - low) + high;
I have been working on this for a while, but can't quite seem to figure out how to add an item to an ArrayList. I would like to add grocItem (should be 7 grocItems from user input from for loop) into an ArrayList of grocList:
public class ItemData{
public ItemData(String name, double cost, int priority){
Main.(ArrayList grocList).add(grocItem);
// Main.groclist.add(grocItem);
}
}
Main Class:
import java.util.*;
public class Main {
public static List<ItemData> itemData = new ArrayList<ItemData>();
public static void main(String[] args) {
int i=0;
//String name1;
//int priority1;
//double cost1;
String[] item = new String[7];
for (i=0; i<item.length; i++) {
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter item name " + i);
String name = keyboard.next();
Scanner keyboard2 = new Scanner(System.in);
System.out.println("Enter the price of item " + i);
double cost = keyboard2.nextDouble();
Scanner keyboard3 = new Scanner(System.in);
System.out.println("Enter Priority Number " + i);
int priority = keyboard3.nextInt();
ItemData grocItem = new ItemData(name, cost, priority);
}
//How do I add grocItem to an Array list of other grocItems (6 grocItems from user input array item)
Main.itemData.add(groclist);
}
}
Change your code, add the method inside the loop.
for (i=0; i<item.length; i++) {
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter item name " + i);
String name = keyboard.next();
Scanner keyboard2 = new Scanner(System.in);
System.out.println("Enter the price of item " + i);
double cost = keyboard2.nextDouble();
Scanner keyboard3 = new Scanner(System.in);
System.out.println("Enter Priority Number " + i);
int priority = keyboard3.nextInt();
ItemData grocItem = new ItemData(name, cost, priority);
itemData.add(grocItem ); // add here
}
You should add the ItemData object to your arraylist inside your loop:
for (i=0; i<item.length; i++) {
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter item name " + i);
String name = keyboard.next();
Scanner keyboard2 = new Scanner(System.in);
System.out.println("Enter the price of item " + i);
double cost = keyboard2.nextDouble();
Scanner keyboard3 = new Scanner(System.in);
System.out.println("Enter Priority Number " + i);
int priority = keyboard3.nextInt();
ItemData grocItem = new ItemData(name, cost, priority);
itemData.add(groclist); // <-- add to arraylist inside the loop
}