Hey! I am new to java programing and need some help with some homework, here are the requirements:
Right now I am working on just setting up my arrays but it is not working. I read the size of the array from the user and then tell the user to enter the elements of the array. I am very confused and frustrated!! Please help if you can, thanks for all the advice in advance.

The assignment
Write a program that will provide methods for manipulating arrays.
Write static methods within your main program class to do the following.
Assume A, B are arrays of int.

ReadArray (A) -          reads int values from std input into the array A.
PrintArray (A) -         printlns the elements of A to std output 8 per line.
int Sum (A) -            returns the sum of the elements in array A.
int[] AddArrays (A, B) - returns a new array where each element is the sum
                           of the corresponding elements in A and B.
int DotProduct (A, B) -  returns the dot product a1*b1 + a2*b2 + ...

The methods should handle arrays of any length. AddArray and DotProduct
should handle two arrays of different length by treating the shorter array as
if it were padded with zeros out to the length of the longer array.

The main method should prompt the user to enter two arrays and their values
(prompt for number of elements, create array, call Readarray). The program
should then print each array and its sum, add the two arrays, print the sum
of this new array, and print the dot product of the two original arrays.

import java.util.Scanner;
import java.io.*;
public class AKL
{ 
    //---------------------------------------------------------------------------
    //  Method main                                            
    //
    //---------------------------------------------------------------------------
    public static void main(String[] args) throws IOException
    {
 int num =0;
    int[] myInts1;
    int[] myInts2;

    Scanner scan = new Scanner(System.in);

    System.out.println("Please enter the size of ARRAY 1 : ");
    num = scan.nextInt();
    myInts1 = new int[num];

    System.out.println("Please enter the size of ARRAY 2 : ");
    int num1 = scan.nextInt();
    myInts2 = new int[num1];

    readarray(myInts1);

    } // ends main

    public static void readarray(int []array)
    {
    Scanner scan = new Scanner(System.in);
    for (int index = 0; index < array.length; index ++)
     {
     System.out.println("Please enter the elements for ARRAY1 : ");
     array[index] = scan.nextInt();
  }
 }     
}//end class

What error are you getting? Or is it just a logic error somewhere? Nothing really stands out, but if you can describe how it isn't working I might be able to find something...

Also, can I suggest putting a few extra println statements in while you are trying to debug your program. Try something like

num = scan.nextInt();
System.out.println("### num = "+num+(" ###");

This will confirm whether you are reading the correct value. Put this sort of statement after every input and don't forget to display your output so you can see the result. This will help to decifer what is happening in your program. Just don't forget to comment them all out before handing in your final assignment!

Cheers,

darkagn

well after the user enters the elements of that they want in the array(intergers 1-100) The prompt comes several times and then an error message comes up saying that it is outbounds---

Are you sure you have compiled it after recent changes? You program worked just fine for me. I was able to enter the number of elements that I had set ARRAY1 to hold with no errors.

yeah but it doesn't print out the array when i tried to trouble shoot it...and when i tried to add the two arrays it also did not work

Well, the code you posted does not have anything written to do either of those things. If you are having trouble with an updated version of it, you'll need to post that with your question. We can't discuss what we can't see very well.

sorry i thought I had given you the correct one but this is the one I am working on---

import java.util.Scanner;
import java.io.*;

public class AKL
{



    //---------------------------------------------------------------------------
    //  Method main                                                Class NKOp8
    //
    //---------------------------------------------------------------------------
    public static void main(String[] args) throws IOException
    {

    int num =0;
    int[] myInts1;
    int[] myInts2;


    Scanner scan = new Scanner(System.in);


    System.out.println("Please enter the size of ARRAY 1 : ");
    num = scan.nextInt();
    myInts1 = new int[num];

    System.out.println("Please enter the size of ARRAY 2 : ");
    int num1 = scan.nextInt();
    myInts2 = new int[num1];

    ReadArray(myInts1);

    } // ends main

    public static void ReadArray(int []array)
    //-------------------------------------------------------------------------
    //  Read Array Method
    //
    //-------------------------------------------------------------------------
    {

    Scanner scan = new Scanner(System.in);
    System.out.println("Please enter the elements for ARRAY1 : ");

    for (int index = 0; index < myInts1.length; index ++)
        {
        myInts1[index] = scan.nextInt();
        }


    for (int index = 0; index < myInts1.length; index ++)
        {
        myInts2[index] = scan.nextInt();
        }



    }//end of ReadArray

}//end class

public static void ReadArray(int []array)
{
Scanner scan = new Scanner(System.in);
for (int index = 0; index < array.length; index ++)
{
System.out.println("Please enter element" + (index+1) + ": ");
array[index] = scan.nextInt();
}
}//end class

Try something like this.....cheers

Hi alexasmith,

The problem you were having has to do with scope. Basically you declare your variable myInts1 in your main method and pass it as a parameter to your readArray method (this is all correct). However, inside your readArray method, the name myInts1 is not defined - it is outside the scope of this method. Inside your readArray method it is called array (which is the name it is given as the parameter). In order to perform calculations on myInts2, you need a second call to your readArray method passing myInts2 to it. This is also called array inside the readArray method - basically the readArray method can perform calculations on one array at a time (the array that gets passed in when the method is called).

I hope this has explained your problem further.

Enjoy!

darkagn

Thank you all for your advice---I think I have solved the problem and the assignment --- I did need to set my variables, including my two arrays as global variables....Thank you again:)

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.