Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Dec 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Infinite while loop problem

    I've spent over 3 hours trying to figure out why this loop won't break. This loop reads in 3 values and stores them in an object. The entire program runs fine, its just that the loop won't' break. Assume that the method i call works fine. Also, SECSENT = EndSearchKeys. Also here's a sample input:
    Lexus 2005 23678.0
    Ford 2001 7595.0
    Honda 2004 15500.0
    EndSearchKeys



    while(scan.hasNext())
    {
    if(carMake.equals(SECSENT))
    {
    break;
    }
    if(scan.hasNextInt())
    {
    carYear = scan.nextInt();
    }
    else
    {
    System.out.println("ERROR - not an int");
    System.exit(0);
    }
    if(scan.hasNextDouble())
    {
    carPrice = scan.nextDouble();
    }
    else
    {
    System.out.println("ERROR - not a double");
    System.exit(0);
    }
    Car key = new Car(carMake, carYear, carPrice);
    // Stores the output of seqSearch in pos.

    // If the debug switch is on, then it prints these statements.
    if(DEBUG_SW == true)
    {
    System.out.println("Search, make = " + key.getMake());
    System.out.println("Search, year = " + key.getYear());
    System.out.println("Search, price = " + key.getPrice());
    }
    System.out.println("key =");
    System.out.println(key);
    pos = seqSearch(carArr, count, key);
    if(pos != -1)
    {
    System.out.println("This vehicle was found at index = " + pos);
    }
    else
    {
    System.out.println("This vehicle was not found in the database.");
    }
    if(scan.hasNext())
    {
    carMake = scan.next();
    }
    else
    {
    System.out.println("ERROR - not a String");
    System.exit(0);
    }

    }

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,910
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    This is the JavaScript forum. Java and Javascript are entirely different programming languages, in spite of the confusingly similar names. Rather like Austria and Australia! Ask a mod to move this thread to the right forum.

    BTW, when posting here please help us to help you by following the posting guidelines and wrapping your code in CODE tags. This means use the octothorpe or # button on the toolbar. You can (and should) edit your previous post.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •