Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Reset the value of each variable

  1. #1
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Reset the value of each variable

    ill start with showing my program with 2 different codes and data flow


    1st
     
    package xTestsx;
     
    import java.io.*;
     
    /**
     *
     * @author
     */
     
    //I just have to make a control flow for the number of orders that is less than 20
    public class CoffeBeans_CONT_UF {
     
        private static BufferedReader br = new BufferedReader(new InputStreamReader(
                                           System.in));
     
        public static void main(String[] args) throws IOException {
     
     
     
            int numberOfOrders;
     
            int largeBox = 0,
                mediumBox = 0,
                smallBox = 0,
                excess = 0;
     
            int orderCount = 0,
                firstSmallBoxCount = 0,
                nextSmallBoxCount = 0,
                mediumBoxCount = 0,
                largeBoxCount = 0;
     
            int totalBox = 0;
     
          do {
            do {
     
                do {       
     
                    System.out.print("Order: ");
                    numberOfOrders = Integer.parseInt(br.readLine());
     
                    if (numberOfOrders <= 5) {
     
                        firstSmallBoxCount = numberOfOrders;
                        smallBox++;
     
                        System.out.print("\n");
                        System.out.println("Your Order Is Too Small.");
                    }
                    else if (numberOfOrders < 10) {
     
                        firstSmallBoxCount = 5;
     
                        for (orderCount = numberOfOrders; orderCount >= 5; orderCount = orderCount - 5) {
     
                            smallBox++;
                            excess = orderCount - 5;
                        }
     
                        if ((excess <= 4) && (excess != 0)) {
     
                            nextSmallBoxCount = excess;
                            excess = 0;
                        }
     
                        System.out.print("\n");
                        System.out.println("Your Order Is Too Small.");
                    }          
                }
                while (numberOfOrders < 10 && ReOrder());
     
                if (numberOfOrders >= 10) {
     
                    break;
                }       
            }
            while (ContinueShipment()); //this part
     
            if (numberOfOrders < 20) {
     
                mediumBoxCount = 10;
     
                for (orderCount = numberOfOrders; orderCount >= 10; orderCount = orderCount - 10) {
     
                    mediumBox++;
                    excess = orderCount - 10;               
                }
     
                if (excess >= 5 ) {
     
                    firstSmallBoxCount = 5;
     
                    for (; excess >= 5; excess = excess - 5) {
     
                        smallBox++;
                    }
                }
     
                if ((excess <= 4) && (excess != 0)) {
     
                    smallBox++;
                    nextSmallBoxCount = excess;
                    excess = 0;
                }
            }
            else if (numberOfOrders >= 20) {
     
                largeBoxCount = 20;
     
                for (orderCount = numberOfOrders; orderCount >= 20; orderCount = orderCount - 20) {
     
                    largeBox++ ;
                    excess = orderCount - 20;
                }
     
                if (excess >= 10) {
     
                    mediumBoxCount = 10;
     
                    for (; excess >= 10; excess = excess - 10) {
     
                        mediumBox++;                      
                    }                
                }
     
                if (excess >= 5) {
     
                    firstSmallBoxCount = 5;
     
                    for (; excess >= 5; excess = excess - 5) {
     
                        smallBox++;          
                    }        
                }     
     
                if ((excess <= 4) && (excess != 0)) {
     
                    smallBox++;          
                    nextSmallBoxCount = excess;         
                    excess = 0;      
                }
            }
     
     
            if (largeBox == 0) {
     
                if ((mediumBox == 1) && (smallBox > 1)) {
     
                    System.out.print("\n");
                    System.out.println("Medium Box Contains " + mediumBoxCount + " Orders");
                    System.out.println("The First Small Box Contains " + firstSmallBoxCount + " Orders");
                    System.out.println("And The Remaining Small Box Contains " + nextSmallBoxCount + " Orders");
                }
                else if ((mediumBox == 1) & (smallBox == 1)) {
     
                    if (firstSmallBoxCount == 5) {
     
                        System.out.print("\n");
                        System.out.println("Medium Box Contains " + mediumBoxCount + " Orders");
                        System.out.println("Small Box Contains " + firstSmallBoxCount + " Orders");
                    }
                    else if (nextSmallBoxCount < 5) {
     
                        System.out.print("\n");
                        System.out.println("Medium Box Contains " + mediumBoxCount + " Orders");
                        System.out.println("Small Box Contains " + nextSmallBoxCount + " Orders");
                    }           
                }
                else if ((mediumBox == 1) && (smallBox == 0)) {
     
                    System.out.print("\n");
                    System.out.println("Medium Box Contains " + mediumBoxCount + " Orders");
                }
                else if ((mediumBox == 0) && (smallBox > 1)) {
     
                    System.out.println("\n");
                    System.out.println("The First Small Box Contains " + firstSmallBoxCount + " Orders");
                    System.out.println("And The Remaining Small Box Contains " + nextSmallBoxCount + " Orders");
                }              
            }
     
     
            else if ((mediumBox == 0) && (smallBox == 0)) {
     
                System.out.print("\n");
                System.out.println(largeBox  +  " " + "Large Box");
     
                if (largeBox == 1) {
     
                    System.out.print("\n");
                    System.out.println("Large Box Contains" + " " + largeBoxCount + " " + "Orders");
                }
                else if (largeBox > 1) {
     
                    System.out.print("\n");
                    System.out.println("Each Large Box Contains" + " " + largeBoxCount + " " + "Orders");
                }
            }
            else if (mediumBox <= 0) {
     
                System.out.print("\n");
                System.out.println(smallBox + " " + "Small Box");
                System.out.println(largeBox  +  " " + "Large Box");
     
                if (largeBox == 1) {
     
                    if (firstSmallBoxCount < 5) {
     
                        System.out.print("\n");
                        System.out.println("Large Box Contains" + " " + largeBoxCount + " " + "Orders");
                        System.out.println("And The Small Box Contains" + " " + nextSmallBoxCount + " " + "Order");
                    }
                    else if (firstSmallBoxCount >= 5) {
     
                        System.out.print("\n");
                        System.out.println("Each Large Box Contains" + " " + largeBoxCount + " " + "Orders");
                        System.out.println("The First Small Contains" + " " + firstSmallBoxCount + " " + "Orders");
                        System.out.println("And The Reamaining Small Box Contains" + " " + nextSmallBoxCount + " " + "Orders");
                    }            
                }
                else if (largeBox > 1) {
     
                    if (firstSmallBoxCount >= 5) {
     
                        System.out.print("\n");
                        System.out.println("Each Large Box Has" + " " + largeBoxCount + " " + "Orders");
                        System.out.println("The First Small Box Has" + " " + firstSmallBoxCount + " " + "Orders");
                        System.out.println("And The Reamaining Small Box Has" + " " + nextSmallBoxCount + " " + "Orders");
                    }
                    else if (firstSmallBoxCount < 5) {
     
                        System.out.print("\n");
                        System.out.println("Each Large Box Has " + " " + largeBoxCount + " " + "Orders");
                        System.out.println("And The Small Box Has" + " " + nextSmallBoxCount + " " + "Order");
                    }
                }
            }
            else if (smallBox <= 0) {
     
                System.out.print("\n");
                System.out.println(mediumBox + " " + "Medium Box");
                System.out.println(largeBox  +  " " + "Large Box");
     
                if (mediumBox == 1) {
     
                    if (largeBox == 1) {
     
                        System.out.print("\n");
                        System.out.println("One Large Box Contains " + " " + largeBoxCount + " " + "Orders");
                        System.out.println("One Medium Box Contains" + " " + mediumBoxCount + " " + "Orders");
                    }
                    else if (largeBox > 1) {
     
                        System.out.print("\n");
                        System.out.println("Each Large Box Contains" + " " + largeBoxCount + " " + "Orders");
                        System.out.println("One Medium Box Contains" + " " + mediumBoxCount + " " + "Orders");
                    }
                }                 
            }
            else if ((largeBox >= 1) && (mediumBox >= 1) && (smallBox >= 1)) {
     
                System.out.print("\n");
                System.out.println(smallBox + " " + "Small Box");
                System.out.println(mediumBox + " " + "Medium Box");
                System.out.println(largeBox  +  " " + "Large Box");
     
                if (largeBox == 1) {
     
                    if (mediumBox == 1) {
     
                        if (smallBox > 1) {
     
                            if (firstSmallBoxCount >= 5) {
     
                                System.out.print("\n");
                                System.out.println("One Large Box Contains" + " " + largeBoxCount + " " + "Orders");
                                System.out.println("One Medium Box Contains" + " " + mediumBoxCount + " " + "Orders");
                                System.out.println("The First Small Box Contains" + " " + firstSmallBoxCount + " " + "Orders");
                                System.out.println("The Remaining Small Box Contains" + " " + nextSmallBoxCount + " " + "Orders");
                            }         
                        }
                        else if (smallBox == 1) {
     
                            System.out.print("\n");
                            System.out.println("One Large Box Contains" + " " + largeBoxCount + " " + "Orders");
                            System.out.println("One Medium Box Contains" + " " + mediumBoxCount + " " + "Orders");
                            System.out.println("One Small Box Contains" + " " + nextSmallBoxCount + " " + "Orders");
                        }
                    }
                }
                else if (largeBox > 1) {
     
                    if (mediumBox == 1) {
     
                        if (smallBox > 1) {
     
                            if (firstSmallBoxCount >= 5) {
     
                                System.out.print("\n");
                                System.out.println("Each Large Box Contains" + " " + largeBoxCount + " " + "Orders");
                                System.out.println("One Medium Box Contains" + " " + mediumBoxCount + " " + "Orders");
                                System.out.println("The First Small Box Contains" + " " + firstSmallBoxCount + " " + "Orders");
                                System.out.println("The Remaining Small Box Contains" + " " + nextSmallBoxCount + " " + "Orders");
                            }
                        }
                        else if (smallBox == 1) {
     
                            System.out.print("\n");
                            System.out.println("Each Large Box Contains" + " " + largeBoxCount + " " + "Orders");
                            System.out.println("One Medium Box Contains" + " " + mediumBoxCount + " " + "Orders");
                            System.out.println("One Small Box Contains" + " " + nextSmallBoxCount + " " + "Orders");
                        }
                    }
                }
            }
     
            totalBox = largeBox + mediumBox + smallBox;
     
            if (totalBox > 1) {
     
                System.out.print("\n");
                System.out.println("The Shipment Will Carry A Total Of: " + totalBox + " " + "Boxes");
            }
            else if (totalBox == 1) {
     
                System.out.print("\n");
                System.out.println("The Shipment Will Carry A Total Of: " + totalBox + " " + "Box");
            }
          }
            while (ContinueOrdering());
        }
     
        public static boolean ContinueShipment() throws IOException {
     
            String cont;
     
            System.out.print("\n");      
            System.out.print("Do You Wish To Include Your Orders To The Shipment?: ");
            cont = br.readLine();
     
            if ((cont.equalsIgnoreCase("Y")) || (cont.equalsIgnoreCase("YES"))) {
     
                return false;
            }
            else if ((cont.equalsIgnoreCase("N")) || (cont.equalsIgnoreCase("NO"))) {
     
                System.out.print("\n");
                System.out.print("Come Again!");
                return false;
            }
            else {
     
                return false;
            }
        }
     
        public static boolean ReOrder() throws IOException {
     
            String reOrder;
     
            System.out.print("\n");
            System.out.println("And Your Orders Might Scramble With Other Orders");
            System.out.print("Do You Want To Order Again?: ");
            reOrder = br.readLine();
     
            if ((reOrder.equalsIgnoreCase("Y")) || (reOrder.equalsIgnoreCase("YES"))) {
     
                System.out.print("\n");
                return true;
            }
            else if ((reOrder.equalsIgnoreCase("N")) || (reOrder.equalsIgnoreCase("NO"))) {
     
                return false;
            }
            else {
     
                return false;
            }
        }
     
        public static boolean ContinueOrdering() throws IOException {
     
            String cont;
     
            System.out.print("\n");
            System.out.print("Continue Ordering? :");
            cont = br.readLine();
     
     
            if ((cont.equalsIgnoreCase("Y")) || (cont.equalsIgnoreCase("YES"))) {
     
                System.out.print("\n");
                return true;
            }
            else if ((cont.equalsIgnoreCase("N")) || (cont.equalsIgnoreCase("NO"))) {
     
                return false;
            }
            else {
     
                return false;
            }
        }        
    }

    2nd

     
    package xTestsx;
     
    import java.io.*;
     
    /**
     *
     * @author
     */
     
    //I just have to make a control flow for the number of orders that is less than 20
    public class CoffeBeans2_CONT_UF {
     
        private static int numberOfOrders;
     
        private static int largeBox = 0,
                           mediumBox = 0,
                           smallBox = 0,
                           excess = 0;
     
        private static int  orderCount = 0,
                            firstSmallBoxCount = 0,
                            nextSmallBoxCount = 0,
                            mediumBoxCount = 0,
                            largeBoxCount = 0;
     
        private static int totalBox = 0;
     
        private static BufferedReader br = new BufferedReader(new InputStreamReader(
                                           System.in));
     
        public static void main(String[] args) throws IOException {
     
     
          do {
            do {
     
                do {
     
                    System.out.print("Order: ");
                    numberOfOrders = Integer.parseInt(br.readLine());
     
                    if (numberOfOrders <= 5) {
     
                        firstSmallBoxCount = numberOfOrders;
                        smallBox++;
     
                        System.out.print("\n");
                        System.out.println("Your Order Is Too Small.");
                    }
                    else if (numberOfOrders < 10) {
     
                        firstSmallBoxCount = 5;
     
                        for (orderCount = numberOfOrders; orderCount >= 5; orderCount = orderCount - 5) {
     
                            smallBox++;
                            excess = orderCount - 5;
                        }
     
                        if ((excess <= 4) && (excess != 0)) {
     
                            nextSmallBoxCount = excess;
                            excess = 0;
                        }
     
                        System.out.print("\n");
                        System.out.println("Your Order Is Too Small.");
                    }
                }
                while (numberOfOrders < 10 && ReOrder());
     
                if (numberOfOrders >= 10) {
     
                    break;
                }
            }
            while (ContinueShipment()); //this part
     
            if (numberOfOrders < 20) {
     
                mediumBoxCount = 10;
     
                for (orderCount = numberOfOrders; orderCount >= 10; orderCount = orderCount - 10) {
     
                    mediumBox++;
                    excess = orderCount - 10;
                }
     
                if (excess >= 5 ) {
     
                    firstSmallBoxCount = 5;
     
                    for (; excess >= 5; excess = excess - 5) {
     
                        smallBox++;
                    }
                }
     
                if ((excess <= 4) && (excess != 0)) {
     
                    smallBox++;
                    nextSmallBoxCount = excess;
                    excess = 0;
                }
            }
            else if (numberOfOrders >= 20) {
     
                largeBoxCount = 20;
     
                for (orderCount = numberOfOrders; orderCount >= 20; orderCount = orderCount - 20) {
     
                    largeBox++ ;
                    excess = orderCount - 20;
                }
     
                if (excess >= 10) {
     
                    mediumBoxCount = 10;
     
                    for (; excess >= 10; excess = excess - 10) {
     
                        mediumBox++;
                    }
                }
     
                if (excess >= 5) {
     
                    firstSmallBoxCount = 5;
     
                    for (; excess >= 5; excess = excess - 5) {
     
                        smallBox++;
                    }
                }
     
                if ((excess <= 4) && (excess != 0)) {
     
                    smallBox++;
                    nextSmallBoxCount = excess;
                    excess = 0;
                }
            }
     
     
            if (largeBox == 0) {
     
                if ((mediumBox == 1) && (smallBox > 1)) {
     
                    System.out.print("\n");
                    System.out.println("Medium Box Contains " + mediumBoxCount + " Orders");
                    System.out.println("The First Small Box Contains " + firstSmallBoxCount + " Orders");
                    System.out.println("And The Remaining Small Box Contains " + nextSmallBoxCount + " Orders");
                }
                else if ((mediumBox == 1) & (smallBox == 1)) {
     
                    if (firstSmallBoxCount == 5) {
     
                        System.out.print("\n");
                        System.out.println("Medium Box Contains " + mediumBoxCount + " Orders");
                        System.out.println("Small Box Contains " + firstSmallBoxCount + " Orders");
                    }
                    else if (nextSmallBoxCount < 5) {
     
                        System.out.print("\n");
                        System.out.println("Medium Box Contains " + mediumBoxCount + " Orders");
                        System.out.println("Small Box Contains " + nextSmallBoxCount + " Orders");
                    }
                }
                else if ((mediumBox == 1) && (smallBox == 0)) {
     
                    System.out.print("\n");
                    System.out.println("Medium Box Contains " + mediumBoxCount + " Orders");
                }
                else if ((mediumBox == 0) && (smallBox > 1)) {
     
                    System.out.println("\n");
                    System.out.println("The First Small Box Contains " + firstSmallBoxCount + " Orders");
                    System.out.println("And The Remaining Small Box Contains " + nextSmallBoxCount + " Orders");
                }
            }
            //END
     
            else if ((mediumBox == 0) && (smallBox == 0)) {
     
                System.out.print("\n");
                System.out.println(largeBox  +  " " + "Large Box");
     
                if (largeBox == 1) {
     
                    System.out.print("\n");
                    System.out.println("Large Box Contains" + " " + largeBoxCount + " " + "Orders");
                }
                else if (largeBox > 1) {
     
                    System.out.print("\n");
                    System.out.println("Each Large Box Contains" + " " + largeBoxCount + " " + "Orders");
                }
            }
            else if (mediumBox <= 0) {
     
                System.out.print("\n");
                System.out.println(smallBox + " " + "Small Box");
                System.out.println(largeBox  +  " " + "Large Box");
     
                if (largeBox == 1) {
     
                    if (firstSmallBoxCount < 5) {
     
                        System.out.print("\n");
                        System.out.println("Large Box Contains" + " " + largeBoxCount + " " + "Orders");
                        System.out.println("And The Small Box Contains" + " " + nextSmallBoxCount + " " + "Order");
                    }
                    else if (firstSmallBoxCount >= 5) {
     
                        System.out.print("\n");
                        System.out.println("Each Large Box Contains" + " " + largeBoxCount + " " + "Orders");
                        System.out.println("The First Small Contains" + " " + firstSmallBoxCount + " " + "Orders");
                        System.out.println("And The Reamaining Small Box Contains" + " " + nextSmallBoxCount + " " + "Orders");
                    }
                }
                else if (largeBox > 1) {
     
                    if (firstSmallBoxCount >= 5) {
     
                        System.out.print("\n");
                        System.out.println("Each Large Box Has" + " " + largeBoxCount + " " + "Orders");
                        System.out.println("The First Small Box Has" + " " + firstSmallBoxCount + " " + "Orders");
                        System.out.println("And The Reamaining Small Box Has" + " " + nextSmallBoxCount + " " + "Orders");
                    }
                    else if (firstSmallBoxCount < 5) {
     
                        System.out.print("\n");
                        System.out.println("Each Large Box Has " + " " + largeBoxCount + " " + "Orders");
                        System.out.println("And The Small Box Has" + " " + nextSmallBoxCount + " " + "Order");
                    }
                }
            }
            else if (smallBox <= 0) {
     
                System.out.print("\n");
                System.out.println(mediumBox + " " + "Medium Box");
                System.out.println(largeBox  +  " " + "Large Box");
     
                if (mediumBox == 1) {
     
                    if (largeBox == 1) {
     
                        System.out.print("\n");
                        System.out.println("One Large Box Contains " + " " + largeBoxCount + " " + "Orders");
                        System.out.println("One Medium Box Contains" + " " + mediumBoxCount + " " + "Orders");
                    }
                    else if (largeBox > 1) {
     
                        System.out.print("\n");
                        System.out.println("Each Large Box Contains" + " " + largeBoxCount + " " + "Orders");
                        System.out.println("One Medium Box Contains" + " " + mediumBoxCount + " " + "Orders");
                    }
                }
            }
            else if ((largeBox >= 1) && (mediumBox >= 1) && (smallBox >= 1)) {
     
                System.out.print("\n");
                System.out.println(smallBox + " " + "Small Box");
                System.out.println(mediumBox + " " + "Medium Box");
                System.out.println(largeBox  +  " " + "Large Box");
     
                if (largeBox == 1) {
     
                    if (mediumBox == 1) {
     
                        if (smallBox > 1) {
     
                            if (firstSmallBoxCount >= 5) {
     
                                System.out.print("\n");
                                System.out.println("One Large Box Contains" + " " + largeBoxCount + " " + "Orders");
                                System.out.println("One Medium Box Contains" + " " + mediumBoxCount + " " + "Orders");
                                System.out.println("The First Small Box Contains" + " " + firstSmallBoxCount + " " + "Orders");
                                System.out.println("The Remaining Small Box Contains" + " " + nextSmallBoxCount + " " + "Orders");
                            }
                        }
                        else if (smallBox == 1) {
     
                            System.out.print("\n");
                            System.out.println("One Large Box Contains" + " " + largeBoxCount + " " + "Orders");
                            System.out.println("One Medium Box Contains" + " " + mediumBoxCount + " " + "Orders");
                            System.out.println("One Small Box Contains" + " " + nextSmallBoxCount + " " + "Orders");
                        }
                    }
                }
                else if (largeBox > 1) {
     
                    if (mediumBox == 1) {
     
                        if (smallBox > 1) {
     
                            if (firstSmallBoxCount >= 5) {
     
                                System.out.print("\n");
                                System.out.println("Each Large Box Contains" + " " + largeBoxCount + " " + "Orders");
                                System.out.println("One Medium Box Contains" + " " + mediumBoxCount + " " + "Orders");
                                System.out.println("The First Small Box Contains" + " " + firstSmallBoxCount + " " + "Orders");
                                System.out.println("The Remaining Small Box Contains" + " " + nextSmallBoxCount + " " + "Orders");
                            }
                        }
                        else if (smallBox == 1) {
     
                            System.out.print("\n");
                            System.out.println("Each Large Box Contains" + " " + largeBoxCount + " " + "Orders");
                            System.out.println("One Medium Box Contains" + " " + mediumBoxCount + " " + "Orders");
                            System.out.println("One Small Box Contains" + " " + nextSmallBoxCount + " " + "Orders");
                        }
                    }
                }
            }
     
            totalBox = largeBox + mediumBox + smallBox;
     
            if (totalBox > 1) {
     
                System.out.print("\n");
                System.out.println("The Shipment Will Carry A Total Of: " + totalBox + " " + "Boxes");
            }
            else if (totalBox == 1) {
     
                System.out.print("\n");
                System.out.println("The Shipment Will Carry A Total Of: " + totalBox + " " + "Box");
            }
          }
            while (ContinueOrdering());
        }
     
        public static boolean ContinueShipment() throws IOException {
     
            String cont;
     
            System.out.print("\n");
            System.out.print("Do You Wish To Include Your Orders To The Shipment?: ");
            cont = br.readLine();
     
            if ((cont.equalsIgnoreCase("Y")) || (cont.equalsIgnoreCase("YES"))) {
     
                return false;
            }
            else if ((cont.equalsIgnoreCase("N")) || (cont.equalsIgnoreCase("NO"))) {
     
                largeBox = 0;
                mediumBox = 0;
                smallBox = 0;
                excess = 0;
                numberOfOrders = 0;
                orderCount = 0;
                firstSmallBoxCount = 0;
                nextSmallBoxCount = 0;
                mediumBoxCount = 0;
                largeBoxCount = 0;
                return false;
            }
            else {
     
                return false;
            }
        }
     
        public static boolean ReOrder() throws IOException {
     
            String reOrder;
     
            System.out.print("\n");
            System.out.println("And Your Orders Might Scramble With Other Orders");
            System.out.print("Do You Want To Order Again?: ");
            reOrder = br.readLine();
     
            if ((reOrder.equalsIgnoreCase("Y")) || (reOrder.equalsIgnoreCase("YES"))) {
     
                System.out.print("\n");
                return true;
            }
            else if ((reOrder.equalsIgnoreCase("N")) || (reOrder.equalsIgnoreCase("NO"))) {
     
                return false;
            }
            else {
     
                return false;
            }
        }
     
        public static boolean ContinueOrdering() throws IOException {
     
            String cont;
     
            System.out.print("\n");
            System.out.print("Continue Ordering? :");
            cont = br.readLine();
     
     
            if ((cont.equalsIgnoreCase("Y")) || (cont.equalsIgnoreCase("YES"))) {
     
                System.out.print("\n");
                return true;
            }
            else if ((cont.equalsIgnoreCase("N")) || (cont.equalsIgnoreCase("NO"))) {
     
                return false;
            }
            else {
     
                return false;
            }
        }
    }


    my logic is this.... in the part of the method ContinueOrdering()..(each code) it will ask me if i want to include the orders in the shipment... i having a hard problem with the first code so what i did is the second code..

    if i enter "n" or "no" it will RESET all The VALUES of the DATA MEMBERS(field),,
    or lets just say that i the method ask me
    If I Want To Include THe Orders In the Shipment
    and i answered no.. then it will not ADD.. (simply by reseting the value of each FIELD)..

    problem:

    1.) regarding with static fields , those fields as you can see is ineffient because its not a constant nor a class datamember,
    2.) i want to resolve this by using the first code,, (using local variables instead of datamembers/fields)
    3.) is there any method in java that can reset a value of a variable? (field or local)
    4.) or one of the effient way to do this is to define a class for this program?
    Last edited by chronoz13; October 21st, 2009 at 10:04 AM.


  2. #2
    Member
    Join Date
    Oct 2009
    Posts
    52
    Thanks
    0
    Thanked 6 Times in 5 Posts

    Default Re: Reset the value of each variable

    This code will loop until your continueOrdering method returns false. If you have any questions, please ask.

    // initialize your variables
    int one = 0;
    int two = 0;
     
    while (true) {
     
        // do your logic
     
        if (continueOrdering()) {
            // reset your variables
        }
        else {
            break;
        }
     
    }

  3. #3
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default Re: Reset the value of each variable

    I'd say if possible avoid using the while(true), for( or any other type of infinite loop definition. It makes reading the code very hard and you need to be really sure about your code inside the block that it will eventually break out of it.

    You might possibly run into compiler errors with unreachable code if you use these types of blocks.

    // Json

  4. #4
    Member
    Join Date
    Oct 2009
    Posts
    52
    Thanks
    0
    Thanked 6 Times in 5 Posts

    Default Re: Reset the value of each variable

    Quote Originally Posted by Json View Post
    I'd say if possible avoid using the while(true), for( or any other type of infinite loop definition. It makes reading the code very hard and you need to be really sure about your code inside the block that it will eventually break out of it.

    You might possibly run into compiler errors with unreachable code if you use these types of blocks.

    // Json
    Isn't that the job of the programmer to begin with?

    For production code, I would say you're right if there is a case the loop might never stop. If you're just writing a toy program, then there is no harm.

  5. #5
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: Reset the value of each variable

    the suggestion for this is?

  6. #6
    Member
    Join Date
    Oct 2009
    Posts
    52
    Thanks
    0
    Thanked 6 Times in 5 Posts

    Default Re: Reset the value of each variable

    Quote Originally Posted by chronoz13 View Post
    the suggestion for this is?
    Did you see my first post?

    To make Json happy, here's a version without an infinite loop. ;-) It is still just as "unsafe" as an infinite loop, but with different constructs.

    What I am offering you is only a template. You still need to add your logic.

    // initialize your variables
    int one = 0;
    int two = 0;
     
    boolean run = true;
     
    while (run) {
     
        // do your logic
     
        if (continueOrdering()) {
            // reset your variables
        }
        else {
            run = false;
        }
     
    }

Similar Threads

  1. Problem with reset on JComboBox
    By WorkingMan in forum AWT / Java Swing
    Replies: 4
    Last Post: April 25th, 2013, 12:19 PM
  2. [SOLVED] Fixing of bug for a small program
    By Koren3 in forum Threads
    Replies: 3
    Last Post: April 21st, 2009, 06:28 AM
  3. Replies: 24
    Last Post: April 14th, 2009, 03:43 PM
  4. [SOLVED] Problem while getting a number from another thread
    By Koren3 in forum Threads
    Replies: 4
    Last Post: April 7th, 2009, 01:42 PM
  5. How to do thread communication in java
    By Koren3 in forum Threads
    Replies: 4
    Last Post: March 29th, 2009, 10:49 AM