Hello all.

I have am currently working on a assignemtn and i am having trouble with calculating the prime number, which the user can enter. I am also having trouble with returing to the begining of the code if the user does not select one of the options available to them.

I am new to Java and wouldlove your help.

I have attached the code completed so far, any assistance you can provide would be great.

import java.io.*;
import java.util.*;
import java.lang.*;

public class Assignment_1
{
    public static void main(String[] args) 
    throws IOException 
    {
        Scanner selection = new Scanner(System.in);
        BufferedReader stdin = new BufferedReader
        (new InputStreamReader (System.in));
            
        // This section declears the variables used in the program
        String menuselection, voweltext;
      
      
       // This section the the menu section that allows you to select your options
      System.out.println("*****************Assignment 1 Menu*****************");
      System.out.println("A. Check to see if a number is Prime.");
      System.out.println("B. Count the number of Vowels in a line.");
      System.out.println("X. Exit the program.");
System.out.println("***************************************************");
      
        System.out.print("Please select one of the above options:  ");
        menuselection = selection.next();
      
           //This section calculates if the number you entered is a prime number
            if (menuselection.equalsIgnoreCase("A"))
            {
               //System.out.println("A selected");
               System.out.println("Enter number:");
               int num = Integer.parseInt(bf.readLine());
               System.out.println("Prime number: ");
               for (i=1; i < num; i++ )
               {
                   int prime;
                   for (prime=2; prime<i; prime++)
                   {
                      int n = i%prime;
                      if (n==0)
                      {
                          break;
                      }
                  }
                  if(i == prime)
                  {
                      System.out.print("  "+i);
                  }
            }
    
            //This section calculates the amount of vowles within a text string.
            else if (menuselection.equalsIgnoreCase("B"))
            {
                System.out.println("Please enter a line of text: ");
                voweltext = selection.next();
                int vowelCount = 0;
                for (int i = 0; i < voweltext.length(); i++)
                  {
                          char currentChar = voweltext.charAt(i);
                          if ((currentChar == 'A') || (currentChar == 'a')
                             ||(currentChar == 'E') || (currentChar == 'e')
                             ||(currentChar == 'I') || (currentChar == 'i')
                             ||(currentChar == 'O') || (currentChar == 'o')
                             ||(currentChar == 'U') || (currentChar == 'u'))
                             vowelCount++;
                       }
                       System.out.println("");
                       System.out.println("Number of vowels found in the line is " + vowelCount);
           }

            //This section exits the program
            else if (menuselection.equalsIgnoreCase("X"))
            {
               System.out.println("Existing program goodbye...");
               System.exit(0);
            }    
            else if (menuselection != ("X"))
            {
                System.out.println("Error! invalid Selection");
                
            }

       }
}

Code tags and formatting will make your code easier to read:

[code=JAVA] // paste your code here

[/code]
Without using code tags, if you had any formatting to your code, it will be lost when you post it here.

As far as returning to the top if the user doesn't pick a valid option, how about a while loop?

boolean goodDataEntered = false;
while (!goodDataEntered)
{
     // display menu.
     // prompt for user choice.
     // if user choice is on the menu, change
     //      goodDataEntered to true.
     // else display error message.
}

As far as the prime number test goes, type "prime number" into Daniweb's search engine and a whole bunch of threads come up. Numerous threads apply to your situation, I'm sure. Since you're asking for algorithm as well as coding advice, don't necessarily confine yourself to the Java forum since the algorithms will be the same in other languages like C/C++, even though the coding will likely be at least a little different (but often not much).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.