1

I am new to programming. (VERY) I am trying to create a statistical calculator. I am not worried about the formulas and what not that I still need to enter. The comments are there because those are the things that I have tried.(Which haven't worked) It is a menu driven program. I select 'a' to go to the series menu, and the 'a' again to enter the numbers. I am trying to fix the code to accept an ArrayList of unknown values. That way a person can enter as many numbers as they want and the rest of the program will continue to run fine. I have it set to an array of 5 for now.

(1) Is there a way to fix the code to accept the ArrayList? using the comments you can see the things that I have tried. When I run it now it compiles and run fine, but when I use the ArrayList I get an error. I really need some help. I am stuck. I am working this portion before I go to the next cases. I would like it to just prompt the user to enter a list of numbers and then use that to perform the rest of the functions in the calculator as requested. Please help. This may be simple but its killing me.

(2) Also, When I run the code and get to where I can enter the first number it takes me all the way back to the main menu. I would like it to stay in the sub-menu/series menu until the user chooses 'q'.

import java.util.Scanner; // Scanner is in the java.util package
//import java.util.ArrayList;
//import java.util.List;
class FloatWrapper {
  public float value;
  public FloatWrapper(float param) {
    value = param;
  }
}

public class BasicCalculator {

  public static void main(String[] args) {
    int menuOption = 0;
    //List myList = new ArrayList();
    double[] myList = new double[5];
    //ArrayList<integer> myList = new ArrayList<>();
    int counter = 0;

    while (menuOption != 'q')
      do {
        menuOption = mainMenu();
        switch (menuOption) {
          case 'a':
            {
              switch (seriesmenu()) {
                case 'a':
                  myList[counter] = dataInput();
                  counter++;
                  break;

                case 'q':
                  mainMenu();
                  break;
                default:
                  seriesmenu();
              }
            }
            break;

            //case 'b' :
            //case 'c' :
            //case 'd' :
            //case 'e' :

          case 'f':
            display(myList);

            break;
            //case 'g' :
            //case 'h' :
            //case 'i' :
            //case 'j' :
          case 'q':
            System.exit(0);
            return;
        }
      }
      while (menuOption != 'q');
  }

  //Main Menu Display
  static char mainMenu() {
    Scanner input = new Scanner(System.in);

    System.out.println("\n\nBASIC CALCULATOR");
    System.out.println(" a. Insert Series");
    System.out.println(" b. Mean");
    System.out.println(" c. Median");
    System.out.println(" d. Mode");
    System.out.println(" e. Standard Deviation\n");
    System.out.println(" f. Show Series");
    System.out.println(" g. Show Elements Greater than Mean");
    System.out.println(" h. Show Elements Less than Mean");
    System.out.println(" i. Show Series in Order");
    System.out.println(" j. Show Full Report\n");
    System.out.println(" q. Quit");
    System.out.print("Insert option: ");

    return (input.next().charAt(0));
  }

  //Menu to insert values
  static char seriesmenu() {
    Scanner input = new Scanner(System.in);

    System.out.println("\n\nSeries Options");
    System.out.println(" a. Enter New Number");
    System.out.println(" q. Back to Calculator");
    System.out.print("Insert option: ");
    return (input.next().charAt(0));
  }

  //Reads input from keyboard (menu, values, etc)
  static double dataInput() {
    Scanner scanner = new Scanner(System.in);
    Scanner input = new Scanner(System.in);
    System.out.print(" Enter a Number: ");
    double i = input.nextInt();

    return i;
  }

  //displays array
  static void display(double[] args) {
    for (int i = 0; i < args.length; i++) {
      System.out.println(args[i]);
    }

  }

}

Here is the updated code that I have, but I am getting so many errors that I think that I want to just go back to the original and try again.

import java.util.Scanner; // Scanner is in the java.util package
import java.util.ArrayList;
import java.util.List;
class FloatWrapper{
  public float value;
  public FloatWrapper(float param){
     value=param;
  }
}

public class BasicCalculator { 
    
    ArrayList<Integer> myArrayList = new ArrayList<>();
    
    public static void main(String[] args) 
    {
        int menuOption = 0;
        //List myList = new ArrayList();
        double [] myList = new double [10];
        
        int counter =0;

     //while(menuOption != 'q')
                do{
               menuOption = mainMenu();
  switch(menuOption)
                {
                    case 'a':
                    {
                        do{
                        seriesOption = seriesmenu();
                        switch(seriesmenu())
                            
                            { 
                                case 'a' :
                                myList[counter] = dataInput();
                                    counter++;
                                break;
                                    
                                case 'q':
                                    mainMenu();
                                break;
                                default:
                                    seriesmenu();
                        } while (seriesOption != 'q');
                    }break;
                        
                    //case 'b' :
                    //case 'c' :
                    //case 'd' :
                    //case 'e' :
                        
                    case 'f' : display(myList);
                        
                    break;
                    //case 'g' :
                    //case 'h' :
                    //case 'i' :
                    //case 'j' :
                    case 'q': System.exit(0);
                        return;
                } 
    }
                while(menuOption != 'q');
    }
   
 //Main Menu Display
 static char mainMenu()
        {
            Scanner input=new Scanner(System.in);

    System.out.println("\n\nBASIC CALCULATOR");
           System.out.println(" a. Insert Series");
           System.out.println(" b. Mean");
           System.out.println(" c. Median");
           System.out.println(" d. Mode");
           System.out.println(" e. Standard Deviation\n");
           System.out.println(" f. Show Series");
           System.out.println(" g. Show Elements Greater than Mean");
           System.out.println(" h. Show Elements Less than Mean");
           System.out.println(" i. Show Series in Order");
           System.out.println(" j. Show Full Report\n");
           System.out.println(" q. Quit");
           System.out.print("Insert option: ");
           
           return(input.next().charAt(0)); 
        }
        
        //Menu to insert values
        static char seriesmenu()
        {
            Scanner input=new Scanner(System.in);

    System.out.println("\n\nSeries Options");
           System.out.println(" a. Enter New Number");
           System.out.println(" q. Back to Calculator");
           System.out.print("Insert option: ");
             return(input.next().charAt(0)); 
        }
        
        //Reads input from keyboard (menu, values, etc)
 static double dataInput()
        {
            Scanner scanner = new Scanner(System.in);
            Scanner input=new Scanner(System.in);
            System.out.print(" Enter a Number: ");
            //double i = input.nextInt();
            int i = input.nextInt();
            myArrayList.add( i );
            return i;
        }
        
        //displays array
        static void display(double [] args)
        {
            for (int i = 0; i < args.length ; i++)
            {
                System.out.println(args[i]);
            }
            
        }

}
D. Skaines
  • 21
  • 7
  • You would see an error if you uncommented out your `mylist` for a few different reasons - 1) `integer` is not a type in Java, you probably want `Integer` and 2) you can't declare 2 types of variables both with name `myList` in the scope of that method. – mkobit Nov 19 '14 at 04:30
  • `but when I use the ArrayList I get an error.` You should certainly specify exception details (error message, stack trace, line that throws an exception). – default locale Nov 19 '14 at 04:31
  • Check out [this question](http://stackoverflow.com/questions/14421943/java-arraylist-for-integers) – default locale Nov 19 '14 at 04:31

1 Answers1

0

Change

ArrayList<integer> myList = new ArrayList<>();

to

static ArrayList<Integer> myArrayList = new ArrayList<>();

And move it out of main into the class so you can use it in dataInput() like this:

int i = input.nextInt();
myArrayList.add( i );

There is certainly more to fix but that should get you over your ArrayList blues.


If you look closely at how mainMenu() is used in main you should see how to use seriesMenu() as well. seriesMenu() returns a value that you don't do anything with. It's not saved in a var like mainMenu() return is saved in menuOption. seriesMenu() needs a do while loop of it's own.

Also, the while do while construct you have going around the switch is pretty wacky. Don't think you need it to be any more complicated than a single do while for the mainMenu and a single do while for the seriesMenu().


You'll need to change the rest of your code so it works with ArrayList:

  //Reads input from keyboard (menu, values, etc)
  static void dataInput() {
    Scanner input = new Scanner(System.in); 
    System.out.print(" Enter a Number: ");
    int i = input.nextInt();
    myArrayList.add( i );    
  }

  //displays array
  static void display(ArrayList<?> args) {
    for (int i = 0; i < args.size(); i++) {
      System.out.println(args.get(i));
    }
  }

Note that dataInput() no longer returns anything so change myList[counter] = dataInput(); to just dataInput();


OK, I'll give you a big hint about the inner do while loop

Note how the pattern goes do-switch-while. You want to do the same pattern on the inner switch.

  do {
    menuOption = mainMenu();
    switch (menuOption) {
    ...
    }
  } while (menuOption != 'q');

Make the switch in case: a look the same:

          do {  
            seriesOption = seriesmenu(); 
            switch (seriesOption) {
            ...
            }
          } while (seriesOption != 'q');    
candied_orange
  • 7,036
  • 2
  • 28
  • 62
  • What do I do about it recycling me to the main menu when I have not declared 'q'.. I want to make it to where the user choses when to stop entering numbers and return to the main menu. – D. Skaines Nov 19 '14 at 04:44
  • also after I get to the "enter new number" i entered "1 2 3 4 5" and then when I displayed the series ('f') it only showed the first input of "1." – D. Skaines Nov 19 '14 at 04:46
  • To be honest I don't know what exactly you mean. I am very new to all of this. I can fix the while do while thing, but I'm confused about what you mean regarding the return being saved. I have the same return statement at the end of each menu. – D. Skaines Nov 19 '14 at 05:06
  • I changed the dataInput to reflect what you said. I am getting two errors on line 106. "myarrayList.add( i );" it is telling me to put a return statement for it... also, that a non-static variable cannot be referenced in a static context. – D. Skaines Nov 19 '14 at 05:15
  • Regarding 'return being saved', Note how `menuOption = mainMenu();` saves what was returned from `mainMenu()` in `menuOption`. Note how `seriesmenu()` never gets saved in any `seriesOption`. You'll need to do that for the inner `do while` that will keep the `seriesmenu()` from dropping you back to the main menu every time. – candied_orange Nov 19 '14 at 05:23
  • I am still running into errors. I don't know why. Ive tried everything you suggested. I will try a second pair of eyes. maybe that will help. It is still exiting the series menu before I want it to. Also it still won't let me input "1 2 3 4 5" and store them i have to enter them individually but would rather do it all at once. – D. Skaines Nov 19 '14 at 05:28
  • Like I said, you need another `do while` loop – candied_orange Nov 19 '14 at 05:30
  • I am not getting anywhere on this. I appreciate all the help, and it has been a great learning experience. However, no matter what I've done I have only been able to take 1 step forward, and then 1 step backward. I fix one thing and the other doesn't work. – D. Skaines Nov 19 '14 at 16:07