help with arrays

Beginner with java coding and weak with arrays

Page 1 of 1

1 Replies - 1060 Views - Last Post: 11 October 2010 - 04:51 AM Rate Topic: -----

#1 JIGAB4CH1  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 11-October 10

help with arrays

Posted 11 October 2010 - 04:34 AM

I am in the middle of writing a code for my class and i am a beginner to Java, I feel I am in the right direction as far as getting the code right but I get a "line 15 ";" expected" error and I think it interferes with the rest of the compiler to tell me if I've written the rest of the code right, if some one can take a look and help me out it would be much appreciated, if it helps i use JCreator.

import java.util.Scanner;//needed to acces the console and client input
import java.util.Random;//neede to generate the random number needed for student


public class projectArrays
{
public static void main (String[] args)
{

    Scanner cin = new Scanner(System.in);//initiate a Scanner for client inputs
    
     
    students =(Math.random)*50)+1;//this will generate a random number between 0-50 for students
    //**bonus attempt** giving the use the option to decline continueing the program
    System.out.println("You have :"+students+"in your class,do you wish to continue? y/n ");
         if (cin.next().startsWith("y")||cin.next().startsWith("Y"))
        {
            //initiating the array to hold the grades according to the number of students       
            int[] grades = new int[students];
            System.out.println("You have a total of"+grades.length+"students");
            
            System.out.println("Enter the numerical grades for all students.");
            System.out.println("Press enter after each entry.");
            //making a statement to allow the client to enter values for the array
            for (int i=0;i<students+1;i++);
            {
                grades[i] = cin.nextInt();
                //creating an ERROR statement if the client enters a "bad" entry
                if(grades.length != students|| 0>cin.nextInt()>100)
                {   
                    System.out.println("ERROR INCORRECT ENTRY!");                       
                }
                
             //continue with coding as if the client has performed the right actions
             else
             {
               //printing the contents of the array
               System.out.println("you have entered");
                for(int i=0;i<grades.length;i++)
                {
                    System.out.print(grades[i] + " ");
                }
                //averaging the elements within the array and displaying them 
                int sum = 0;
                for(int i=0;i<grades.length;i++);
                {
                    sum= sum+grades[i];
                    double average= sum / grades.length;
                    System.out.println("the Average of all grades is"+sum+" ");
                    
                }
                
                //this section if for counting the amount of zeors in entered into the array
                int countZ = 0 ;
                for (int i=0;i<grades.length;i++)
                {if( grades[i]==0)
                countZ++;
                }
                //this section now counts the amount of Hundreds entered into the array
                int countH = 0; 
                for (int i=0;i<grades.length;i++)
                {if(grades[i]==100)
                countH++;
                //now to display both amounts of zeros & hundreds found
                System.out.println("There are"+countZ+"zeros.");
                System.out.println("there are"+countH+"hundreds.");
            
            }


            }
            
        
        }
        
       }
       //else statement to run in case the client denies to use program 
       else
       { 
         System.out.println("you have chosen to conclude your session, you may now exit.");
       }    
}       

}



Is This A Good Question/Topic? 0
  • +

Replies To: help with arrays

#2 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon

Reputation: 2965
  • View blog
  • Posts: 11,222
  • Joined: 15-July 08

Re: help with arrays

Posted 11 October 2010 - 04:51 AM

You have not specified the variable type on students...
int students = (int) ((Math.random())*50)+1



Also you need to get rid of the semicolon here:
for (int i=0;i<students+1;i++)



Also, this is invalid syntax:
0>cin.nextInt()>100



You should assign cin.next() to a variable and compare to the variable instead or you will have to input a new string every time you compare it.

Also, you have for loops inside the for loops that use a variable called i. However, you've already used i in this scope. You need to rename he variables to say, j.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1