Essentially i've isolated the issue, the int numpersons begins as 0. I take a user input to make it a particular number which is the array size, when the second method begins it takes the 0 again and then the array has an out of bounds exception. I want to pass it from one method to the next, or make them more successive, idk how to do this
thanks in advance
import java.util.Scanner;
public class BankApp {
Scanner input = new Scanner(System.in);
int numpersons = 0;
private SavingsAccount[] clients = new SavingsAccount[numpersons];
public BankApp() {
while (numpersons < 1) {
System.out.println("How many people are there?");
numpersons = input.nextInt();
if (numpersons < 1 || 2147483647 < numpersons) {
System.out.println("invalid number, please enter again");
}
}
input.nextLine();
}
public void addClients() {
int i = 0;
while (i < numpersons) {
System.out.println("enter account id " + (i + 1));
String AccountID = input.nextLine();
System.out.println("enter account name " + (i + 1));
String AccountName = input.nextLine();
System.out.println("enter account balance " + (i + 1));
Double AccountBalance = input.nextDouble();
clients[i] = new SavingsAccount(AccountID, AccountName, AccountBalance);
input.nextLine();
i++;
}
}
public void displayClients() {
int i = 0;
while (i < numpersons) {
System.out.println("======================================");
System.out.println("Account ID " + (i + 1) + ": " + clients[i].getID());
System.out.println("Account Name " + (i + 1) + ": " + clients[i].getName());
System.out.println("Account Balance " + (i + 1) + ": " + clients[i].getBalance());
System.out.println("======================================");
i++;
}
}
public static void main(String args[]) {
BankApp ba = new BankApp();
ba.addClients();
ba.displayClients();
}
}
I've been working on a small bit of code that helps me calculate performance of a movie's Box-Office weekend. I want it to require the
Name of the movie
How much it'll cost to use
How much it's predicted to make that weekend
Then I want it to output all the above information, with the addition of each movie's performance. Well, I'm having issues with it saving multiple String[] values as well as int[] values. I can remove the String lines, and all the int[]'s (both the price and the prediction) work fine, or I can remove the int[]'s and the String[] works fine. Can anyone help me find my errors?
import java.util.*;
public class FMLArrays {
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
String[] movieName = new String[15];
int[] moviePrice = new int[15];
int[] moviePrediction = new int[15];
int[] moviePreformance = new int[15];
for (int i = 0; i < movieName.length; i++) {
System.out.print("Enter the name of movie " + (i+1) + ": ");
movieName[i] = console.nextLine();
System.out.print("Enter the price of movie " + (i+1) + ": ");
moviePrice[i] = console.nextInt();
System.out.print("Enetr the prediction of movie " + (i+1) + ": ");
moviePrediction[i] = console.nextInt();
moviePreformance[i] = Math.round(((moviePrediction[i] * 1000000) / moviePrice[i]) * 100) / 100;
System.out.println();
}
for (int i = 0; i < movieName.length; i++) {
System.out.println();
System.out.println("Name: " + movieName[i]);
System.out.println("Price: $" + moviePrice[i]);
System.out.println("Prediction: $" + moviePrediction[i] + " million");
System.out.println("Preformance: $" + moviePreformance[i]);
}
}
}
When you used nextInt() and entered a number you also enterd a new line character, so the next time you called nextLine() this new line character was your next input waiting.
You could fix this by always using nextLine, or by adding a call for nextLine.
import java.util.*;
public class FMLArrays {
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
String[] movieName = new String[2];
int[] moviePrice = new int[2];
int[] moviePrediction = new int[2];
int[] moviePreformance = new int[2];
for (int i = 0; i < movieName.length; i++) {
System.out.print("Enter the name of movie " + (i+1) + ": ");
movieName[i] = console.nextLine();
System.out.print("Enter the price of movie " + (i+1) + ": ");
moviePrice[i] = Integer.parseInt(console.nextLine());
System.out.print("Enetr the prediction of movie " + (i+1) + ": ");
moviePrediction[i] = Integer.parseInt(console.nextLine());
moviePreformance[i] = Math.round(((moviePrediction[i] * 1000000) / moviePrice[i]) * 100) / 100;
System.out.println();
}
for (int i = 0; i < movieName.length; i++) {
System.out.println();
System.out.println("Name: " + movieName[i]);
System.out.println("Price: $" + moviePrice[i]);
System.out.println("Prediction: $" + moviePrediction[i] + " million");
System.out.println("Preformance: $" + moviePreformance[i]);
}
}
}
This should fix the issue:
moviePrice[i] = console.nextInt();
console.nextLine();
moviePrediction[i] = console.nextInt();
console.nextLine();
console.nextInt() only reads the number, not the rest of the line, like carriage return.
I'm writing a simple Java program for my history final project and am having trouble exporting it out of eclipse so it can be run on my teacher's computer (a Mac).
Here is the code:
package project;
import java.util.Scanner;
public class Denim {
public static void main(String []args) {
Scanner scan = new Scanner(System.in);
System.out.println("What item of denim is in question?");
String str = scan.nextLine();
String str1 = str.toUpperCase();
String jeans = "JEANS";
String jeansp = "JEAN PANTS";
String tux = "CANADIAN TUXEDO";
String tux1 = "CANADIEN TUXEDO";
String tux2 = "CANADIAN TUX";
String tux3 = "CANADIEN TUX";
String jacket = "JACKET";
String jjacket = "JEAN JACKET";
String hat = "HAT";
String dhat = "DENIM BASEBALL HAT";
String bhat = "BASEBALL HAT";
String c = "C";
int jeanFactor = 9982;
double jacketFactor = 10466.25178;
double bottleFactor = 128/16.9;
double hatFactor = 314.1415;
if (str1.equals(jeans) || str1.equals(jeansp)){
System.out.println("How many pairs of jeans?");
int numj = scan.nextInt();
int gallonj = numj*jeanFactor;
System.out.println("Producing " + numj + " pairs of jeans would use: ");
System.out.println(gallonj + " gallons of water");
double bottlesj = gallonj*bottleFactor;
System.out.println(bottlesj + " bottles of water");}
else if ((str1.equals(tux)) || (str1.equals(tux1)) || (str1.equals(tux2)) ||(str.equals(tux3))){
System.out.println("How many tuxedos?");
int numt = scan.nextInt();
int gallontp = numt * jeanFactor;
double gallontj = numt * jacketFactor;
double gallont = gallontp + gallontj;
double bottlest = gallont*bottleFactor;
System.out.println("Producing " + numt +" " + c + str.substring(1,8) + " tuexedos would use: ");
System.out.println(gallont +" gallons of water.");
System.out.println(bottlest + " bottles of water");}
else if (str1.equals(jacket) || str.equals(jjacket)){
System.out.println("How many jackets?");
int numjj = scan.nextInt();
double gallonjj = numjj*jacketFactor;
double bottlesjj = numjj*bottleFactor;
System.out.println("Producing " + numjj + " jackets would use: ");
System.out.println(gallonjj + " gallons of water");
System.out.println(bottlesjj + " bottles of water");}
else if (str1.equals(hat) || str.equals(dhat) || str.equals(bhat)){
System.out.println("How many hats?");
int numh = scan.nextInt();
double gallonh = numh*hatFactor;
double bottlesh = numh*bottleFactor;
System.out.println("Producing " + numh + " jackets would use: ");
System.out.println(gallonh + " gallons of water");
System.out.println(bottlesh + " bottles of water");
}
}
}
I click file-export and export it as a .jar file, but every time I try and open it to run it, a message pops up saying "The Java JAR file could not be launched. Does anyone know how to fix this or what I'm doing wrong? Both my computer and my teacher's are Macbooks.
So I can't get the variables to be divisible, I need to be able to do this, otherwise I don't know of a way to finish building the lock that I want to build.
It uses 20 inputted numbers, and then arranges them into a Algebra2/calculus system of equations, and then solves for the "s", "a", "f", and "e" it starts by removing "e" from the equation by substituting.
I would greatly appreciate help, I'm open to ideas as well, because sofar I have 25 of these to build, and this is only 1/3 of the first one.
In short, how do I divide variables?
import java.util.Scanner;
public class Lock
{
public static void main(String[] args) {
Scanner user_input = new Scanner (System.in);
String num_a;
System.out.print("Enter the first number: ");
num_a = user_input.next();
String num_b;
System.out.print("Enter the second number: ");
num_b = user_input.next();
String num_c;
System.out.print("Enter the third number: ");
num_c = user_input.next();
String num_d;
System.out.print("Enter the fourth number: ");
num_d = user_input.next();
String num_e;
System.out.print("Enter the fifth number: ");
num_e = user_input.next();
String num_f;
System.out.print("Enter the sixth number: ");
num_f = user_input.next();
String num_g;
System.out.print("Enter the seventh number: ");
num_g = user_input.next();
String num_h;
System.out.print("Enter the eigth number: ");
num_h = user_input.next();
String num_i;
System.out.print("Enter the ninth number: ");
num_i = user_input.next();
String num_j;
System.out.print("Enter the tenth number: ");
num_j = user_input.next();
String num_k;
System.out.print("Enter the eleventh number: ");
num_k = user_input.next();
String num_l;
System.out.print("Enter the twetlth number: ");
num_l = user_input.next();
String num_m;
System.out.print("Enter the thirteenth number: ");
num_m = user_input.next();
String num_n;
System.out.print("Enter the fourteenth number: ");
num_n = user_input.next();
String num_o;
System.out.print("Enter the fifteenth number: ");
num_o = user_input.next();
String num_p;
System.out.print("Enter the sixteenth number: ");
num_p = user_input.next();
String num_q;
System.out.print("Enter the seventeenth number: ");
num_q = user_input.next();
String num_r;
System.out.print("Enter the eighteenth number: ");
num_r = user_input.next();
String num_s;
System.out.print("Enter the nineteenth number: ");
num_s = user_input.next();
String num_t;
System.out.print("Enter the twentieth number: ");
num_t = user_input.next();
System.out.println(num_a + "s + " + num_b + "a + " + num_c + "f + " + num_d + "e = " + num_e);
System.out.println(num_f + "s + " + num_g + "a + " + num_h + "f + " + num_i + "e = " + num_j);
System.out.println(num_k + "s + " + num_l + "a + " + num_m + "f + " + num_n + "e = " + num_o);
System.out.println(num_p + "s + " + num_q + "a + " + num_r + "f + " + num_s + "e = " + num_t);
System.out.println(num_a + "s + " + num_b + "a + " + num_c + "f + " + num_d + "[(" + num_t + " " + num_p + "s + " + num_q + "a " + num_r + "f) / " + num_s + "] =" + num_e);
System.out.println(num_f + "s + " + num_g + "a + " + num_h + "f + " + num_i + "[(" + num_t + " " + num_p + "s + " + num_q + "a " + num_r + "f) / " + num_s + "] =" + num_j);
System.out.println(num_k + "s + " + num_l + "a + " + num_m + "f + " + num_n + "[(" + num_t + " " + num_p + "s + " + num_q + "a " + num_r + "f) / " + num_s + "] =" + num_o);
// THIS creates the fourth equation items/order to be substituted into the other first three equations.
int t = num_t;
int s = num_s;
int num_ts = (t / s);
num_ts =
num_ps = (num_p / num_s);
num_qs = (num_q / num_s);
num_rs = (num_r / num_s);
// THIS is the Fourth equation being substituted into the First Equation
num_dts = (num_d * num_ts);
num_dps = (num_d * num_ps);
num_dqs = (num_d * num_qs);
num_drs = (num_d * num_rs);
// THIS is the Fourth equation being substituted into the Second Equation
num_its = (num_i * num_ts);
num_ips = (num_i * num_ps);
num_iqs = (num_i * num_qs);
num_irs = (num_i * num_rs);
// THIS is the fourth equation being substituted into the Third Equation
num_nts = (num_n * num_ts);
num_nps = (num_n * num_ps);
num_nqs = (num_n * num_qs);
num_nrs = (num_n * num_rs);
System.out.println(num_a + "s + " + num_b + "a + " + num_c + "f + " + num_dts + " " + num_dps + "s + " + num_dqs + "a " + num_drs + "f = " + num_e);
System.out.println(num_f + "s + " + num_g + "a + " + num_h + "f + " + num_its + " " + num_ips + "s + " + num_iqs + "a " + num_irs + "f = " + num_j);
System.out.println(num_k + "s + " + num_l + "a + " + num_m + "f + " + num_nts + " " + num_nps + "s + " + num_nqs + "a " + num_nrs + "f = " + num_o);
}
}
You can't add, subtract, divide, or multiply String variables. You have to make your variables into ints in order to do that. Also, you can use an array to hold your variables, since there is so many of them.
String, Integer, Float, are not the same types. you can't apply operators like / or * on String for instance. + is special because it has a definition for String, which means concatenate.
Since you need to do some operations on the user inputs, you can read them directly as int:
System.out.print("Enter the first number: ");
int num_a = user_input.nextInt();
System.out.print("Enter the second number: ");
int num_b = user_input.nextInt();
Then you can do
int num_ab = a / b;
Note that if a < b, then num_ab will be 0, since this is an integer. You may want to do something like
float num_ab = (float)a / b;
Now, this code is quite tedious. If you accept to handle indices instead of letters for the variables, you can initialise them in a loop, e.g.
Scanner in = new Scanner(System.in);
int[] numbers = new int[20];
int index = 0;
while (index < numbers.length) {
System.out.println("Enter the "+(index+1)+"th number");
int n = in.nextInt();
numbers[index] = n;
index++;
}
System.out.println(Arrays.toString(numbers));
And use the array of numbers
// arrays start at 0
int num_ab = numbers[0] / numbers[1];
And if you want to be able to access the variables through names, you can define constants
static final int a = 0;
static final int b = 1;
static final int c = 2;
//...
int num_ab = numbers[a] / numbers[b];
But in your case, it may be handy to store initial variables and computed ones in some place where you can retrieve them for further computations:
// the store for all the variables and their value
static Map<String, Integer> vars = new HashMap<>();
// the function to read in the store
static Integer var(String name) {
return vars.get(name);
}
The store is initialised by a loop:
Scanner in = new Scanner(System.in);
// The 20 variables...
String alpha = "abcdefghijklmnopqrst";
for (char c : alpha.toCharArray()) {
String varName = String.valueOf(c);
System.out.println("Enter the value for "+ varName);
int n = in.nextInt();
vars.put(varName, n);
}
System.out.println(vars.toString());
int num_ab = var("a")/var("b");
// Store ab for further computation
vars.put("ab", num_ab);
System.out.println("ab is " + var("ab");
public class PizzaExamfinalOne{
public static void main(String[]args){
double total2 = 0;
String customerName = " ";
String address = " ";
String phoneNum ="0";
int max = 5;
int done = 1;
double totalPizza = 0;
double totalA = 0;
int pizzaChoice =0;
String [] pizzaType = {"Placeholder", "Hawaian" , "Classic Italian" , "Cheese Extreme" , "Veg Delight" , "Pepperoni" , "Tomato Margherita" , "Beef & Onion" , "Super Supreme" , "Meat Lovers" , "Hot 'n Spicy" , "Italiano" , "Italian Veg"};
double [] pizzaPrice = {0, 8.5 , 8.5 , 8.5 , 8.5 , 8.5 , 8.5 , 8.5 , 13.5 , 13.5 , 13.5 , 13.5 , 13.5};
int [] orderDisplay = {0,0,0,0,0,0,0,0,0,0,0,0,0};
boolean pickup = true;
int pickup1 = readInt("If you wish to pickup press 1 or press 0 for delivery");
if (pickup1 ==0){
pickup = false;
}
for (int i = 1; i <pizzaType.length; i++){
System.out.println(i + " " +pizzaType[i] + " $" + pizzaPrice[i]);
}
while (done !=0){
pizzaChoice = readInt ("Choose the type of pizza you wish to order");
double pizzaQuantity = readInt ("How many " + pizzaType[pizzaChoice] + " pizzas do you wish to buy (Maximum is 5)");
totalPizza = totalPizza + pizzaQuantity;
if(totalPizza > max){
System.out.println("You cannot order more than five pizzas");
pizzaChoice = 0;
pizzaQuantity = 0;
}
done= readInt ("Press 0 if you are done or press 1 to make another pizza order");
double total1 = pizzaQuantity*pizzaPrice[pizzaChoice];
total2 = total2 + total1;
}
if(pickup==false){
int deliveryCost = 3;
customerName = readString ("Please enter your full name");
address = readString ("Please enter the Delivery address");
phoneNum = readString ("Please enter the phone number of " + customerName);
totalA = total2 + deliveryCost;
System.out.println("Delivery order for " + customerName);
System.out.println("To " + address + ", Phone number 0" + phoneNum);
}
else{
customerName = readString ("Please enter your full name");
System.out.println("Pickup order for " + customerName);
int deliveryCost = 0;
totalA = total2 + deliveryCost;
}
My problem is here. I need to display the pizzas that the user has ordered along with the amount of pizzas ordered and I'm stuck here. there are no errors in the code it just doesnt display the following
for(int i=1;i<orderDisplay.length;i++){
if(orderDisplay[i]>0){
System.out.println(" " + orderDisplay[i] + " " + pizzaType[i] + " pizzas");
}
}
System.out.println("The total amount of your order is " + formatMoney(totalA));
}
public static int readInt(String prompt){
System.out.println(prompt);
java.util.Scanner keyboard = new java.util.Scanner(System.in);
return keyboard.nextInt();
}
public static String readString (String prompt){
System.out.println (prompt);
java.util.Scanner keyboard= new java.util.Scanner(System.in);
return keyboard.nextLine();
}
public static String formatMoney(double phoneNum){
java.text.NumberFormat fmt = java.text.NumberFormat. getCurrencyInstance();
return(fmt.format(phoneNum));
}
}
You don't add orders to your orderDisplay array.
You should handle it in if(pickup==false){..}
You have set all the array elements of orderDisplay to 0.
int [] orderDisplay = {0,0,0,0,0,0,0,0,0,0,0,0,0};
U never changed the value of orderDispaly elements and checking for value greater than 0 and u have started your loop with value 1. check the below lines.
for(int i=1;i<orderDisplay.length;i++){
if(orderDisplay[i]>0){
System.out.println(" " + orderDisplay[i] + " " + pizzaType[i] + " pizzas");
}
Here is your answer :
public class PizzaExamFinalOne
{
public static void main(String[]args)
{
double total2 = 0;
String customerName = " ";
String address = " ";
String phoneNum ="0";
int max = 5;
int done = 1;
double totalPizza = 0;
double totalA = 0;
int pizzaChoice =0;
String [] pizzaType = {"Placeholder", "Hawaian" , "Classic Italian" , "Cheese Extreme" , "Veg Delight" , "Pepperoni" , "Tomato Margherita" , "Beef & Onion" , "Super Supreme" , "Meat Lovers" , "Hot 'n Spicy" , "Italiano" , "Italian Veg"};
double [] pizzaPrice = {0, 8.5 , 8.5 , 8.5 , 8.5 , 8.5 , 8.5 , 8.5 , 13.5 , 13.5 , 13.5 , 13.5 , 13.5};
int [] orderDisplay = {0,0,0,0,0,0,0,0,0,0,0,0,0};
boolean pickup = true;
int pickup1 = readInt("If you wish to pickup press 1 or press 0 for delivery");
if (pickup1 == 0)
{
pickup = false;
}
while (done != 0)
{
for (int i = 1; i <pizzaType.length; i++)
{
System.out.println(i + " " +pizzaType[i] + " $" + pizzaPrice[i]);
}
pizzaChoice = readInt ("Choose the type of pizza you wish to order");
int pizzaQuantity = readInt ("How many " + pizzaType[pizzaChoice] + " pizzas do you wish to buy (Maximum is 5)");
totalPizza = totalPizza + pizzaQuantity;
if(totalPizza > max)
{
totalPizza = totalPizza - pizzaQuantity;
System.out.println("You cannot order more than five pizzas");
pizzaChoice = 0;
pizzaQuantity = 0;
}
else
{
orderDisplay[pizzaChoice] = pizzaQuantity;
}
done= readInt ("Press 0 if you are done or press 1 to make another pizza order");
double total1 = pizzaQuantity*pizzaPrice[pizzaChoice];
total2 = total2 + total1;
}
if(totalPizza > 0)
{
if(pickup == false)
{
int deliveryCost = 3;
customerName = readString ("Please enter your full name");
address = readString ("Please enter the Delivery address");
phoneNum = readString ("Please enter the phone number of " + customerName);
totalA = total2 + deliveryCost;
System.out.println("Delivery order for " + customerName);
System.out.println("To " + address + ", Phone number 0" + phoneNum);
}
else
{
customerName = readString ("Please enter your full name");
System.out.println("Pickup order for " + customerName);
int deliveryCost = 0;
totalA = total2 + deliveryCost;
}
for(int i=1;i<orderDisplay.length;i++)
{
if(orderDisplay[i]>0)
{
System.out.println(" " + orderDisplay[i] + " " + pizzaType[i] + " pizzas");
}
}
System.out.println("The total amount of your order is " + formatMoney(totalA));
}
else
{
System.out.println("You have not ordered anything.");
}
}
public static int readInt(String prompt)
{
System.out.println(prompt);
java.util.Scanner keyboard = new java.util.Scanner(System.in);
return keyboard.nextInt();
}
public static String readString (String prompt)
{
System.out.println (prompt);
java.util.Scanner keyboard= new java.util.Scanner(System.in);
return keyboard.nextLine();
}
public static String formatMoney(double phoneNum)
{
java.text.NumberFormat fmt = java.text.NumberFormat. getCurrencyInstance();
return(fmt.format(phoneNum));
}
}