Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Adding elements in two arrays

  1. #1
    Junior Member
    Join Date
    Jun 2021
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Adding elements in two arrays

    Hello ive been working on this assignment for a collage project, the program is suppossed to function like a calculator, add, subtract, multiply, divide, and do a random number generator as well. Im trying to have the program run with arrays and the main problem im having is trying to write for loops to add user inputs for example have the variables in the for loops act as the numbers that are added or subtracted.

     
    package array_calculator;
     
    import java.util.Scanner;
     
    import java.util.Arrays;
     
    //Setting up constant variables for use in calc_menu.
    public class ArrayCalculator {
    	public static final String String = null;
    	public static final double[] operand1 = new double[3];
    	public static final double[] operand2 = new double[3];
    	public static final double lowerLimit = 0;
    	public static final double upperLimit = 0;
    	public static final int size = 0;
    	public static void main(String[] args) {
     
    		/*Calls up get operand and get menu option methods, and to let user
    		 * choose from several options within the menu in 
    		 * getOperand method.*/
     
    		System.out.println("Hello welcome to the array calculator! Enter a number to choose option ");
    		String prompt = null;
    		getOperand(prompt);
    		getMenuOption();
    	}
     
    	public static int getMenuOption() {
     
    		//This method lets user choose the options in the getOperad method or calculator menu.
    		//This method runs through several if, else if, and else statements.
    		//The if, else if statement loops inside a do while loop. 
    		// The do while loop determines what method to call based on users choice, or if user chooses 6 or any key it exits the menu.
     
    		Scanner input = new Scanner(System.in);
     
    		int calc_menu = input.nextInt();
    		do {
    			if(calc_menu == 1) {
    				add(operand1, operand2);
    				break;
    			}
    			else if(calc_menu == 2) {
    				subtract(operand1, operand2);
    				break;
    			}
    			else if(calc_menu == 3) {
    				multiply(operand1, operand2);
    				break;
    			}
    			else if(calc_menu == 4) {
    				divide(operand1, operand2);
    				break;
    			}
    			else if(calc_menu == 5) {
    				random(lowerLimit, upperLimit, size);
    				break;
    			}else {
    				break;
    			}
    		}while(calc_menu >= 1 && calc_menu <= 5);
     
    		return calc_menu;
    	}
     
    	public static double[] getOperand(String prompt, int size) {
     
    	}
     
    	public static String getOperand(String prompt) {
     
    		// This method when called in main method simply displays the calculator menu for the user.
     
    		System.out.println("Array Calculator Menu ");
    		System.out.println("<----------------------->");
    		System.out.println("1: Add ");
    		System.out.println("2: Subtract ");
    		System.out.println("3: Multiply ");
    		System.out.println("4: Divide ");
    		System.out.println("5: Random ");
    		System.out.println("6: Or Any Key To Exit ");
    		System.out.println("<----------------------->");
     
    		return String;
    	}
     
    	public static double add(double[] operand1, double[] operand2) {
     
    		// This method is called from the getMenuOption method, and adds values from two arrays.
     
    		Scanner input = new Scanner(System.in);
    		System.out.println("You have chosen to add, enter a number total of three numbers per array ");
     
    		for(int m = 0; m < operand1.length; m++) {
    			operand1[m] = input.nextDouble();
    		}
    		for(int n = 0; n < operand2.length; n++) {
    			operand2[n] = input.nextDouble();
    		}
    		double aDD = operand1[m] + operand2[n]; 
    		System.out.println("Your totals are " + aDD);
    		return aDD;
    	}

    Here's what i have so far the part of the code im struggling with is in lines 86 - 102, any help would me much appriciated!

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Adding elements in two arrays

    Please copy the full text of the error messages and paste it here.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. adding elements on two mutual ArrayList's
    By Stormbringer in forum Collections and Generics
    Replies: 20
    Last Post: October 17th, 2013, 04:24 AM
  2. LinkedList : Adding elements in constructor
    By Anusha870 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 8th, 2012, 12:45 PM
  3. java: adding array elements
    By dambundos in forum Collections and Generics
    Replies: 3
    Last Post: November 4th, 2011, 06:30 AM
  4. Help with ArrayList ( Adding certain elements together)
    By williamsant in forum Collections and Generics
    Replies: 13
    Last Post: September 27th, 2011, 09:32 AM
  5. Adding array elements inside a loop
    By Scotty in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 28th, 2010, 09:48 PM

Tags for this Thread