Welcome to Dream.In.Code
Become a Java Expert!

Join 149,585 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,778 people online right now. Registration is fast and FREE... Join Now!




Another little uh-oh while while loops

 
Reply to this topicStart new topic

Another little uh-oh while while loops, SENTINEL value confusion

sgmikewoody
17 Oct, 2007 - 06:25 PM
Post #1

New D.I.C Head
*

Joined: 30 Sep, 2007
Posts: 15


My Contributions
Branching from the last assignment, I suppose some confusion still lingers on the while loops. I've set a sentinel value as a string, but whenever the string value is entered as the sentinel value (999), it doesn't recognize it as a sentinel value and the loop keeps repeating.

The assignment is simple enough. Keep entering names of items until 999 is entered, then kill the loop and present the number of items that were scanned. Save the names and prices to an output file.

Any help is appreciated, thanks.

CODE
import java.io.*;
import java.util.*;

public class UPCCodes
{    
    static final String SENTINEL = "999";
    
    public static void main(String [] args) throws
                                                            FileNotFoundException
    {
            
        PrintWriter outFile = new
                            PrintWriter("c:\\Java\\invoice1.dat");
        
        Scanner console = new Scanner(System.in);

        double price;
        String item_name;
        int items_scanned = 0;
        
        System.out.println("This order-processing program simulates "
                                + "using a \ncode reader to scan an item "
                                + "and create an invoice.\n");
                                
            
        while (item_name != SENTINEL)
            {
            System.out.println("Please scan the name of the first item: ");
            item_name = console.next();
            System.out.println("Please scan the price of the " +item_name
                                    +": ");
            price = console.nextDouble();
            ++items_scanned;
            
            outFile.println(item_name);
            outFile.println(price);
                    
            }
            
            
        System.out.print("\n" + items_scanned + " items scanned.");
            outFile.close();
    }
    
}

User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Another Little Uh-oh While While Loops
17 Oct, 2007 - 06:54 PM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,655



Thanked: 313 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Just so you know, most programmers think of a sentinel as a specific numeric value... usually integer. But I will let you slide. The trick here is how you are comparing your values. In the string class you will see a function called equals() which will be very useful here. The idea is this...

CODE

// Compare the value in sentinel against the item name.
while(!item_name.equals(SENTINEL)) {
      // Block of code here
}


This function is nice and clean and very easy to use for comparing strings. It essentially takes the value passed to it and compares it to the string calling it. So in the code above it takes "999" and says "are you NOT equal to the value in item_name?" If the answer is yes, we continue.

Hopefully this makes some sense.

Enjoy! smile.gif
User is offlineProfile CardPM
+Quote Post

sgmikewoody
RE: Another Little Uh-oh While While Loops
17 Oct, 2007 - 07:01 PM
Post #3

New D.I.C Head
*

Joined: 30 Sep, 2007
Posts: 15


My Contributions
QUOTE(Martyr2 @ 17 Oct, 2007 - 07:54 PM) *

Just so you know, most programmers think of a sentinel as a specific numeric value... usually integer. But I will let you slide. The trick here is how you are comparing your values. In the string class you will see a function called equals() which will be very useful here. The idea is this...

CODE

// Compare the value in sentinel against the item name.
while(!item_name.equals(SENTINEL)) {
      // Block of code here
}


This function is nice and clean and very easy to use for comparing strings. It essentially takes the value passed to it and compares it to the string calling it. So in the code above it takes "999" and says "are you NOT equal to the value in item_name?" If the answer is yes, we continue.

Hopefully this makes some sense.

Enjoy! smile.gif



Thanks for the leniency! Haha, thank you so much again.

QUOTE(sgmikewoody @ 17 Oct, 2007 - 07:56 PM) *

QUOTE(Martyr2 @ 17 Oct, 2007 - 07:54 PM) *

Just so you know, most programmers think of a sentinel as a specific numeric value... usually integer. But I will let you slide. The trick here is how you are comparing your values. In the string class you will see a function called equals() which will be very useful here. The idea is this...

CODE

// Compare the value in sentinel against the item name.
while(!item_name.equals(SENTINEL)) {
      // Block of code here
}


This function is nice and clean and very easy to use for comparing strings. It essentially takes the value passed to it and compares it to the string calling it. So in the code above it takes "999" and says "are you NOT equal to the value in item_name?" If the answer is yes, we continue.

Hopefully this makes some sense.

Enjoy! smile.gif



Thanks for the leniency! Haha, thank you so much again.



UPCCodes.java:26: variable item_name might not have been initialized
while (!item_name.equals(SENTINEL))
^
1 error

----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.

Still having a bit of difficulty with this one. I will play with it a bit more I suppose anime2.gif

CODE
import java.io.*;
import java.util.*;

public class UPCCodes
{    
    static final String SENTINEL = "999";
    
    public static void main(String [] args) throws
                                                            FileNotFoundException
    {
            
        PrintWriter outFile = new
                            PrintWriter("c:\\Java\\invoice1.dat");
        
        Scanner console = new Scanner(System.in);

        double price;
        String item_name;
        int items_scanned = 0;
        
        System.out.println("This order-processing program simulates "
                                + "using a \ncode reader to scan an item "
                                + "and create an invoice.\n");
                                
            System.out.println("Please scan the name of the first item: ");
            item_name = console.next();
            
            if(!item_name.equals(SENTINEL))
            
                {
                System.out.println("Please scan the price of the " +item_name
                                    +": ");
                price = console.nextDouble();
                ++items_scanned;
            
                outFile.println(item_name);
                outFile.println(price);
                }
                
        while (!item_name.equals(SENTINEL))
            {
                        System.out.println("Please scan the name of the first item: ");
            item_name = console.next();
            
            if(!item_name.equals(SENTINEL))
            
                {
                System.out.println("Please scan the price of the " +item_name
                                    +": ");
                price = console.nextDouble();
                ++items_scanned;
            
                outFile.println(item_name);
                outFile.println(price);
                }                    
            }
            
            
        System.out.print("\n" + items_scanned + " items scanned.");
            outFile.close();
    }
    
}

Think I got it =D

This post has been edited by sgmikewoody: 17 Oct, 2007 - 07:11 PM
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Another Little Uh-oh While While Loops
17 Oct, 2007 - 07:04 PM
Post #4

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,655



Thanked: 313 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Yeah remember to have item_name set to something the first time you go into the loop. You could even set it to an empty string if you like.

smile.gif
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 10:58PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month