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]);
}
}
}