Results 1 to 14 of 14
  1. #1
    moncur is offline Member
    Join Date
    Oct 2010
    Posts
    26
    Rep Power
    0

    Default help with loops and output

    Write a code segment to read in first names until the user enters quit for the name (the letters in “quit” can be of any case). Create a new string that contains all the names entered separated by a comma. Display the string of names at the end. Use a while loop. The format of the output should be:

    Manjula, Homer, Karl, Maggie

    this is what i got so far...it does not quit when i type quit, i need it to stop the loop when i type "quit". Also how do i output the names that are inputted from user, i need it to output like this: Manjula, Homer, Karl, Maggie

    Java Code:
    import java.util.Scanner;
    public class Quiz6Q5
    {
    
    
        public static void main(String[] args)
        {
            Scanner scan = new Scanner ( System.in );
            String firstName = null;
            
            while (firstName != "quit" )
            {
                System.out.println("Enter first name:");
                firstName = scan.next();
    
            }
            
        }
    
    }

  2. #2
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    Do not use == or != when comparing Strings or any other object (non-primitive). The method equals() is there for a reason!
    Ever seen a dog chase its tail? Now that's an infinite loop.

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Here's the API docs for String's equals(). Just posting that in case you poke around and find anything else useful thereabouts.

  4. #4
    moncur is offline Member
    Join Date
    Oct 2010
    Posts
    26
    Rep Power
    0

    Default so...

    so how do i do a loop that gets the names from the user entering them and only quit the loop when the user types "quit"

    while ( name.equalsIgnoreCase("quit) ) // what statement can i use to say while name is not equal to quit? or is there a better way all around to do what i am trying to do?

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    !

    (Ignore this line: the forum software won't let me post a one character reply.)

  6. #6
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    To elaborate on pbrockways post, ! is the negative operator, meaning it changes a boolean:
    Java Code:
    !true == false
    You can also prepend ! to a method call that returns a boolean, in this case, your equalsIgnoreCase():
    Java Code:
    while(!input.equalsIgnoreCase("quit"))
    Ever seen a dog chase its tail? Now that's an infinite loop.

  7. #7
    moncur is offline Member
    Join Date
    Oct 2010
    Posts
    26
    Rep Power
    0

    Default

    while (!firstName.equalsIgnoreCase("quit") ) //does not work.


    i get this error:
    Exception in thread "main" java.lang.NullPointerException
    at Quiz6Q5.main(Quiz6Q5.java:16)

  8. #8
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    That's because firstName is null the first time you hit that loop. There are, traditionally, two ways around this:

    Java Code:
    while(firstName == null || !firstName.equalsIgnoreCase("quit")) {
    
    // and
    
    while(!"quit".equalsIgnoreCase(firstName)) {

  9. #9
    moncur is offline Member
    Join Date
    Oct 2010
    Posts
    26
    Rep Power
    0

    Default

    Quote Originally Posted by pbrockway2 View Post
    That's because firstName is null the first time you hit that loop. There are, traditionally, two ways around this:

    Java Code:
    while(firstName == null || !firstName.equalsIgnoreCase("quit")) {
    
    // and
    
    while(!"quit".equalsIgnoreCase(firstName)) {
    this works thank you...

    now what about saving the names that are entered in and displaying the names like this:

    Bob, John, Amy, Adam [quit]

  10. #10
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Yes, exactly. What about that?

    Do you have any ideas? Could you print the names as soon as you know about them? Or do you have to save them and wait? And if you have to save them, what type would you use to hold a whole lot of Strings?

  11. #11
    moncur is offline Member
    Join Date
    Oct 2010
    Posts
    26
    Rep Power
    0

    Default

    i am not sure... i think as long as it's on the same line it would be correct.. in order to do that on one line i believe i would have to save the names somehow. because the line asking for another name would split all the names up.

    so basically... with the names. They want them on one line and split up with commas.

  12. #12
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    You could build a String containing all the names and output it when the loop finishes execution.
    Ever seen a dog chase its tail? Now that's an infinite loop.

  13. #13
    moncur is offline Member
    Join Date
    Oct 2010
    Posts
    26
    Rep Power
    0

    Default

    Quote Originally Posted by m00nchile View Post
    You could build a String containing all the names and output it when the loop finishes execution.
    how do you go about doing that? cause i have no idea!

  14. #14
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    Look up a guide on string concatenation. In short, you can combine two strings like this:
    Java Code:
    String s = "ab"+"cd"; //same as String s = "abcd";
    Ever seen a dog chase its tail? Now that's an infinite loop.

Similar Threads

  1. Help with loops!
    By jrelvi23 in forum New To Java
    Replies: 9
    Last Post: 09-04-2012, 01:36 PM
  2. Replies: 0
    Last Post: 09-30-2010, 08:16 PM
  3. need some help with loops!
    By Chewart in forum New To Java
    Replies: 2
    Last Post: 12-03-2009, 11:32 PM
  4. these loops...
    By Blaedel in forum New To Java
    Replies: 0
    Last Post: 10-01-2009, 06:59 PM
  5. Java, output string, getting correct output? HELP!
    By computerboyo in forum New To Java
    Replies: 2
    Last Post: 02-25-2009, 11:44 PM

Posting Permissions

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