0

I'm sure this is going to be really easy to figure out and might even be something like trying to use correct grammar (this has happened to me a few times), but I've stared at this code for hours upon hours and don't know why NetBeans says it's wrong.
There is an illegal start of type in my public class Main { and
identifier> expected on my import java.util.Scanner;

I had more issues but somehow fixed them while typing this.
Yes this is homework, my teacher came from Russia and I can't understand a word he says!!! ~Great way to spend $1,000~

This program will get an unknown number of numbers from the user and determine their average. The user will quit by entering -1 if I can get it right.


package javaapplication17;

import java.util.Scanner;

/**
*
* @author Bren
*/
public class Main {

/**
* @param args the command line arguments
*/

import java.util.Scanner; // Scanner class used for getting user input

public class AverageNumbers

{

// The main method that begins execution of Java application

public void main(String args[])

{

// variable declarations

int number;

int total = 0;

int count = 0;

double average;

// create Scanner to capture input from console

Scanner input = new Scanner(System.in);

// get initial user input

System.out.print("Enter a number (-1 to quit): ");

number = input.nextInt();

// calculate total sum and get user input until number = -1

while (number != -1)

{

count++;

total += number;

System.out.print("Enter a number (-1 to quit): ");

number = input.nextInt();

}

// Calculate the average

average = (double)total / count;

// Display average System.out.printf("The average is %.2f"n", average");

} // end method main

} // end class AverageNumbers


}

2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by PopeJareth
0

your java file should be named the same as your class (in this case AverageNumbers.java should contain class AverageNumbers)

Also your main method should always be "public static void.main(String [] args) { "
Without the "static" you will have compilation errors.

This should get you running =)

/**
*
* @author Bren
*/


/**
* @param args the command line arguments
*/

import java.util.Scanner; // Scanner class used for getting user input



public class AverageNumbers{

// The main method that begins execution of Java application

public static void main(String args[])

{

// variable declarations

int number; 

int total = 0;

int count = 0;

double average;



// create Scanner to capture input from console

Scanner input = new Scanner(System.in);



// get initial user input

System.out.print("Enter a number (-1 to quit): ");

number = input.nextInt();



// calculate total sum and get user input until number = -1

while (number != -1)

{

count++;

total += number;



System.out.print("Enter a number (-1 to quit): ");

number = input.nextInt();

}



// Calculate the average

average = (double)total / count;



// Display average System.out.printf("The average is %.2f"n", average");
System.out.println("The average is "+average);
} // end method main

} // end class AverageNumbers
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.