8 Replies Latest reply on Apr 17, 2008 3:21 PM by 807601

    Variable string output

    807601
      I have searched tons of forums and cant seem to find code that will allow me to output the users string inputs to the dos screen.

      An example of this would be.

      Enter shopping items: Ham
      another?: yes
      Enter shopping items: Eggs
      another?: no

      Your list contains:

      Ham
      Eggs

      here is my code:
      System.out.println("This program will create a shopping list:");
      String keepGoing = "yes";
      String item = null;
      Scanner keyboard = new Scanner(System.in);
      for (int count = 1; count < 999 && keepGoing.equalsIgnoreCase("yes");count++)
      {//ask for input, write it to a file
      System.out.println("Please enter item #" + count + ":");
      item = keyboard.nextLine();
      inputItems.println(count + " " + item);
      //ask if you want to keep going
      System.out.println("Do you wish to enter more items (yes/no)");
      keepGoing = keyboard.nextLine();
      System.out.println("Your shopping list contains: " + );
      ***** Not sure what to put in the above ("Your shopping list contains: " + ) "******
      }

      Any help?
        • 1. Re: Variable string output
          807601
          check the comments...

                 // ...... (your other bits of code)
                  
                  // Use a boolean variable instead of a Stirng
                  boolean keepGoing = true;
                  
                  // keep looping until user says 'no'
                  while(keepGoing)
                  {
          
                      
                      // ...... (your other bits of code)
                      
                      
                      System.out.println("Do you wish to enter more items (yes/no)");
                      
                      String temp = keyboard.nextLine();
                      
                      // anything other than yes is a no!
                      keepGoing = temp.equalsIgnoreCase("yes");
                      
                   // ...... (your other bits of code)
                      
                  }
          hope this helps...


          ----------------


          deepak@1your.com
          • 2. Re: Variable string output
            807601
            // SNIP------------------
            
            boolean askForMore = true;
            
            while(askForMore)
            {
                Scanner keyboard = new Scanner(System.in);
                // SNIP------------------
                if(keepGoing.equalsIgnoreCase("no"))
                  askForMore = false;
                else
                  askForMore = true;
            }
            // SNIP------------------
            Try that to keep your loop going.

            Then store your values in a temporary String that you concatenate to with every new addition of input. Finally display the entire String at
            System.out.println("Your shopping list contains: " + tempString);
            • 3. Re: Variable string output
              807601
              i am a newbie compared to most of the java users here so forgive me for not understanding how replacing the for loop with a while loop would help output all of the users entries to the screen. Can you explain?
              • 4. Re: Variable string output
                800308
                Do NOT get a new Scanner in every loop!
                • 5. Re: Variable string output
                  807601
                  lol......maybe im missing something, should i be using 2 loops...3 loops?
                  • 6. Re: Variable string output
                    800308
                    This isn't compiled, let alone tested...
                    Console console = System.console();
                    
                    private doSomeStuff() {
                      do {
                        // do stuff
                      } while ( continue() );
                    }
                    
                    private boolean continue() {
                      System.out.print("Do you wish to continue (Y/N) : ");
                      String response = console.readLine();
                      return response!=null && response.charAt(0).toUpperCase()=='Y';
                    }
                    Edited by: corlettk on 17/04/2008 15:05 - that's better...

                    Edited by: corlettk on 17/04/2008 15:05 - that might actually work
                    • 7. Re: Variable string output
                      807601
                      beatjunkie wrote:
                      i am a newbie compared to most of the java users here so forgive me for not understanding how replacing the for loop with a while loop would help output all of the users entries to the screen. Can you explain?
                      The for loop is fixed. Think of a case where you feel that the maximum number of entries a person can expect to input is 10. Now suppose that there is this user that just feels that 11 entries are enough for her. What happens then? Your code breaks!

                      The while loop, we may safely say, is indefinite. It depends on a certain condition (in this case her answering "no" to a question). This may happen on the first entry, third or even Nth time. But whatever the time it comes, your code is ready for her.

                      Read more on [Control Flow Statements from the Java Tutorial|http://java.sun.com/docs/books/tutorial/java/nutsandbolts/flow.html].
                      • 8. Re: Variable string output
                        807601
                        Jamwa,

                        thanks for the explanation!