-1

I have to create a method to find the biggest number via an array.

I have tried this:

import java.util.*;

class Main {
  public static void main(String[] args) {
    Scanner enter = new Scanner (System.in);

    int[] tab = {10,4,23,45,28,34,89,9,16,55};
    int choice = 0;


    do{
      System.out.println("*********Menu*********");
      System.out.println("1) - The biggest number : ");
      System.out.println("9) - Exit :");

      System.out.print("Enter your choice please : ");
      choice = enter.nextInt();

      switch(choice){

        case 1: 
          System.out.println("Option 1 :");
          biggest_number(big);
        break;

      }


    } while(choice != 9);

  }

    public static int biggest_number(int big){
      for(int i=0;i<tab.length;i++){
        if(tab[i] > big){
          big = tab[i];
        }
      }
      return big; 
      System.out.print("The biggest number is  => " + big);
  }


}

I have several error messages:

Main.java:23: error: cannot find symbol
          biggest_number(big);
                         ^
  symbol:   variable big
  location: class Main
Main.java:34: error: cannot find symbol
      for(int i=0;i<tab.length;i++){
                    ^
  symbol:   variable tab
  location: class Main
Main.java:35: error: cannot find symbol
        if(tab[i] > big){
           ^
  symbol:   variable tab
  location: class Main
Main.java:36: error: cannot find symbol
          big = tab[i];
                ^

I don't understand my errors? I have declared a parameter which is called big.

Is it return is correct also according you?

For information: I am obliged to use a method for my learning in Java.

user11124425
  • 961
  • 1
  • 11
  • 22
  • When you call `biggest_number` in `main` you need to pass in a value to go into the `big` argument, not just put the word `big`. Alternatively, remove `big` as a parameter from `biggest_number`. That seems to be something you want to get out of the method, not pass in. – khelwood Nov 23 '19 at 19:50

3 Answers3

1

You need to correct/change the following things in your program to make it work as you are expecting:

  1. Pass the array itself to the method, biggest_number(...) because the scope of the array, tab is local to public static void main(String[] args) i.e. it won't be visible to the method, biggest_number(...)
  2. Remove System.out.print("The biggest number is => " + big); after the return big; statement as it be unreachable. The program control exits the method/function after the return statement; therefore, any code after the return statement will be treated unreachable/dead which will fail compilation.

Given below is the correct program incorporating the points mentioned above:

import java.util.Scanner;

class Main {
    public static void main(String[] args) {
        Scanner enter = new Scanner(System.in);

        int[] tab = { 10, 4, 23, 45, 28, 34, 89, 9, 16, 55 };
        int choice = 0;

        do {
            System.out.println();
            System.out.println("*********Menu*********");
            System.out.println("1) - The biggest number : ");
            System.out.println("9) - Exit :");

            System.out.print("Enter your choice please : ");
            choice = enter.nextInt();

            switch (choice) {

            case 1:
                System.out.println("Option 1 :");
                System.out.print("The biggest number is  => " + biggest_number(tab));
                break;
            }

        } while (choice != 9);
    }

    public static int biggest_number(int[] tab) {
        int big=tab[0];
        for (int i = 0; i < tab.length; i++) {
            if (tab[i] > big) {
                big = tab[i];
            }
        }
        return big;
    }
}

Output:

*********Menu*********
1) - The biggest number : 
9) - Exit :
Enter your choice please : 1
Option 1 :
The biggest number is  => 89
*********Menu*********
1) - The biggest number : 
9) - Exit :
Enter your choice please : 
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
1

The method biggest_number needs only the array as input:

static int biggest_number( int[] arr) {
   if ( arr.length > 0 ) [
     int big = arr[0];
     for ( int i=1; i < arr.length; i++ ) {
        if ( arr[i] > big ) {
           big = arr[i];
        }
      }
      return big;
   }
   return 0; // or throw an exception
}
FredK
  • 4,094
  • 1
  • 9
  • 11
0

As the error messages says:

  • You have are using the parameter big the wrong way, remove it.
  • you can't access the array tab from public static int biggest_number. either pass it as a parameter, or declare it as a static field.
Mohamed
  • 131
  • 1
  • 5