Hi im trying to make a program that passes an array to a method. the method then finds the smallest number in the array and passes that number back to the main where its printed out. I am getting an error saying: "error: number cannot be resolved to a variable". I am using drjava. here is my code.

import java.util.*;

public class homeWorkTwo{
  public static void main(String[] args)
  {
    int[] arrayA;
    arrayA = new int[10];
    arrayA[0]=7;
    arrayA[1]=5;
    arrayA[2]=8;
    arrayA[3]=9;
    arrayA[4]=2;
    arrayA[5]=10;
    arrayA[6]=11;
    arrayA[7]=1;
    find_sum(arrayA);
    //int lowestNumber = find_sum(number);
    //System.out.print("The smallest number is: "+lowestNumber);
  }
  public static int find_sum(int [ ] arrayA){
    int isItSmaller=0;
    int small=0;
    for(int i=0;i<=7;i++){
      isItSmaller=arrayA[i];
      if(small<isItSmaller){}
      else
        small=isItSmaller;
    }
   System.out.print(small);
   return small;
  }
}

Recommended Answers

All 2 Replies

It should also tell the line the error is occurring on.

Yes.i figured it out though on my own. I would like to thank you for taking thd time to reply to my thread. I greatly appreciate it. Ill make another post in a second with the corrected code incase anyone can use it in the future.

EDIT:Here is the corrected code. if anyone has questions concerning what I did to correct it, just PM me :)

/*This program will take an array and pass it to a method. the method with then take the array into a for loop
 * and determine the smallest element of that array. the method will then return that number to the main which
 * will then print it.*/
public class homeWorkTwo{
  public static void main(String[] args)
  {
    int[] arrayA; //declares an array of ints
    arrayA = new int[8]; //8 elements in the array
    arrayA[0]=7; //sets all the elements of the array to the test numbers given to us on the csi310 website
    arrayA[1]=5;
    arrayA[2]=8;
    arrayA[3]=9;
    arrayA[4]=2;
    arrayA[5]=10;
    arrayA[6]=11;
    arrayA[7]=1;
    find_small(arrayA); //calls the method
    int lowestNumber = find_small(arrayA); //sets int lowestNumber equal to the variable small returned from the method.
    System.out.print("The smallest number is: "+lowestNumber); //prints the lowest number of the array
  }
  public static int find_small(int [ ] arrayA){ //method to find the lowest int in array. it takes arrayA as an argument
    int isItSmaller=0;
    int small=100000; //makes sure that int small is larger than any element in the arrary so the if else statement will work
    for(int i=0;i<=7;i++){ //runs through all elements of the array one at a time
      isItSmaller=arrayA[i];
      if(small<isItSmaller){} //checks to see if the current number is lower than the lowest one so far
      else
        small=isItSmaller; //keeps the lowest number updated
    }
   return small; //returns the lowest number
  }
}
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.