Possibly using correct grammar??

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jul 2009
Posts: 3
Reputation: CTUBren is an unknown quantity at this point 
Solved Threads: 0
CTUBren CTUBren is offline Offline
Newbie Poster

Possibly using correct grammar??

 
0
  #1
Jul 9th, 2009
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


}
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 28
Reputation: PopeJareth is an unknown quantity at this point 
Solved Threads: 3
PopeJareth PopeJareth is offline Offline
Light Poster

Re: Possibly using correct grammar??

 
0
  #2
Jul 9th, 2009
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 =)

  1.  
  2. /**
  3. *
  4. * @author Bren
  5. */
  6.  
  7.  
  8. /**
  9. * @param args the command line arguments
  10. */
  11.  
  12. import java.util.Scanner; // Scanner class used for getting user input
  13.  
  14.  
  15.  
  16. public class AverageNumbers{
  17.  
  18. // The main method that begins execution of Java application
  19.  
  20. public static void main(String args[])
  21.  
  22. {
  23.  
  24. // variable declarations
  25.  
  26. int number;
  27.  
  28. int total = 0;
  29.  
  30. int count = 0;
  31.  
  32. double average;
  33.  
  34.  
  35.  
  36. // create Scanner to capture input from console
  37.  
  38. Scanner input = new Scanner(System.in);
  39.  
  40.  
  41.  
  42. // get initial user input
  43.  
  44. System.out.print("Enter a number (-1 to quit): ");
  45.  
  46. number = input.nextInt();
  47.  
  48.  
  49.  
  50. // calculate total sum and get user input until number = -1
  51.  
  52. while (number != -1)
  53.  
  54. {
  55.  
  56. count++;
  57.  
  58. total += number;
  59.  
  60.  
  61.  
  62. System.out.print("Enter a number (-1 to quit): ");
  63.  
  64. number = input.nextInt();
  65.  
  66. }
  67.  
  68.  
  69.  
  70. // Calculate the average
  71.  
  72. average = (double)total / count;
  73.  
  74.  
  75.  
  76. // Display average System.out.printf("The average is %.2f"n", average");
  77. System.out.println("The average is "+average);
  78. } // end method main
  79.  
  80. } // end class AverageNumbers
Last edited by PopeJareth; Jul 9th, 2009 at 2:20 am.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC