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: How To Ask the User to Enter Another Number Using the Number 1

  1. #1
    Member
    Join Date
    Apr 2012
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How To Ask the User to Enter Another Number Using the Number 1

    Hello, I made a program called count vowels and I finished it but I want to ask the user to enter another number by pressing the number 1 and any other number entered would be invalid. I use JOptionPane, please help! Thanks. Here is the code:

    package countvowels;
    import javax.swing.JOptionPane;

    public class CountVowels {

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {

    //Ask the user to enter a sentence and convert it to a string
    String text = JOptionPane.showInputDialog("Type a sentence and this program will tell you how many vowels there are (excluding 'y'): ");

    //Declare the counter and declare the vowels
    int count = 0;
    for (int i = 0; i < text.length(); i++) {
    char c = text.charAt(i);
    if (c=='a' || c=='e' || c=='i' || c=='o' || c=='u') {
    count++;
    }
    }

    //Determine the number of vowels in the sentence
    JOptionPane.showMessageDialog(null, "There are" + " " + count + " " + "vowel(s) in the phrase you entered.");

    //Ask the user if they would like to enter another word
    JOptionPane.showMessageDialog(null, "Press 1 to try a new phrase or press any other number to exit.");



    }
    }


  2. #2
    Member
    Join Date
    Apr 2012
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How To Ask the User to Enter Another Number Using the Number 1

    Sorry, I mean enter another word using the number 1.

  3. #3
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: How To Ask the User to Enter Another Number Using the Number 1

    Hello Pettsa!
    You can place your code inside a do...while loop (since you want it to execute at least once). You can then ask the user for an int like you do when you ask for a sentence, parse the String to an int with one of the Integer's methods and have your loop going for as long as user enters 1.

  4. #4
    Member
    Join Date
    Apr 2012
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How To Ask the User to Enter Another Number Using the Number 1

    Thanks, but what do i state if if they hit another number?

  5. #5
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: How To Ask the User to Enter Another Number Using the Number 1

    Quote Originally Posted by Pettsa View Post
    Thanks, but what do i state if if they hit another number?
    I'm not sure I understood your question.
    If the user inputs another number the program will stop since it will keep looping only if the user enters 1.
    Why don't you try it and see what will happen?

  6. #6
    Member
    Join Date
    Apr 2012
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How To Ask the User to Enter Another Number Using the Number 1

    Ok, I got the loop done but.... when I hit 1 the first time, it works well but when the program runs the second, I hit 1 to the second time and it exits the program. I don't know why.

    Here is the code:

    package countvowels;
    import javax.swing.JOptionPane;

    public class CountVowels {

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {

    //Ask the user to enter a sentence and convert it to a string
    String text = JOptionPane.showInputDialog("Type a sentence and this program will tell you how many vowels there are (excluding 'y'): ");

    //Declare the counter and declare the vowels
    int count = 0;
    for (int i = 0; i < text.length(); i++) {
    char c = text.charAt(i);
    if (c=='a' || c=='e' || c=='i' || c=='o' || c=='u') {
    count++;
    }
    }

    //Determine the number of vowels in the sentence
    JOptionPane.showMessageDialog(null, "There are" + " " + count + " " + "vowel(s) in the phrase you entered.");

    //Declare variables
    int i = 0;

    //Ask the user if they would like to enter another word from a string and convert to a double
    String inputNumber = JOptionPane.showInputDialog("Press 1 to try a new phrase or press any other number to exit.");
    i = Integer.parseInt(inputNumber);

    do
    {
    i++;
    JOptionPane.showInputDialog("Type a sentence and this program will tell you how many vowels there are (excluding 'y'): ");
    JOptionPane.showMessageDialog(null, "There are" + " " + count + " " + "vowel(s) in the phrase you entered.");
    JOptionPane.showInputDialog("Press 1 to try a new phrase or press any other number to exit.");

    }
    while (i == 0);
    {

    if (i == 1) {
    JOptionPane.showInputDialog("Type a sentence and this program will tell you how many vowels there are (excluding 'y'): ");
    }

    else
    System.exit(i);
    }
    }
    }

  7. #7
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: How To Ask the User to Enter Another Number Using the Number 1

    Quote Originally Posted by Pettsa View Post
    Ok, I got the loop done but.... when I hit 1 the first time, it works well but when the program runs the second, I hit 1 to the second time and it exits the program. I don't know why.
    I don't think ity works well when you hit 1 the first time. The vowels counting is incorrect because you don't count them at all, you only print the number of vowels (count) of the first sentence. You should check again the logic of your program. It should be something like;
    do
       prompt the user to enter a sentence
       count vowels
       print #vowels
       ask if he wants to continue
    while (he wants to continue)

Similar Threads

  1. Trying to get Java to ask the user for a number
    By SunsetSkies in forum What's Wrong With My Code?
    Replies: 7
    Last Post: February 26th, 2012, 06:18 PM
  2. Have user enter in 2 inputs instead of using -1 to end. Help appreciated!
    By neontiger in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 21st, 2012, 04:26 AM
  3. How to display the results + repeat to the number entered by user?
    By TheBattousaixx in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 17th, 2011, 11:56 PM
  4. How to returned random number to original number?
    By i4ba1 in forum Algorithms & Recursion
    Replies: 2
    Last Post: March 19th, 2011, 04:35 AM
  5. How to make user press enter to continue in program?
    By BC2210 in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: October 3rd, 2009, 05:08 AM