(// See my program below-after the question)

The program is supposed to do the following:

Repeatedly offer the user the following 10 menu options (they must enter the option number):

(1) insert n values in the array, starting with position 1.
(2) insert n values immediately after the last entered value (as long as there is room in the array)
(3) print all the user-entered values in the array on one screen or in one pop-up window
(4) return the average (not sum) of the user-entered array values
(must not loop over locations not currently being used).
(5) return the minimum (not maximum) of the user-entered values.
(6) return the number of times a user-chosen value occurs in the array.
(7) change the value of one array element (chosen by index number)
(8) return the first position a given value occurs (-1 if it's not in the array)
(9) delete the value in a user-defined position (1 = index 1, 2 = index 2, etc.)
Compact the rest of the array (i.e. Move all the values after the deleted
down by one place towards position 1.)
(10) quit the program

Each of the options should be implemented as a method that is called from a switch statement in the main method. All input should be obtained via the getNumber() method. All output should be done via a printResults method (that has one String parameter, the message).

// Here is the program I have tried to write, but of course it's not running!!


import javax.swing.JOptionPane;
import java.io.*;

public class ArrayAssignment1 {
public static void main(String args[])
{
   final int MAX_VALUES = 11;
   final int QUIT = 10;       //option used to quit the menu and exit
   int  userChoice;           // holds option picked by user from menu
   int [] theArray = new int[MAX_VALUES]; //NOTE ARRAY SET UP BEFORE MENU LOOP
   int  theValue,
        index,
        returnValue, newValue;
   float returnFloatValue;

    // beginning of executable statements

   userChoice = showMenu();
   while (userChoice != QUIT)
   {
      switch (userChoice)
      {
        case 1:  index = 0;
           break;

 case 2:  for (int i= 0; i < 10; i++)
break;

         case 3:  System.out.println (theArray);
                    break;

      case 4:  if (theArray != null)
              System.out.println("Average of user-entered values is: " + returnFloatValue / 11 );
                break;

           case 5:  for (int i=0; i<10; i++)
            if (index < 10)

                    System.out.print("The min is: " + newValue);
                    break;

           case 6:  theValue = getNumber("For which array value are we searching?");
                    for ( int i=0; i < 10; i++ )
    if ( userChoice <= 10) // Value found.
                    System.out.println(theValue + "  occurred " + returnValue +
                                 "times in the array");
                    break;

           case 7:  index = getNumber(" At which index is the value being changed?");
            System.out.print(" What's the new value?");
    System.out.println(theValue + " index " + returnValue);
    break;

           case 8:  System.out.print("What number are we looking for?");
                    BufferedReader getNumber =
        new BufferedReader (new InputStreamReader (System.in));
                    System.out.print (theValue + " first occurs at position " + returnValue);
                    break;

           case 9:  theValue = getNumber("From which position are we deleting?");
                    for (int i=0; i<10; i++)
                   if (index == userChoice)

                    break;

                    for(int i=0; newValue < 10 - 1; index++)
                        userChoice += 1;
                    newValue--;
                    System.out.println (index + " ");

      }   // end of switch

      userChoice = showMenu();        // get next option from the user
   }  // end of while

   System.exit(0);
}

static int getNumber(String message)
{
   return Integer.parseInt(JOptionPane.showInputDialog(message));
}

static void printResults(String message)
{
   System.out.println(message);
}

static int showMenu()
{
    String message = "Here are your Choices: \n"
        + "Enter 1 to add Values into the Array \n"
        + "Enter 2 to add Values immidiately after the last \n"
        + "Enter 3 to show the values entered by the user \n"
        + "Enter 4 to calculate the avarage of the values entered\n"
        + "Enter 5 to the minimum value entered\n"
         + "Enter 6 to show the number of times you chose a choice\n"
         + "Enter 7 to modify a choice \n"
         + "Enter 8 to return the first position a value occurs\n"
         + "Enter 9 to delete a value in a position\n"
          + "Enter 10 to Quit \n\n";

    return getNumber(message);


}

} // end of class

Recommended Answers

All 5 Replies

What errors are you getting and what is your question? Please be specific and use code tags around code you post.

basically, his program runs but it does nothing. The only thing that shows up is the menu options.

basically, his program runs but it does nothing. The only thing that shows up is the menu options.

His program? I thought this was your program? And of course it doesn't do what it is supposed to: most of the switch cases have only fragments of code in them (which also goes against the assignment that each of those options should call a method to do their work.

You can't possibly expect someone to just write this to make it work for you, so ask your questions and state what you are having difficulty with.

Sorry, that was a typo..I meant "this" NOT "his"

I am not trying to have you write the whole program, I just needed examples/direction on how to use switch statement to have the program do what is asked in the question.

Thanks anyways.

import javax.swing.JOptionPane;
import java.io.*;

public class ArrayAssignment07 {
public static void main(String args[])
{
   final int MAX_VALUES = 11;
   final int QUIT = 10;       //option used to quit the menu and exit
   int  userChoice;           // holds option picked by user from menu
   int [] theArray = new int[MAX_VALUES]; //NOTE ARRAY SET UP BEFORE MENU LOOP
   int  theValue,
        index,
        averageArray,
        returnValue, newValue;
   float [] returnFloatValue;
    BufferedReader console =
                        new BufferedReader(
                        new InputStreamReader(System.in));

   userChoice = showMenu();

    while (userChoice != QUIT)
   {
      switch (userChoice)
      {
           case 1:  for (int i = 0; i<theArray.length; i++)

                    break;

           case 2:  // the array has 11 elements starting at zero
                    System.out.print("\nAdd values to the array ");

                    // create array and prompt user to enter array elements, then read them in
                    for (int i=0; i<theArray.length; i++)
                    break;

           case 3:  System.out.println("\n\nThe values you entered are:");
                    for (int i = 0; i<theArray.length; i++)
                    break;

           case 4:
//-----------------------------------------------------------------------
                    // average: finds the average of the array elements
                    float sum = 0;
                    for (int i = 0; i<theArray.length; i++){
                    sum += theArray[i];
                    }

                    System.out.println("The average of the usre-entered elements: " + theArray);
                    break;

           case 5:  for (int i=0; i<10; i++)
                    {// Get the minimum value from the array and return it
                        System.out.print("The min is: " + " ");
                    }
                    break;

           case 6:  theValue = Integer.parseInt(JOptionPane.showInputDialog("For which array value are we searching?"));
                    for ( int i=0; i < 10; i++ )
                        if ( userChoice <= 10) // Value found.
                    System.out.println(theValue + "  occurred " + "times in the array");
                    break;

           case 7:  index = Integer.parseInt(JOptionPane.showInputDialog("At which index is the value being changed?"));
                    for (int i = 0; i < MAX_VALUES; i++)
                    theArray[i] = i++;
                    System.out.println("What's the new value?");
                    break;

           case 8:  theValue = Integer.parseInt(JOptionPane.showInputDialog("What number are we looking for?"));
                    BufferedReader getNumber =
                    new BufferedReader (new InputStreamReader (System.in));
                    System.out.print (theValue + " first occurs at position:");
                    break;

           case 9:  theValue = Integer.parseInt(JOptionPane.showInputDialog("From which position are we deleting?"));
                    for (int i=0; i<10; i++)
                    userChoice += 1;
                    theValue--;
                    System.out.println (theArray + " ");
                    break;


      }   // end of switch

      userChoice = showMenu();        // get next option from the user
   }  // end of while

   System.exit(0);
}

static int getNumber(String message)
{
   return Integer.parseInt(JOptionPane.showInputDialog(message));
}


static void printResults(String message)
{
   System.out.println(message);
}

static int showMenu()
{
    String message = "Here are your Choices: \n"
                       + "Enter 1 to add Values into the Array \n"
                       + "Enter 2 to add Values immidiately after the last \n"
                       + "Enter 3 to show the values entered by the user \n"
                       + "Enter 4 to calculate the avarage of the values entered\n"
                       + "Enter 5 to the minimum value entered\n"
                       + "Enter 6 to show the number of times you chose a choice\n"
                       + "Enter 7 to modify a choice \n"
                       + "Enter 8 to return the first position a value occurs\n"
                       + "Enter 9 to delete a value in a position\n"
                       + "Enter 10 to Quit \n\n";

    return getNumber(message);


}

} // end of class

This program only shows the menu but it's not running. please offer advice.
It's supposed to prompt a user for array values, calculate min, average, etc. using the swtch statement. see first posting with question.

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.