0

My code will not compile and I can not figure out how to fix the error. It is a compile time run error I think. My error is in my method to print out array. Then, it says that there is something wrong with my closing bracket for the class. I am using a scanner class and I did import it, it is just not shown. Any help?

My code:

 public static void main (String[] args){
        
   int[] getArray; //reference to an int array of type and takes no parameters 
  
    System.out.println("How many numbers would you like to enter?" ); 
    int size = scan.nextInt(); 
    int[] nums; //reference to nums array of type int  
    nums = new int[size]; /* new array of type int. assigned to nums. 
    size, to specify number of elements array will have */
  
  for(int i=0; i<nums.length; i++){
    System.out.println("Enter a number." +(i+1)+ "left"); 
    nums[i] = scan.nextInt(); //storing user input into array
  
    return nums;
  
  }//closing for loop
  
    int[] minimumValue; 
    min = scan.nextInt();
    min = nums[0]; //assigning min value to first element in array
    
    return min; 
    
  for(int i=0; i<min.length; i++){
  
    if(i<min){
      min = nums[0];
    }
  }
   return min;
  
  public static void printArray (int[] getArray){ //method to print out array
    for(int i = 0; i < nums.length; i++){ 
    System.out.print(nums.length); //print out array
    }
  }
  
  public static void printArrayInReverse(int[] getArray){  //method for arrayInReverse
    for(int i = nums.length - 1; i >= 0; i--){ 
    System.out.print(nums[i]); 

    }
  }
  
  int[] numbers = getArray();// calling getArray method
  
  public static void main (String[] args){
    System.out.print("************");
    printArray(numbers); //calling printArray method and passing numbers through it

    printArrayInReverse(numbers);// calling printArrayInReverse method and passing numbers through it

    System.out.print(minimumValue(numbers)); /* calling minVal through print statement and passing 
                                              numbers through it*/
  } 
  
  }
  }
5
  • Is this a class? With two main methods? Commented Mar 4, 2021 at 5:44
  • In your first for-loop, you should not return num; inside the loop. That will end your main method abruptly. You shouldn't have any return statements anywhere since all your methods are void Commented Mar 4, 2021 at 5:50
  • @ Benjamin M it should be three methods. One to create a new array and the other two to print out the array and the arrayInReverse Commented Mar 4, 2021 at 6:02
  • @Erik when I remove all returns, it still shows that " public static void printArray" has an error that says its an illegal start of expression Commented Mar 4, 2021 at 6:03
  • It says that public static void printArray() is an illegal start of an expression because you write that method definition in the middle of the public static void main() method. In Java you cannot nest method declarations. Move the definitions of the printArray() and the printArrayInReverse() methods after the main() method. Commented Mar 4, 2021 at 6:32

2 Answers 2

1

It is very hard to tell what you are trying to accomplish here from your code. There is no class here which means your program will not even compile, please remember to post all applicable code to your question as it makes it easier for people to help you.

Firstly, you can only have one entry point (ie. main(String[] args) for each class. This is better explained here Can there exist two main methods in a Java program?.

Within this main method, you cannot have any other methods, you can only call other methods and perform operations ad such.

The variable "scan" cannot ever do anything if it is not instantiated prior to use. The variable getArray is being used as a method, which it is not.

Please take a look at Simple Java array program where it shows more in-depth how to use arrays.

Take a look at this as see if it even accomplishes what you want to do, which is still somewhat unclear. I shortened everything so that it is simpler, with a program this small multiple methods are not needed unless there is some logic to denote when to print the array or when to print the array reversed.

import java.util.Scanner;

public class CLASSNAME {
    
        
    static Scanner scan = new Scanner(System.in);
    
    static int[] nums;
        
    public static void main(String[] args){
        
        // Get Size Of Array
        System.out.println("How many numbers would you like to enter?" ); 
        int size = scan.nextInt(); 
        nums = new int[size];
            
         // Fill Array
         for(int i = 0; i < nums.length; i++){
            System.out.println("Enter a number." +(i+1)+ "left"); 
            nums[i] = scan.nextInt();
         }
            
         // Set 0th Number To
         System.out.println("Enter 0th Number");
         int min = scan.nextInt();
         nums[0] = min;
        
        // Print Array
        System.out.println("\n" + "Printing Array Index 0 -> ArraySize");
         for(int i = 0; i < nums.length; i++){ 
            System.out.print(nums.length);
        }
         
         // Print Array Reversed
         System.out.println("\n" + "Printing Array Index ArraySize -> 0");
        for(int i = nums.length - 1; i >= 0; i--){ 
            System.out.print(nums[i]);
        }
      }  
      
}
Sign up to request clarification or add additional context in comments.

Comments

0
  1. you need to create a Scanner object to use it
  2. you can't create more than one main method
  3. you must create methods outside the main method

Example:

import java.util.Scanner;

public class a {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        //code here
    }


    public static void method_name() {
        //code here
    }

    //If you want to return integer value for example
    public static int method_name() {
        //code here
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.