简体   繁体   中英

Using method in System.out wont work?

I am tasked with several objectives in my assignment, I am to read a file, which i believe I did correctly, and from that file of integers, put it into an array. It wont let me compile the code, it comes up with an error at smallest. So, how do I printout the method of the min?

public class jlrogers2 {

public static void reader(int[] arr) throws FileNotFoundException { 

 Scanner scanner = new Scanner(new FileReader("numbers.txt"));
    int i = 0;

    while(scanner.hasNextInt())
    {
        arr[i++] = scanner.nextInt();
    }


}

public static int minnimum(int[] arr){

int smallest =arr[0];

for (int i = 1; i>arr.length; i++){
        if (arr[i] > smallest)
        {
            smallest= arr[i];
            System.out.println(smallest);
        }
    }
    return smallest;
}
 public static void main(String [] args) throws FileNotFoundException   
{  


    Scanner in = new Scanner (System.in);
    System.out.println("Enter 1 for max index value.\nEnter 2 for min index   value.\nEnter 3 to search for an index value.\n"
            + "Enter 4 for display all index's\nEnter 5 for numbers in a range.\nEnter 6 to exit menu. ");

    int number = in.nextInt();

    if(number==6){
        System.out.println("Thank you for being awesome");
    }

    if (number==5){
    System.out.println(minnimum(smallest))  // here is my issue }


}  

}

Change for (int i = 1; i>arr.length; i++) to for (int i = 0; i<arr.length; i++) And also your logic to find smallest is wrong. Actually you are finding the largest.

To find smallest put it as

if(arr[i] < smallest ) inside for loop

Change the main as

 public static void main(String [] args) throws FileNotFoundException   
{  

 Scanner in = new Scanner (System.in);
 System.out.println("Enter 1 for max index value.\nEnter 2 for min index   value.\nEnter 3 to search for an index value.\n"
        + "Enter 4 for display all index's\nEnter 5 for numbers in a range.\nEnter 6 to exit menu. ");

 int number = in.nextInt();
 int arr[] = new int [200];//change this according to the requirement
 if(number==6){
    System.out.println("Thank you for being awesome");
}

if (number==5){
  reader(arr); 
 System.out.println(minnimum(arr)); 
 }
}  

There seem to be many problems with your code:-

->First as others have pointed change for (int i = 1; i>arr.length; i++) to for (int i = 1; i<arr.length; i++)

-> variable int smallest used, is not a class level variable and also is never locally declared in main()

-> method minimum() is never called from main, also method reader() seems to be not called

-> close the scanners after use, call scanner.close() at method end

Make some changes hence:-

-> main() would now look like:-

public static void main(String [] args) throws FileNotFoundException   
{  
    int[] arr=new int[100];
    reader(arr);//call this to populate arr[]
    Scanner in = new Scanner (System.in);
    System.out.println("Enter 1 for max index value.\nEnter 2 for min index   value.\nEnter 3 to search for an index value.\n"
            + "Enter 4 for display all index's\nEnter 5 for numbers in a range.\nEnter 6 to exit menu. ");

    int number = in.nextInt();

    if(number==6){
        System.out.println("Thank you for being awesome");
    }

    if (number==5){
        System.out.println(minnimum(arr));
    }

   in.close();
}  

To find smallest element you need to change your loop like this:

for (int i = 1; i < arr.length; i++) 
{
      if (arr[i] < smallest)
      {
        smallest = arr[i];
      }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM