d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Can Someone Help With My Errors?
Add Reply New Topic New Poll
Member
Posts: 24,488
Joined: Aug 5 2008
Gold: 38.00
Dec 19 2012 01:47am


import java.util.Scanner; //util/scannerclass
import java.io.*; //needed for files

public class Lab4


{
public static void main(String[]args) throws FileNotFoundException
{










int calorieaverage=0,
monthlyaverage;







Scanner keyboard = new Scanner(System.in); //input new scanner




int[] arynums = { 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 4000, 2000,};

String[] aryMonth = { "Jan", "Feb", "Mar", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",};


for(int x = 0; x < aryMonth.length; x++)
{

System.out.println("" + arymonth[x] + " " + arynums[x]);
}


}
for(int x = 0; x < arynums; x++){
calorieaverage = (calorieaverage + arynums[x]);
}

monthlyaverage= calorieaverage/12;

System.out.println("The total for the year is >", calorieaverage);
System.out.println("The average monthly total is >", monthlyaverage);

}

}




Lab4.java:55: error: illegal start of type
for(int x = 0; x < arynums; x++){
^
Lab4.java:55: error: ')' expected
for(int x = 0; x < arynums; x++){
^
Lab4.java:55: error: illegal start of type
for(int x = 0; x < arynums; x++){
^
Lab4.java:55: error: <identifier> expected
for(int x = 0; x < arynums; x++){
^
Lab4.java:55: error: ';' expected
for(int x = 0; x < arynums; x++){
^
Lab4.java:55: error: > expected
for(int x = 0; x < arynums; x++){
^
Lab4.java:55: error: '(' expected
for(int x = 0; x < arynums; x++){
^
Lab4.java:59: error: <identifier> expected
monthlyaverage= calorieaverage/12;
^
Lab4.java:61: error: <identifier> expected
System.out.println("The total for the year is >", calorieaverage);
^
Lab4.java:61: error: illegal start of type
System.out.println("The total for the year is >", calorieaverage);
^
Lab4.java:61: error: <identifier> expected
System.out.println("The total for the year is >", calorieaverage);
^
Lab4.java:62: error: <identifier> expected
System.out.println("The average monthly total is >", monthlyaverage);
^
Lab4.java:62: error: illegal start of type
System.out.println("The average monthly total is >", monthlyaverage);
^
Lab4.java:62: error: <identifier> expected
System.out.println("The average monthly total is >", monthlyaverage);
^
Lab4.java:66: error: class, interface, or enum expected
}
^
15 errors

This post was edited by Archer on Dec 19 2012 01:47am
Member
Posts: 1
Joined: Dec 19 2012
Gold: 0.00
Dec 19 2012 02:09am
1) You have an extra curly brace here:
}
for(int x = 0; x < arynums; x++){
calorieaverage = (calorieaverage + arynums[x]);
}

Remove the top }.

2) At the same for loop, change arynums to arnums.length.

3) At this loop:
for(int x = 0; x < aryMonth.length; x++)
{

System.out.println("" + arymonth[x] + " " + arynums[x]);
}

Change arymonth[x] to aryMonth[x].

4) System.out.println("The total for the year is >", calorieaverage);
I don't think this is allowed. Change it to System.out.println("The total for the year is >" + calorieaverage); I believe java will already convert calorieaverage to the equivalent string form.
Do the same for the other System.out.println.

edit: You should also declare monthlyaverage as a double or float instead of an integer, because the average isn't guaranteed to be an integer. In this case, you should also change this line:

monthlyaverage = calorieaverage/12 to monthlyaverage = calorieaverage/12.0

The final program should then be:

Code
import java.util.Scanner; //util/scannerclass
import java.io.*; //needed for files

public class Lab4 {
public static void main(String[]args) throws FileNotFoundException {

 int calorieaverage=0;
 double monthlyaverage;
 Scanner keyboard = new Scanner(System.in); //input new scanner
 
 int[] arynums = { 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 4000, 2000};

 String[] aryMonth = { "Jan", "Feb", "Mar", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};


 for(int x = 0; x < aryMonth.length; x++) {
  System.out.println("" + aryMonth[x] + " " + arynums[x]);
 }

 for(int x = 0; x < arynums.length; x++) {
  calorieaverage = (calorieaverage + arynums[x]);
 }

 monthlyaverage= calorieaverage/12.0;

 System.out.println("The total for the year is >" + calorieaverage);
 System.out.println("The average monthly total is >" + monthlyaverage);

}
}


This post was edited by INeedALife22 on Dec 19 2012 02:30am
Member
Posts: 24,488
Joined: Aug 5 2008
Gold: 38.00
Dec 19 2012 08:16am
Quote (INeedALife22 @ Dec 19 2012 03:09am)
1) You have an extra curly brace here:
}
for(int x = 0; x < arynums; x++){
calorieaverage = (calorieaverage + arynums[x]);
}

Remove the top }.

2) At the same for loop, change arynums to arnums.length.

3) At this loop:
for(int x = 0; x < aryMonth.length; x++)
{

System.out.println("" + arymonth[x] + " " + arynums[x]);
}

Change arymonth[x] to aryMonth[x].

4) System.out.println("The total for the year is >", calorieaverage);
I don't think this is allowed. Change it to System.out.println("The total for the year is >" + calorieaverage); I believe java will already convert calorieaverage to the equivalent string form.
Do the same for the other System.out.println.

edit: You should also declare monthlyaverage as a double or float instead of an integer, because the average isn't guaranteed to be an integer. In this case, you should also change this line:

monthlyaverage = calorieaverage/12 to monthlyaverage = calorieaverage/12.0 

The final program should then be:

Code
import java.util.Scanner; //util/scannerclass
import java.io.*; //needed for files

public class Lab4 {
public static void main(String[]args) throws FileNotFoundException {

 int calorieaverage=0;
 double monthlyaverage;
 Scanner keyboard = new Scanner(System.in); //input new scanner
 
 int[] arynums = { 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 2000, 4000, 2000};

 String[] aryMonth = { "Jan", "Feb", "Mar", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};


 for(int x = 0; x < aryMonth.length; x++) {
  System.out.println("" + aryMonth[x] + " " + arynums[x]);
 }

 for(int x = 0; x < arynums.length; x++) {
  calorieaverage = (calorieaverage + arynums[x]);
 }

 monthlyaverage= calorieaverage/12.0;

 System.out.println("The total for the year is >" + calorieaverage);
 System.out.println("The average monthly total is >" + monthlyaverage);

}
}


Thank you it worked :)
Member
Posts: 23
Joined: Dec 27 2012
Gold: 0.00
Warn: 10%
Dec 27 2012 05:41pm
hmm strange
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll