I made this code which has got two separate classes
this code is a calculator program which i am practicing on
it takes user input and then converts them into arrays
I think the user input is out of range when we are supposed to enter either 1,2,3,4 for selecting the calculation type
take your time to explain why is it out of range, which part of the program is causing this and how do i solve it
sorry if it's getting annoying or stupid, I am new and learning all by myself
here is the main class
and here is the secondary class i made to store methodsimport java.util.*; public class main { public static void main(String args[]) { //creation of objects Scanner input = new Scanner(System.in); Scanner input1 = new Scanner(System.in);//this takes input List <Double> quantity = new ArrayList<Double>(); //list cant work without array object to convert into array formula formula = new formula(); //creating object of class "formula" to acess its methods aka formulas //creation of variables int digit; //taking of user input(numbers) System.out.println("enter your numbers"); while (input.hasNextDouble()) { //reads as while input has quantity.add(input.nextDouble()); //quantity will store all input in list and now we need to cinvert it to aray } Double []quantityarray = new Double[quantity.size()]; /*size of array is fixed so we dceclare. array no. needs to we need to wriet the full name of the variable type. the size of array after user input The size of this array = size of arraylist(quantity)*/ quantity.toArray(quantityarray); /* now we transfered all the values inside arraylist to array "quantityarray*/ for (Double x:quantityarray) { System.out.print(x+", "); } int lenght = quantityarray.length; /*we took the legth of the array to check if its 2 number or 1 or more*/ if (lenght<=2) { if (lenght==1) { System.out.println("since you entered only one number, we only support power"); System.out.println("enter the power of your number"); digit = input.nextInt(); System.out.println("the answer is"+formula.power(quantityarray[0], digit)); } System.out.println("press 1 for addition, 2 for subtraction, 3 for multiplication, 4 for division"); } input.close(); digit = input1.nextInt(); /*now we create a switch and if digit = switch case no. it will execute that case*/ switch (digit) { case 1 : formula.addition(quantityarray); case 2 : formula.subtraction(quantityarray[0],quantityarray[1] ); case 3 : formula.multiplication(quantityarray); case 4 : formula.division(quantityarray); } if (lenght<2) { System.out.println("press 1 for addition, 2 for multipication"); digit = input1.nextInt(); switch (digit) { case 1 : formula.addition(quantityarray); case 2 : formula.multiplication(quantityarray); } } input1.close(); } }
public class formula { public double power (double a, double b) { double power = Math.pow(a, b); return power ; } public double subtraction(double a, double b) { double result = a - b; return result; } public double multiplication(Double... d) /*"..." is needed cuz array has a lot of numbers inside it not one*/ { double sum = 1; for (double x:d) /*using enhanced for loop instead of regular loop*/ sum = sum * x; return sum; } public double addition(Double... d) //capital Double when using arrrays with unknown no. of arguments { double sum=0; for (double i: d) { sum = sum + i; } return sum; } public double division(Double...d) { double sum = 1; for (double i:d) /* "int i" wont work as "d" is also double*/ { sum = i/sum; } return sum; } }
--- Update ---
Update :- I think Ifound that it is because when it" asks for number " test if it's wrong and correct me. I am at my my office so I have no way to test it right now
I entered numbers which aren't doubleSystem.out.println("enter your numbers"); while (input.hasNextDouble()) { //reads as while input has quantity.add(input.nextDouble());
Eg :-
1
5
3
6
Which doesn't have any decimal points.. But in calculator we never really know when user's input will be integer or double.
Is there any way to solve this
For example my numbers are
32 and 21.3
The 32 will cause the mismatch I guess
Since it's integer. Is there anyway for the integer to be converted to 32.0 automatically by any pre made method?
--- Update ---
Update :- I think Ifound that it is because when it" asks for number " test if it's wrong and correct me. I am at my my office so I have no way to test it right now
I entered numbers which aren't doubleSystem.out.println("enter your numbers"); while (input.hasNextDouble()) { //reads as while input has quantity.add(input.nextDouble());
Eg :-
1
5
3
6
Which doesn't have any decimal points.. But in calculator we never really know when user's input will be integer or double.
Is there any way to solve this
For example my numbers are
32 and 21.3
The 32 will cause the mismatch I guess
Since it's integer. Is there anyway for the integer to be converted to 32.0 automatically by any pre made method?