1 Replies - 412 Views - Last Post: 05 October 2012 - 06:22 AM Rate Topic: -----

#1 repulsivesalvation  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 05-October 12

Extreme beginner needing help with loops

Posted 05 October 2012 - 05:56 AM

I am new to using java and I'm having a hard time understanding how to use loop statements. I can't even figure out how to start the problem I posted below. I know I have to start by asking the user to enter a sting, but thats as far as I can get. This is all I got so far and its kind of embarrassing.

        Scanner keyboard = new Scanner(System.in);
        System.out.print("Please Enter A String");
        String first  = keyboard.next();

Write a Java program that will prompt and read a series of strings from the user. Processing will stop when the user enters the string quit. The program will print the number of strings entered (excluding the string quit), the average string length, and the number of possible words. A possible word is a string that contains only letters.

Sample run:

Please enter a string: cat
Please enter a string: Dog
Please enter a string: hamster
Please enter a string: 1234
Please enter a string: quit

Number of strings: 4
Average string length: 4.25
Number of possible words: 3

Is This A Good Question/Topic? 0
  • +

Replies To: Extreme beginner needing help with loops

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2250
  • View blog
  • Posts: 5,340
  • Joined: 10-September 10

Re: Extreme beginner needing help with loops

Posted 05 October 2012 - 06:22 AM

Loops are certainly important to this program, but they might be the least of your worries.

You know the 3 lines you posted belong in a class and that class should have a main() method. When you build those necessary parts, post the whole program. It might be best at your stage to start with an outline or a skeleton of the standard parts of a Java program and build onto that.

A typical loop to do what you've described would be the do/while loop. It will do the desired action at least once:
boolean goAgain = true;
   // get the user's input
   // compare input to "quit"
   // if input is "quit" then goAgain = false
while ( goAgain );

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1