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 7 of 7

Thread: Need help with looping

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need help with looping

    So the question asks us to use a sentinel value to end a program, but the sentinel value is 999, but the while loop only recognises strings... Im totally lost guys. Hope you can help me

     
       static Scanner console = new Scanner (System.in);
       public static void main (String[] args)
                               throws FileNotFoundException
     
        {
     
             PrintWriter outfFile = new PrintWriter ("Invoice.txt")   ;
     
             String firstItem;
             String item ;
             int price;
     
             System.out.println(" Please scan the name of the first item: ");
             firstItem = console.next();
     
             while (item != 999)
     
             {
                System.out.println("Enter the next item");
                item = console.next();
                System.out.println("Enter the price of the " + item);
                price = console.nextInt();
                outFile.Println("Item Name    Item Price");
                outFile.println( item + " " + price);
     
     
     
         }
       }


    --- Update ---

    If you would like me to post the complete question, I'd be happy to do so


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Need help with looping

    IF you are getting an error message, please copy the full text and paste it here.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help with looping

    This is the error I am getting.


    ----jGRASP exec: javac -g Scan1.java

    Scan1.java:23: error: incomparable types: String and int
    while (item != 999)
    ^
    Scan1.java:30: error: cannot find symbol
    outFile.Println("Item Name Item Price");
    ^
    symbol: variable outFile
    location: class Scan1
    Scan1.java:31: error: cannot find symbol
    outFile.println( item + " " + price);
    ^
    symbol: variable outFile
    location: class Scan1
    3 errors

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

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Need help with looping

    Check that you have spelled the names correctly.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    May 2013
    Location
    Charleston SC
    Posts
    21
    Thanks
    0
    Thanked 8 Times in 7 Posts

    Default Re: Need help with looping

    item is a String. You are comparing an int to a String in item!=999. Your issue would be because of this fact.

    --- Update ---

    The other issue you are having is outFile doesn't exist. My advise is to use a System.out.println( instead of outFile.Println(
    Last edited by jbarke12; March 24th, 2014 at 10:53 AM.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Need help with looping

    @jbarke12 The java class is spelled: String not string
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Oct 2013
    Location
    Manila, Philippines
    Posts
    285
    My Mood
    Amused
    Thanks
    6
    Thanked 64 Times in 61 Posts

    Default Re: Need help with looping

    your code:
    String item; // a String object
    while (item != 999) // String compares to integer

    You are comparing a String object to an integer. if you want that kind of comparison, you better parse the String to integer first then do the comparison

    Scan1.java:30: error: cannot find symbol
    outFile.Println("Item Name Item Price");
    ^
    you don't have outFile variable, but you do have outfFile variable PrintWriter outfFile = new PrintWriter ("Invoice.txt");, please check the spelling.
    but, it would still have an error since PrintWriter class doesn't have an Println method, but it has println.
    Println is different from println, java is a case sensitive programming language.

Similar Threads

  1. DO WHILE LOOPING
    By njabulo ngcobo in forum What's Wrong With My Code?
    Replies: 14
    Last Post: May 22nd, 2013, 09:57 AM
  2. Looping Over and Over Again?
    By avalanche72 in forum Loops & Control Statements
    Replies: 1
    Last Post: February 1st, 2012, 05:11 AM
  3. Need help with looping!
    By crsoccerplayer6 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 29th, 2011, 04:09 PM
  4. Help in looping
    By endframe in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 28th, 2010, 03:24 PM
  5. [SOLVED] looping, for,while,do-while.
    By chronoz13 in forum Loops & Control Statements
    Replies: 4
    Last Post: August 6th, 2009, 01:32 PM