Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Cant end input of data on my program...please help

  1. #1
    Member
    Join Date
    Jan 2014
    Location
    New Jersey
    Posts
    48
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Cant end input of data on my program...please help

    So here are my directions for this program:

    Write a program that allows the user to enter students names followed by their test scores and outputs the following information(assume that the maximum nmber of students in the class is 50; if the number of students is less than 50, to indicate the end of input data, after entering the last students data, on a line by itself, hold the ctrl key/press z and then press the Enter key):

    a) Class average

    b) Names of all the students whose test scores are below the class average, with an appropriate message( youre below class average)

    c) Highest test score and the names of all the students having the highest score.

    Use methods.

    Now I wrote the program but i cant figure out how to end the input by pressing ctrl key/press z and then press the Enter key... Can some one show me how this is done?

    Here is my code
    import java.util.Scanner;
     
    public class ClassAverage
    {
    	public static void main(String args[])
       	{
       	    String names[] = new String[50];
       	    int scores[] = new int[50];
       	    int entries = 0;
     
       	    Scanner in = new Scanner(System.in);
       	    //System.out.println("Enter number of entries");
       	    //int entry = in.nextInt();
     
       	    System.out.println("Enter the names followed by scores of students: ");
     
       	    for(int i = 0; i < 50; i++)
       	    {
       	        names[i] = in.next();
       	        scores[i] = in.nextInt();
       	        entries++;
       	    }
       	    Average avg = new Average();
       	    double average = avg.CalcAvg(scores,entries);
     
       	    System.out.println("The class average is: " + average);
       	    avg.belowAvg(scores,average,names,entries);
       	    avg.highestScore(scores,names, entries);
       	}
    }
    class Average
    {
    	Average()
    	{
    		System.out.println("The averages: ");
    	}
        double CalcAvg(int scores[], int entries)
        {
            double avg;
            int total = 0;
     
            for(int i = 0; i < entries; i++)
            {
               total += scores[i];
            }
               avg = total/entries;
            return avg;
        }
     
        void belowAvg(int scores[],double average,String names[], int entries)
        {
            for(int i = 0; i < entries; i++)
            {
                if(scores[i] < average)
     
                    System.out.println(names[i] + "You're below class average");
     
            }
        }
     
        void highestScore(int scores[],String names[], int entries)
        {
            int max = scores[1];
     
            for(int i = 0; i < entries; i++)
            {
                if(scores[i]>=max)
                    max=scores[i];
            }
            System.out.println("The maximum score is: " + max);
            System.out.println("The highest score acheivers list: ");
     
            for(int i = 0; i < entries; i++)
            {
                if(scores[i] == max)
                    System.out.println(names[i]);
            }
        }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Cant end input of data on my program...please help

    did you mean ctrl+c
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Jan 2014
    Location
    New Jersey
    Posts
    48
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Cant end input of data on my program...please help

    No, the directions said ctrl/press z then Enter key....
    Quote Originally Posted by Norm View Post
    did you mean ctrl+c

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Cant end input of data on my program...please help

    CTRL-Z (ASCII code 26) is the DOS/Windows EOF marker. Look at the API for your input to determine how the "end of the file" is detected. For example, in a file reader, the way to read data until the end of the file has been reached is something like:

    while ( fileReader.hasNextLine() )

Similar Threads

  1. comparison of data from front-end jsp page to database values
    By yashwanthguru in forum JDBC & Databases
    Replies: 0
    Last Post: March 1st, 2013, 12:37 PM
  2. program will not end
    By Zilender in forum What's Wrong With My Code?
    Replies: 8
    Last Post: April 24th, 2012, 10:16 AM
  3. [SOLVED] allow a new input, dicarding the last mismatch input without terminating the program
    By voltaire in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 9th, 2010, 04:44 AM
  4. Java program to find the minimum and maximum values of input data
    By awake77 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: December 20th, 2008, 05:12 PM