Stuck on a loop...again

Discussion in 'Mac Programming' started by bill macleod, Sep 30, 2006.

  1. bill macleod macrumors newbie

    Sep 8, 2006
    Here I am up way to late trying to get all my semi-colons to behave.

    The problem is: user enters name to search for in a dialog box.
    I need to use a flag controlled while loop with a compound conditional expression
    that utilizes the boolean variable found and the scanner method hasNext
    to search through a local file and return the price(which is on the next line after the item in the file).
    I get the general idea; that I need a while expression that matches the user input to each line in the
    local file and if it returns false goes through the loop again till it matches. But I'm stuck.

    After a couple of hours typing and deleting
    and moving things around and looking in my books
    and online......this is all I have:

    Scanner fr = new Scanner(new FileReader("invoice.dat"));
               double t = 0;
        	     String u = null;
    	     String usrIn;
         Icon Q = new ImageIcon("Q_icon.jpg");
    	JLabel Qjl = new JLabel("Please enter the name of the item you " +
    				  "want information about...", Q, JLabel.CENTER);
    	usrIn = JOptionPane.showInputDialog(null, Qjl, "SUPER INVENTORY " +
    	     "SEARCH FACILITATOR ©2006", JOptionPane.PLAIN_MESSAGE);  
    boolean found = (usrIn.equals(fr.hasNext()));
    It is probably obvious that this is not working.
    It seems like there are a few concepts I dont have a grip on.

    I would appreciate a shove in the right direction, gentle or otherwise.
    Thanks for your time.

  2. deputy_doofy macrumors 65816


    Sep 11, 2002
    Admittedly, it has been some time since I worked with a programming language that could read external data/text files. As a guess, your loop may never end because information cannot be found. Further examination shows that you only check for the "next" item outside of your while loop, so there's no iteration. In other words, found never looks for the "next" piece of data beyond its first check, therefore it is always true (and thus, stuck in the loop). Perhaps the following might fix your code...
    boolean found = (usrIn.equals(fr.hasNext()));
    		while(!found && !fr.eof()){
    			// stuff
                            // more stuff
                            [b]found = (usrIn.equals(fr.hasNext()));[/b]
    Edit: I added the !fr.eof (and not sure if that exists in java, but eof is generally an end of file check in some languages) because it is possible that the info may NEVER be found, yet the search has come to the end of the data file. The loop should break in that instance too.
  3. Grover macrumors member

    May 14, 2004
    It looks to me as if you're using the hasNext() method in an unintended way. According to the documentation it returns a boolean if there are more tokens in the input:

    Returns true if this scanner has another token in its input.

    It looks to me as if you want to call it this way:

    hasNext(Pattern pattern)
    Returns true if the next complete token matches the specified pattern.

    And pass it a pattern matching the user's input as the argument.

    What you're trying to do now is compare the boolean returned from the hasNext() method to the user's input and I can't see that's what you want. I haven't tried this myself but it seems to me that even if it runs you may find that you've got the equivalent of while(true) until you reach the EOF.

    That said, I think you may be looking at the problem backwards. You're looking for the one line that matches, not all the lines that don't. You might want to try something more along the lines of:

    while (fr.hasNext()) //if there's another token
    //check whether it matches your pattern
    if (usrIn.equals(
    //and, if it does, do whatever you need to do

    It also looks as if your requirement is that you compare lines but you're actually comparing tokens. Without knowing what your file looks like, it's hard to know if that's what you want. If you actually want to compare lines, you may want to set a newline as the delimiter for your scanner so that it tokenizes the lines.

    I was looking at the documentation at:
  4. bill macleod thread starter macrumors newbie

    Sep 8, 2006
    thanks to you

    Just wanted to say thank a lot for your replies. I'm going to spend the afternoon working on this.

Share This Page