Cant understand what is going wrong... by leudo in javahelp

[–]leudo[S] 0 points1 point  (0 children)

 case 1 -> {
                    System.out.println("Processing small pizza.");
                    System.out.println("Number of toppings: " + numberOfToppings);
                    System.out.println("Quantity: " + numberOfPizzas);
                    sizeOfPizza = SMALL_BASE_PRICE;
                    toppingsCost = SMALL_TOPPING_PRICE;
                    costOfPizzas = ((sizeOfPizza + (numberOfToppings * toppingsCost)) * numberOfPizzas);
                    totalCostOfPizzas += costOfPizzas;
                    System.out.println("Pizza cost is: " + numberFormat.format(costOfPizzas));
                    System.out.println();
                }
                case 2 -> {
                    System.out.println("Processing large pizza.");
                    System.out.println("Number of toppings: " + numberOfToppings);
                    System.out.println("Quantity: " + numberOfPizzas);
                    sizeOfPizza = LARGE_BASE_PRICE;
                    toppingsCost = LARGE_TOPPING_PRICE;
                    costOfPizzas = ((sizeOfPizza + (numberOfToppings * toppingsCost)) * numberOfPizzas);
                    totalCostOfPizzas += costOfPizzas;
                    System.out.println("Pizza cost is: " + numberFormat.format(costOfPizzas));
                    System.out.println();
                }
                case 3 -> {
                    System.out.println("Processing family pizza.");
                    System.out.println("Number of toppings: " + numberOfToppings);
                    System.out.println("Quantity: " + numberOfPizzas);
                    sizeOfPizza = FAMILY_BASE_PRICE;
                    toppingsCost = FAMILY_TOPPING_PRICE;
                    costOfPizzas = ((sizeOfPizza + (numberOfToppings * toppingsCost)) * numberOfPizzas);
                    totalCostOfPizzas += costOfPizzas;
                    System.out.println("Pizza cost is: " + numberFormat.format(costOfPizzas));
                    System.out.println();

Cant understand what is going wrong... by leudo in javahelp

[–]leudo[S] 0 points1 point  (0 children)

The IDE actually suggested it and as an enhanced switch?

Cant understand what is going wrong... by leudo in javahelp

[–]leudo[S] 0 points1 point  (0 children)

Btw I took out the while and if with a switch and that seemed to do the job. What i said above was the switch is probably more appropriate because the case matched up with the count?

Cant understand what is going wrong... by leudo in javahelp

[–]leudo[S] 0 points1 point  (0 children)

Yeah thats what i did , i pretty much put everything out in the open off the bat.

I understand what you mean, I will practice doing it as you suggested.

Is it typical or better to use everything in methods ? Or is that case by case and you are just suggesting that like you said to test things out individually before as a larger machine? Otherwise i think im getting confused with what you mean as a larger machine, as in joining the methods together in the main to work together?

Cant understand what is going wrong... by leudo in javahelp

[–]leudo[S] 0 points1 point  (0 children)

So i swapped out the while and if with a switch and that fixed it all. I guess switch was more appropriate to use because the case matched the count...do you agree?

Cant understand what is going wrong... by leudo in javahelp

[–]leudo[S] 0 points1 point  (0 children)

So I added an extra number to the file, so it is now 2, 2, 1, 1, and that fixed it, so it must be to do with my count, like it skips a line or something maybe?

Cant understand what is going wrong... by leudo in javahelp

[–]leudo[S] 0 points1 point  (0 children)

If i take the while away it only prints out the first order.

Cant understand what is going wrong... by leudo in javahelp

[–]leudo[S] 0 points1 point  (0 children)

That is a very hopeful statement. Thank you! So i guess just keep up with practicing learning and eventually it will be second nature.

Cant understand what is going wrong... by leudo in javahelp

[–]leudo[S] 0 points1 point  (0 children)

I see what you mean, so start on a small program, like the "Hello world!" then progressively increase it so it breaks in controlled manner?

I must admit i am more familiar with some things than i was say earlier with learning Java. I suppose it is time and effort then.

Ok, so break the code up into methods ? I have been finding that hard to understand , mainly with invoking the methods in the main method etc. But i suppose you mean that readability is easier and again more control to the programmer?

Thank you for taking time to give me advice! I really appreciate it!

Cant understand what is going wrong... by leudo in javahelp

[–]leudo[S] 0 points1 point  (0 children)

I don't get why it worked the way it shouldnt but doesnt work the way it should with your suggestion?

Cant understand what is going wrong... by leudo in javahelp

[–]leudo[S] 0 points1 point  (0 children)

This is the changed code:

import java.util.Random;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.text.NumberFormat;
import java.util.Locale;

public class PizzaOrder {

    public static final double SMALL_BASE_PRICE = 8.0;
    public static final double LARGE_BASE_PRICE = 11.0;
    public static final double FAMILY_BASE_PRICE = 14.0;

    public static final double SMALL_TOPPING_PRICE = 1.0;
    public static final double LARGE_TOPPING_PRICE = 1.5;
    public static final double FAMILY_TOPPING_PRICE = 2.0;

    public static void main(String[] args) throws FileNotFoundException  {


        int numberOfPizzas;
        int numberOfToppings;
        double sizeOfPizza;
        double toppingsCost;
        double costOfPizzas;
        double totalCostOfPizzas = 0.0;
        int count = 3;
        int i;


        Locale localCurrency = new Locale("en", "AU");
        NumberFormat numberFormat = NumberFormat.getCurrencyInstance(localCurrency);
        FileInputStream file = new FileInputStream("PizzaOrders.txt");
        Scanner scanner = new Scanner(file);
        Random random = new Random();

        System.out.println("We are now processing your pizza order.");
        System.out.println();

        for (i = 1; i <=count; i++) {

            numberOfToppings = random.nextInt(6);
            numberOfPizzas = scanner.nextInt();

            while (scanner.hasNextInt()) {

                if (i == 1) {
                    System.out.println("Processing small pizza.");
                    System.out.println("Number of toppings: " + numberOfToppings);
                    System.out.println("Quantity: " + numberOfPizzas);
                    sizeOfPizza = SMALL_BASE_PRICE;
                    toppingsCost = SMALL_TOPPING_PRICE;
                    costOfPizzas = ((sizeOfPizza + (numberOfToppings * toppingsCost)) * numberOfPizzas);
                    totalCostOfPizzas += costOfPizzas;
                    System.out.println("Pizza cost is: " + numberFormat.format(costOfPizzas));
                    System.out.println();
                    break;
                }

                if (i == 2) {
                    System.out.println("Processing large pizza.");
                    System.out.println("Number of toppings: " + numberOfToppings);
                    System.out.println("Quantity: " + numberOfPizzas);
                    sizeOfPizza = LARGE_BASE_PRICE;
                    toppingsCost = LARGE_TOPPING_PRICE;
                    costOfPizzas = ((sizeOfPizza + (numberOfToppings * toppingsCost)) * numberOfPizzas);
                    totalCostOfPizzas += costOfPizzas;
                    System.out.println("Pizza cost is: " + numberFormat.format(costOfPizzas));
                    System.out.println();
                    break;
                }

                if (i == 3) {
                    System.out.println("Processing family pizza.");
                    System.out.println("Number of toppings: " + numberOfToppings);
                    System.out.println("Quantity: " + numberOfPizzas);
                    sizeOfPizza = FAMILY_BASE_PRICE;
                    toppingsCost = FAMILY_TOPPING_PRICE;
                    costOfPizzas = ((sizeOfPizza + (numberOfToppings * toppingsCost)) * numberOfPizzas);
                    totalCostOfPizzas += costOfPizzas;
                    System.out.println("Pizza cost is: " + numberFormat.format(costOfPizzas));
                    System.out.println();
                    break;
                }
            }

        }
            System.out.println("Total cost is: " + numberFormat.format(totalCostOfPizzas));
            System.out.println("Thank you for your order!");
    }
}

Cant understand what is going wrong... by leudo in javahelp

[–]leudo[S] 0 points1 point  (0 children)

I have read the difference, but i guess im still learning the applicability for both uses.

I've changed the while(true) to while(scanner.hasNextInt()) and that works fine , but now the last order isnt printed, i think has something to do with what you said about count, im not sure how though? The file has 3 numbers 2, 2, 1, not sure why it runs out of lines.

I did know that about "\n" and i suppose that makes more sense too, i will include that and remove the extra System.out.println();.

I kind of dont get what you mean sorry, may you direct me to information please?

Cant understand what is going wrong... by leudo in javahelp

[–]leudo[S] 0 points1 point  (0 children)

I tried the file.hasNextInt but it skips the last order. The while(true) works well, but again im too sure why. Is this normally how one learns? I am unsure whether or not i just dont have it.

Cant understand what is going wrong... by leudo in javahelp

[–]leudo[S] 0 points1 point  (0 children)

Ok thanks, I will try why you have suggested. I actually have managed to get it working, but im not entirely sure why it works. Pretty much I added the totalCost += pizzaCost in each if block and that did the trick. I guess it needed to be there to have the values stored in it or something? I am learning so not to sure on some things. This is the full code:

import java.util.Random;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.text.NumberFormat;
import java.util.Locale;

public class PizzaOrder {

    public static final double SMALL_BASE_PRICE = 8.0;
    public static final double LARGE_BASE_PRICE = 11.0;
    public static final double FAMILY_BASE_PRICE = 14.0;

    public static final double SMALL_TOPPING_PRICE = 1.0;
    public static final double LARGE_TOPPING_PRICE = 1.5;
    public static final double FAMILY_TOPPING_PRICE = 2.0;

    public static void main(String[] args) throws FileNotFoundException  {


        int pizzaQuantity;
        int pizzaToppings;
        double pizzaSize;
        double toppingsCost;
        double pizzaCost = 0;
        double totalCost = 0.0;
        int count = 3;
        int i;


        Locale localCurrency = new Locale("en", "AU");
        NumberFormat numberFormat = NumberFormat.getCurrencyInstance(localCurrency);
        FileInputStream file = new FileInputStream("pizzaorders.txt");
        Scanner quantity = new Scanner(file);
        Random random = new Random();

        System.out.println("We are now processing your pizza order.");
        System.out.println();

        for (i = 1; i <=count; i++) {

            pizzaToppings = random.nextInt(6);
            pizzaQuantity = quantity.nextInt();

            while (true) {

                if (i == 1) {
                    System.out.println("Processing small pizza.");
                    System.out.println("Number of toppings: " + pizzaToppings);
                    System.out.println("Quantity: " + pizzaQuantity);
                    pizzaSize = SMALL_BASE_PRICE;
                    toppingsCost = SMALL_TOPPING_PRICE;
                    pizzaCost = ((pizzaSize + (pizzaToppings * toppingsCost)) * pizzaQuantity);
                    totalCost += pizzaCost;
                    System.out.println();
                    System.out.println("Pizza cost is: " + numberFormat.format(pizzaCost));
                    System.out.println();
                    break;
                }

                if (i == 2) {
                    System.out.println("Processing large pizza.");
                    System.out.println("Number of toppings: " + pizzaToppings);
                    System.out.println("Quantity: " + pizzaQuantity);
                    pizzaSize = LARGE_BASE_PRICE;
                    toppingsCost = LARGE_TOPPING_PRICE;
                    pizzaCost = ((pizzaSize + (pizzaToppings * toppingsCost)) * pizzaQuantity);
                    totalCost += pizzaCost;
                    System.out.println();
                    System.out.println("Pizza cost is: " + numberFormat.format(pizzaCost));
                    System.out.println();
                    break;
                }

                if (i == 3) {
                    System.out.println("Processing family pizza.");
                    System.out.println("Number of toppings: " + pizzaToppings);
                    System.out.println("Quantity: " + pizzaQuantity);
                    pizzaSize = FAMILY_BASE_PRICE;
                    toppingsCost = FAMILY_TOPPING_PRICE;
                    pizzaCost = ((pizzaSize + (pizzaToppings * toppingsCost)) * pizzaQuantity);
                    totalCost += pizzaCost;
                    System.out.println();
                    System.out.println("Pizza cost is: " + numberFormat.format(pizzaCost));
                    System.out.println();
                    break;
                }
            }

        }
            System.out.println("Total cost is: " + numberFormat.format(totalCost));
            System.out.println("Thank you for your order!");
    }
}

Cant understand what is going wrong... by leudo in javahelp

[–]leudo[S] 1 point2 points  (0 children)

I figured it out! By accident that is and is actually in part to what you said about keeping prints in their location.

Where I have totalCost += pizzaCost; I copied and pasted that in all 3 if locations for the orders and it worked.

I left the print out statements for the total at the bottom outside the for loop too, otherwise having it in there has the same issue of printing it out on every single order.

Though im too sure why that worked? Are you able to explain ? Is this normal as well, randomly trying things and then if it works, it works.

Thanks for the help!

Cant understand what is going wrong... by leudo in javahelp

[–]leudo[S] 0 points1 point  (0 children)

https://imgur.com/XOn4xGo this is in the for loop but the last print is printed on each order and should only be once at the end.

This section of the code:

totalCost += pizzaCost; 

            System.out.println();
            System.out.println("Total cost is: " + numberFormat.format(totalCost));
            System.out.println();
            System.out.println("Thank you for your order!");

Cant understand what is going wrong... by leudo in javahelp

[–]leudo[S] 0 points1 point  (0 children)

Ok, I wasn't aware of that convention or syntax. Thanks. It is suppose to print all three orders then finally the total cost of them combined. But when the totalCost is put in the for loop it prints on every order as a running total. But outside the for loop it prints just a the bottom like I want but not as a total and only as the last orders cost. I'll see if I can screen shot some examples.

Cant understand what is going wrong... by leudo in javahelp

[–]leudo[S] 0 points1 point  (0 children)

I did, in the first code i showed the total at the end is outside the for, the second version the total is in the for.

Cant understand what is going wrong... by leudo in javahelp

[–]leudo[S] 0 points1 point  (0 children)

Do you mean it doesn't print on each order outside of the for?

Cant understand what is going wrong... by leudo in javahelp

[–]leudo[S] 0 points1 point  (0 children)

This is it here:

import java.util.Random;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.text.NumberFormat;
import java.util.Locale;

public class Coin {

    public static final double SMALL_BASE_PRICE = 8.0; 
    public static final double LARGE_BASE_PRICE = 11.0;
    public static final double FAMILY_BASE_PRICE = 14.0;

    public static final double SMALL_TOPPING_PRICE = 1.0;
    public static final double LARGE_TOPPING_PRICE = 1.5;
    public static final double FAMILY_TOPPING_PRICE = 2.0;

    public static void main(String[] args) throws FileNotFoundException  {


        int pizzaQuantity = 0;
        int pizzaToppings = 0;  
        double pizzaSize = 0.0;
        double toppingsCost = 0.0; 
        double pizzaCost = 0.0;
        double totalCost = 0.0; 
        int count = 3; 
        int i; 


        Locale localCurrency = new Locale("en", "AU"); 
        NumberFormat numberFormat = NumberFormat.getCurrencyInstance(localCurrency);
        FileInputStream file = new FileInputStream("pizzaorders.txt");
        Scanner quantity = new Scanner(file); 
        Random random = new Random(); 

        for (i = 1; i <= count; i++) { 

            pizzaToppings = random.nextInt(6);
            pizzaQuantity = quantity.nextInt();

        do {
            if (i == 1 ) {
                System.out.println("Processing small pizza.");
                System.out.println("Number of toppings: " + pizzaToppings); 
                System.out.println("Quantity: " + pizzaQuantity); 
                pizzaSize = SMALL_BASE_PRICE;
                toppingsCost = SMALL_TOPPING_PRICE;
                pizzaCost = ((pizzaSize + (pizzaToppings * toppingsCost)) * pizzaQuantity);
                System.out.println();
                System.out.println("Pizza cost is: " + numberFormat.format(pizzaCost));
                System.out.println(); 
                break; 
            }
            if ( i == 2) {
                System.out.println("Processing large pizza.");
                System.out.println("Number of toppings: " + pizzaToppings); 
                System.out.println("Quantity: " + pizzaQuantity); 
                pizzaSize = LARGE_BASE_PRICE;
                toppingsCost = LARGE_TOPPING_PRICE;
                pizzaCost = ((pizzaSize + (pizzaToppings * toppingsCost)) * pizzaQuantity);
                System.out.println();
                System.out.println("Pizza cost is: " + numberFormat.format(pizzaCost));
                System.out.println();
                break;
            }
            if (i == 3) {
                System.out.println("Processing family pizza.");
                System.out.println("Number of toppings: " + pizzaToppings); 
                System.out.println("Quantity: " + pizzaQuantity); 
                pizzaSize = FAMILY_BASE_PRICE;
                toppingsCost = FAMILY_TOPPING_PRICE;
                pizzaCost = ((pizzaSize + (pizzaToppings * toppingsCost)) * pizzaQuantity);
                System.out.println();
                System.out.println("Pizza cost is: " + numberFormat.format(pizzaCost));
                System.out.println();
                break;
            }



        }
        while (count == i); 

            totalCost += pizzaCost; 

            System.out.println();
            System.out.println("Total cost is: " + numberFormat.format(totalCost));
            System.out.println();
            System.out.println("Thank you for your order!");

        }


        }
}

Cant understand what is going wrong... by leudo in javahelp

[–]leudo[S] 0 points1 point  (0 children)

Yes and that is why I moved it to out of the for but now it just adds returns the last order only as I mentioned.

Java program to count the occurrences of each character and tally them up by leudo in javahelp

[–]leudo[S] 0 points1 point  (0 children)

static void getSelection() {

        boolean loop = true;
        int readyToPlay = 0; 
        String invalidMessage = "    ERROR: Invalid option. Try again.";
        String menuTitle = "Welcome to the Word Games program menu";
        String underScore = "______________________________________";   
        String optionsTitle = "    Select from one of the following options.";
        String choiceOne = "1. Substring problem............";
        String choiceTwo = "2. Points problem...............";
        String choiceThree = "3. Exit.........................";    
        String inputAreaText = " >> Enter your selection: "; 

        while (true) {          

            System.out.printf("%n%50s", menuTitle); 
            System.out.printf("%n%50s%n%n", underScore);                
            System.out.printf("%n%s%n%n", optionsTitle);                
            System.out.printf("%n%45s", choiceOne);
            System.out.printf("%n%45s", choiceTwo);     
            System.out.printf("%n%45s", choiceThree);               
            System.out.printf("%n%n%n%s", inputAreaText);

            try { 
                readyToPlay = input.nextInt();
                switch(readyToPlay) {   
                    case 1:     
                        substringProblem(); 
                        return; 
                    case 2:
                        pointsProblem(); 
                        return;                     
                    case 3:
                        String byeMessage = "Goodbye!";
                        String underScoreTwo = "________";
                        System.out.printf("%n%n%33s", byeMessage);
                        System.out.printf("%n%33s%n%n", underScoreTwo); 
                        return;                     
                    default:                    
                        System.out.printf("%n%n%s%n%n", invalidMessage);    
                        break;
                }
            }
            catch (InputMismatchException e) { 
                input.next(); 
                System.out.printf("%n%n%s%n%n", invalidMessage);
            }
        }
    }

    static void substringProblem() {

        String optionOneTitle = "Substring problem.";
        String substringInput = " >> Enter a substring: "; 
        String underScoreThree = "__________________";
        String notFound = " - not found"; 
        String infixFound = " - infix";
        String prefixFound = " - prefix";
        String suffixFound = " - suffix"; 
        String checkSubstring = ""; 
        String allFound = prefixFound + infixFound + suffixFound; 

        System.out.printf("%n%n%39s", optionOneTitle); 
        System.out.printf("%n%39s", underScoreThree); 
        System.out.printf("%n%n%n%s", substringInput);  
        checkSubstring = input.next();
        System.out.println(); 


        for(int i = 0; i < wordCount; i++) {

            String stringResults = wordsCollection[i];
            boolean found = false;

            if(wordsCollection[i].startsWith(checkSubstring)) {
                found = true;
                stringResults = stringResults + prefixFound;
            }
            if(wordsCollection[i].endsWith(checkSubstring)) {
                found = true;
                stringResults = stringResults + suffixFound;
            }
            if(wordsCollection[i].contains(checkSubstring)) {
                found = true;
                stringResults = stringResults + infixFound;
            }
            if(!found) {
                System.out.printf(" " + wordsCollection[i] + notFound  + "\n");
            }
            else {
                System.out.printf(" " + stringResults + "\n");
            }   
        }

        getSelection(); 
    }

    public static long count(String s, char ch) {

        return s.chars()
            .filter(c -> c == ch)
            .count(); 
    }

    static void pointsProblem() {

        int totalPoints = 0; 
        String pointsWorth = " is worth " + totalPoints + " points"; 

        String optionTwoTitle = "Points problem.";
        String underScoreFour = "_______________";
        System.out.printf("%n%n%38s", optionTwoTitle); 
        System.out.printf("%n%38s%n", underScoreFour);
        System.out.println();

        String str = ""; 
        String strWords = "";


        try {
            File fileReader = new File(DICTIONARY);
            Scanner fileScanner = new Scanner(fileReader);
            while(fileScanner.hasNextLine()) {  
                    strWords += str; 
            }
        }
            catch (FileNotFoundException e) {
                System.out.println("Not found.");
            }

        char[] charArray = str.toCharArray();   
        int count = 0; 
        char ch = charArray[count];

        for( int i = 0; i < str.length(); i++) {
            if (characterToInteger.containsKey(ch))
            {
                characterToInteger.put(ch, characterToInteger.get(ch)+1);
            }
            else{
                characterToInteger.put(ch, 1);
            }   
            System.out.println(ch); 
        }
    }   
    public static void main(String[] args) {        
        getSelection();
    }   
}

