| | |
Possibly using correct grammar??
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jul 2009
Posts: 3
Reputation:
Solved Threads: 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
}
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
}
•
•
Join Date: Jun 2009
Posts: 28
Reputation:
Solved Threads: 3
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 =)
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 =)
java Syntax (Toggle Plain Text)
/** * * @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
Last edited by PopeJareth; Jul 9th, 2009 at 2:20 am.
![]() |
Similar Threads
- Definitive guide to asking for support here... (Motherboards, CPUs and RAM)
- Help with database (C#)
- How do you apply/use the OSI MODEL? (Networking Hardware Configuration)
- Problem installing new SATA hard drive (Storage)
- Windows XP disk check destroys ICS (Windows NT / 2000 / XP)
- help ASAP please (Viruses, Spyware and other Nasties)
- OS Reformat, a good idea? (Windows NT / 2000 / XP)
- cannot delete shortcut (Windows 95 / 98 / Me)
Other Threads in the Java Forum
- Previous Thread: ClockViewer - Cannot Draw Clock
- Next Thread: Cannot Find symbol- String/Vector Issue
| Thread Tools | Search this Thread |
2dgraphics account android api apple applet application array arrays automation banking binary binarytree bluetooth chat chatprogramusingobjects class classes client code component data database derby design draw eclipse encryption error event exception fractal game givemetehcodez graphics gui html ide if_statement image inheritance input integer interface j2me java javadesktopapplications javaprojects jlabel jni jpanel jtextfield julia linux list loop map method methods midlethttpconnection mobile monitoring netbeans newbie nullpointerexception open-source oracle print printing problem program programming project property recursion reference ria scanner screen search server set size sms sort sourcelabs splash sql static stop string swing testautomation threads time tree ui unicode validation windows





