I'm currently working on an offline Jackpot system for fun, and i got stuck with the ticket problem.
Basically I want it to be (which seems to me like the best way to do it) so for every 10$ a user enters, you'll get 1 ticket. so let's say Jason deposits 250$, then he gets tickets 1-25, Bob deposits 100$ then he gets tickets 26-36. etc etc
then in the end it'd pick a random number and it'd pick a player that has that number in his range.
this is the current code:
public class Main {
private static int totalPlayers = 0;
private static int totalMoney = 0;
private static int playerDeposit = 0;
private static ArrayList<String> players = new ArrayList<String>();
private static ArrayList<Integer> botsMoney = new ArrayList<Integer>();
private static Timer timer;
private static Random random = new Random();
private static int playerMoney = 500;
private static String playerName;
private static double playerChance;
public static void main(String[] args) {
onUserEnter();
}
public static boolean onUserEnter(){
Scanner scan = new Scanner(System.in);
System.out.println("Enter your name:");
playerName = scan.nextLine();
System.out.println("Money: 500$");
System.out.println("How much would you like to deposit?");
try{
playerDeposit = scan.nextInt();
}catch(NumberFormatException ex) {
System.out.println(playerDeposit + " is not a proper number!");
playerDeposit = 0;
return false;
}
if(playerDeposit <= 0) {
System.out.println("You can't deposit " + playerDeposit + "$");
return false;
}else if(playerDeposit > playerMoney){
System.out.println("You can't deposit more money than what you have!");
return false;
}
playerMoney -= playerDeposit;
System.out.println("You now have: " + playerMoney + "$");
players.add(playerName);
totalPlayers = players.size();
totalMoney += playerDeposit;
System.out.println(totalPlayers + " Players: " + players);
System.out.println("Total Pot: " + totalMoney + ", Your chances: 100%");
TimerTask tasknew = new TimerTask() {
#Override
public void run() {
}
};
timer = new Timer();
timer.schedule(tasknew, 20000);
botJoin();
return true;
}
public static boolean botJoin(){
botsMoney.add(5355);
int x = 0;
while(true){
int willBotJoin = random.nextInt(1000000000);
if(willBotJoin <= 2){
if(players.size() >= 10) {
break;
}
int moneyBotDeposits = random.nextInt(1100);
botsMoney.add(moneyBotDeposits);
double tickets = (double) moneyBotDeposits / 10;
x++;
players.add("Bob #" + x);
totalPlayers = players.size();
totalMoney += moneyBotDeposits;
System.out.println(totalPlayers + " Players: " + players);
playerChance = ((double) playerDeposit / totalMoney) * 100;
System.out.println("Total Pot: " + totalMoney + "$, Your chances: " + playerChance + "%");
}
}
botsPrecentage();
return true;
}
public static boolean botsPrecentage(){
for (int i = 1; i <= 9; i++){
double botPrecent = ((double) botsMoney.get(i) / totalMoney) * 100;
String botName = players.get(i);
System.out.println(botName + " has " + botPrecent + "%");
}
System.out.println(playerName + " has " + playerChance + "%");
return true;
}
Also I tried setting up a timer which I don't understand how, I've looked up Timer online but I just can't understand the explanations.
I'm making a simple multiplication game, but when the user enters a correct answer, it still runs the else statement. I know it's a simple solution but I just can't figure it out. Can someone give me a hand?
public static void partB() {
System.out.println("Exercise 1B");
int count = 0;
String active = "true";
int correct = 0;
while (active == "true") {
Random r = new Random();
int Low = 10; //inclusive
int High = 21; //not inclusive
int Result = r.nextInt(High - Low) + Low;
Random r2 = new Random();
int Result2 = r2.nextInt(High - Low) + Low;
int Total = Result * Result2;
Scanner input = new Scanner(System.in);
System.out.println(Result + "*" + Result2 + "=?");
String guess = new String(input.nextLine());
String tostrng = String.valueOf(Total);
if (guess.equals (tostrng)) {
correct += 1;
count+=1;
System.out.println("Correct answer. Score: " + correct + "(" + count + ")");
}
if (guess.equals("q")) {
System.out.println("Good Bye!");
active = "false";
//return;
}
else {
count +=1;
System.out.println("Incorrect answer. Score:" + correct + "(" + count + ")");
}
}
}
Your else with wrong if
public static void partB() {
System.out.println("Exercise 1B");
int count = 0;
String active = "true";
int correct = 0;
while (active == "true") {
Random r = new Random();
int Low = 10; //inclusive
int High = 21; //not inclusive
int Result = r.nextInt(High - Low) + Low;
Random r2 = new Random();
int Result2 = r2.nextInt(High - Low) + Low;
int Total = Result * Result2;
Scanner input = new Scanner(System.in);
System.out.println(Result + "*" + Result2 + "=?");
String guess = new String(input.nextLine());
String tostrng = String.valueOf(Total);
if (guess.equals (tostrng)) {
correct += 1;
count+=1;
System.out.println("Correct answer. Score: " + correct + "(" + count + ")");
}
else {
count +=1;
System.out.println("Incorrect answer. Score:" + correct + "(" + count + ")");
}
if (guess.equals("q")) {
System.out.println("Good Bye!");
active = "false";
//return;
}
}
}
Make type of active variable as boolean i.e. boolean active=true;
and for comparing string values always use .equals() method.
Fixed it like this!
thanks for the help though guys.
if (guess.equals("q")) {
System.out.println("Good Bye!");
return;
}
if (guess.equals(tostrng)) {
correct += 1;
count += 1;
System.out.println("Correct answer. Score: " + correct + "(" + count + ")");
} else {
count += 1;
System.out.println("Incorrect answer. Score:" + correct + "(" + count + ")");
}
I am trying to make this file loop, but I keep getting an error on at the last if statement (Marked by an arrow).
The program is supposed to read in a file with the first line being the name of the customer.
On the second line, the first number is the number of trees to be removed(150 per tree), the second number is tree trimming to be done(50 an hour).
The third line are all the stumps to be removed and their diameter(one number is one stump and also it's diameter).
This is the file that is supposed to be read in (http://pastebin.com/gXkujcaM).
public class Prog_5 {
public static void main(String[] args) throws FileNotFoundException {
String name = "Joe";
double trees = 0;
double treeTrimming = 0;
double stumpInches = 0;
double stumpTotal = 0;
double total = 0;
double totalRev = 0;
Scanner in = new Scanner(System.in);
System.out.print("Input file: ");
String inputFile = in.nextLine();
System.out.print("Output file: ");
String outputFile = in.nextLine();
in.close();
File input = new File(inputFile);
in = new Scanner(input);
PrintWriter output = new PrintWriter(outputFile);
while(in.hasNext()){
name = in.nextLine();
output.println("Customer: " + name);
System.out.println("Customer: " + name);
trees = in.nextDouble();
trees *= 150;
output.println("Tree Removal: $" + trees);
System.out.println("Tree Removal: $" + trees);
treeTrimming = in.nextDouble();
treeTrimming *= 50;
output.println("Tree Trimming: $" + treeTrimming);
System.out.println("Tree Trimming: $" + treeTrimming);
while (in.hasNextDouble()) {
stumpInches = in.nextDouble();
if (stumpInches != -1) {
stumpTotal = stumpTotal + 30;
if (stumpInches > 12) {
stumpInches -= 12;
stumpInches *= 2;
}
stumpTotal += stumpInches;
}
}
output.println("Stump Removal: $" + stumpTotal);
System.out.println("Stump Removal: $" + stumpTotal);
total = (trees + treeTrimming + stumpTotal);
output.println("Total: $" + total);
System.out.println("Total: $" + total);
totalRev += total;
stumpTotal = 0;
trees = 0;
treeTrimming = 0;
if(in.hasNext());
in.next();
}
output.close();
}
}
it is your if loop is problem . you should write it like below . but you terminated it with ;
if(in.hasNext()){
in.next();
}
so it will keep throwing nosuchelement exception every time if it reaches EOF
There is no need of outer while loop, if condition at the end. Inner while loops takes care of what your program is intended to do. PFB corrected code:
import java.io.*;
import java.util.Scanner;
public class Prog_5 {
public static void main(String[] args) throws FileNotFoundException {
String name = "Joe";
double trees = 0;
double treeTrimming = 0;
double stumpInches = 0;
double stumpTotal = 0;
double total = 0;
double totalRev = 0;
Scanner in = new Scanner(System.in);
System.out.print("Input file: ");
String inputFile = in.nextLine();
System.out.print("Output file: ");
String outputFile = in.nextLine();
in.close();
File input = new File(inputFile);
in = new Scanner(input);
PrintWriter output = new PrintWriter(outputFile);
// while (in.hasNext()) {
name = in.nextLine();
output.println("Customer: " + name);
System.out.println("Customer: " + name);
trees = in.nextDouble();
trees *= 150;
output.println("Tree Removal: $" + trees);
System.out.println("Tree Removal: $" + trees);
treeTrimming = in.nextDouble();
treeTrimming *= 50;
output.println("Tree Trimming: $" + treeTrimming);
System.out.println("Tree Trimming: $" + treeTrimming);
while (in.hasNextDouble()) {
stumpInches = in.nextDouble();
if (stumpInches != -1) {
stumpTotal = stumpTotal + 30;
if (stumpInches > 12) {
stumpInches -= 12;
stumpInches *= 2;
}
stumpTotal += stumpInches;
}
}
output.println("Stump Removal: $" + stumpTotal);
System.out.println("Stump Removal: $" + stumpTotal);
total = (trees + treeTrimming + stumpTotal);
output.println("Total: $" + total);
System.out.println("Total: $" + total);
totalRev += total;
stumpTotal = 0;
trees = 0;
treeTrimming = 0;
// if (in.hasNext())
// ;
// in.next();
// }
in.close();
output.close();
}
}
I am trying to store the data that's created in the while loop in the list variable so that I can use it later with the JOptionPane.INFORMATION_MESSAGE.
I tried using an array to store the data but it would not work.
Please let me know what I am missing.
package loops;
import java.text.DecimalFormat;
import java.util.ArrayList;
import javax.swing.JOptionPane;
public class Statistics
{
public static void main(String[] args)
{
int observations = 1,
num = 0,
sum = 0,
max = Integer.MIN_VALUE,
min = Integer.MAX_VALUE;
double mean = 0.0;
String userEntry = "",
result,
list = " ",
seperator = "\n***********\nYou entered the following observations: ";
DecimalFormat twoDigits = new DecimalFormat ("0.00");
userEntry = JOptionPane.showInputDialog ("Enter observation # " + observations +
" (or \"end\" to quit) ");
//num = Integer.parseInt(userEntry);
String[] list2 = new String[num];
while(!userEntry.equalsIgnoreCase("end"))
{
num = Integer.parseInt(userEntry);
observations ++;
userEntry = JOptionPane.showInputDialog ("Enter observation #" + observations +
" (or \"end\" to quit) ");
//num = Integer.parseInt(userEntry);
//num = Integer.parseInt(userEntry);
//list = "\n" + num;
//list = list.toString();
sum += num;
if(num > max)
{
max = num;
}
if(num < min)
{
min = num;
}
//Integer.toString(num);
//ArrayList<String> list = new ArrayList<String>();
//list.add(num);
//list = "\n" + Integer.toString(num);
//String[] list2 = new String[num];
//for(String list1 : list2)
//{
list = "\n" + num;
//}
}
observations --;
mean = sum / (double)observations;
//twoDigits.format(mean);
if(observations == 0)
{
result = "no observations selected";
}
else
{
result = "You entered " + observations +
(observations == 1 ? " observation" : " observations");
result = result + "\nThe minimum is " + min;
result = result + "\nThe maximum is " + max;
result = result + "\nThe sum is " + sum;
result = result + "\nThe mean is " + twoDigits.format(mean);
result = result + seperator;
result = result + list;
}
JOptionPane.showMessageDialog(null, result,
"Results", JOptionPane.INFORMATION_MESSAGE);
//observations --;
System.exit(0);
}
}
Try using ArrayList instead of []. Example based on your code is shown below.
package loops;
import java.text.DecimalFormat;
import java.util.ArrayList;
import javax.swing.JOptionPane;
public class Statistics {
public static void main(String[] args) {
// method local variables
int observations = 0;
int num = 0;
int sum = 0;
int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;
double mean = 0.0;
String userEntry = "";
String result;
String list = " ";
String seperator = "\n***********\nYou entered the following observations: ";
DecimalFormat twoDigits = new DecimalFormat("0.00");
ArrayList<Integer> intList = new ArrayList<Integer>();
// get user entry from JOptionPane
userEntry = JOptionPane.showInputDialog("Enter observation # " + observations + " (or \"end\" to quit) ");
// iterate until user enters end
while (userEntry.equalsIgnoreCase("end") == false) {
observations++;
num = Integer.parseInt(userEntry);
userEntry = JOptionPane.showInputDialog("Enter observation #" + observations + " (or \"end\" to quit) ");
sum = sum + num;
// change number to max if > max
if (num > max) {
max = num;
}
// change number to min if < min
if (num < min) {
min = num;
}
// add the number to the string and to the list
list = list + "\n" + num;
intList.add(num);
}
echoList(intList);
mean = sum / (double) observations;
if (observations == 0) {
result = "no observations selected";
}
else {
result = "You entered " + observations + (observations == 1 ? " observation" : " observations");
result = result + "\nThe minimum is " + min;
result = result + "\nThe maximum is " + max;
result = result + "\nThe sum is " + sum;
result = result + "\nThe mean is " + twoDigits.format(mean);
result = result + seperator;
result = result + list;
}
// show results and exit
JOptionPane.showMessageDialog(null, result, "Results", JOptionPane.INFORMATION_MESSAGE);
System.exit(0);
}
private static void echoList(ArrayList<Integer> intList) {
System.out.println("------------------------------");
System.out.println("Got " + intList.size() + " integers.");
for(Integer i : intList) {
System.out.println("\t" + i);
}
System.out.println("------------------------------");
}
}
JOptionPane.showInputDialog() will return the string the user has entered if the user hits ok, and returns null otherwise.
Therefore, if the user hits cancel, your while condition (userEntry.equalsIgnoreCase("end") == false) will throw a NullPointerException.
Therefore, it's safer to change the while condition to (userEntry != null).
Now, if you want to store all the user entries, use a list of integers, say List<Integer> userEntries
List<Integer> userEntries = new ArrayList<>();
while (userEntry.equalsIgnoreCase("end") == false)
{
observations++;
num = Integer.parseInt(userEntry);
userEntries.add(num);
...
}
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));
}
}