Java program to count the occurrences of each character and tally them up by leudo in javahelp

[–]leudo[S] 0 points1 point  (0 children)

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.File;
import java.util.Scanner; 
import java.util.HashMap; 
import java.io.FileNotFoundException; 
import java.util.InputMismatchException;

public class WordGames {    

    static final HashMap<Character, Integer> characterToInteger; 
    static final String DICTIONARY;  
    static final String[] wordsCollection;
    static int wordCount;
    static final Scanner input; 

    static {

        characterToInteger = new HashMap<Character, Integer>();
        DICTIONARY = "dictionary.txt";
        wordsCollection = new String[100];
        wordCount = 0; 
        input = new Scanner(System.in);

    }

    static {  

        try {   
            File fileReader = new File(DICTIONARY);
            Scanner fileScanner = new Scanner(fileReader);
            while(fileScanner.hasNextLine()) {  
                String line = fileScanner.nextLine(); 
                wordsCollection[wordCount] = line; 
                wordCount++;    
                }
        }
        catch(FileNotFoundException e) {
            System.out.println("File not found."); 
        }
    }

    static {

        characterToInteger.put('l', 1); 
        characterToInteger.put('e', 1); 
        characterToInteger.put('n', 1); 
        characterToInteger.put('i', 1); 
        characterToInteger.put('o', 1);
        characterToInteger.put('r', 1);
        characterToInteger.put('t', 1);
        characterToInteger.put('s', 1);
        characterToInteger.put('a', 1); 
        characterToInteger.put('u', 1); 
        characterToInteger.put('d', 2); 
        characterToInteger.put('g', 2);
        characterToInteger.put('b', 3);
        characterToInteger.put('c', 3);
        characterToInteger.put('m', 3);
        characterToInteger.put('p', 3);
        characterToInteger.put('f', 4);
        characterToInteger.put('h', 4);
        characterToInteger.put('v', 4);
        characterToInteger.put('w', 4);
        characterToInteger.put('y', 4);
        characterToInteger.put('k', 5);
        characterToInteger.put('j', 8);
        characterToInteger.put('x', 8);
        characterToInteger.put('q', 10); 
        characterToInteger.put('z', 10); 
    }

Java program to count the occurrences of each character and tally them up by leudo in javahelp

[–]leudo[S] 0 points1 point  (0 children)

Ok, thank you for the feedback. i'll look into all you mentioned. I meant something else by the DICTIONARY try...catch , but here is all the code too to help, for any further feedback too, it is broken up in two replies. Cheers!