954,536 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Problem with gathering input

Hello,

I am having a hard time with a easy problem. I need to ask a user how many scores they would like to enter (up to 15) and then use a method to get the number of scores. in the form of

Enter score 1: xx.xx

Enter score 2: xx.xx

Enter score 3: xx.xx

and so on till you hit the requested number or 15.

Here is what I have so far. Your help is greatly appreciated.

import java.util.Scanner;

 public class Exams
 {
	 public static void main ( String args [] )
     {}
	    private final static int MAX_SCORE =15;
        private int count=0;
     
        
        public Exams()
        {
        	Scanner input=new Scanner(System.in);
        	System.out.print("Welcome to the Exam Scores Program \n");
        	System.out.print ("\n");

        	System.out.print ( "   How many scores will you enter (up to 15):");
            count = input.nextInt();
            System.out.print ("\n");
            
            if (count > MAX_SCORE)
            {
                    System.out.print("   Error: You may onlyenter upto 15 scores - Please        try again:");
                    count=input.nextInt();
                    System.out.print ("\n");
            }
        }
        
        public void getScores ()
        {
        	int i = 0;
        	while (1 <= count)
        	{
        		System.out.print("Enter Score " + i );
        		
        		i++;
        	}
        }

 }
sateal8
Newbie Poster
7 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
 
codewall
Junior Poster in Training
80 posts since Dec 2010
Reputation Points: 13
Solved Threads: 11
 

your getScores() method does not contain a line to take input. inside your constructor method, there is a line that takes input through nextInt() method. you need similar code for your getScores() method so that your app can get scores. plus, you need some array variable to store your input scores. and, your if statement on line 21 is of not much use. you'd be better of using a while loop which would execute as long as the input on line 18 is not in the range 0 - 15. hope this helps.

bibiki
Posting Whiz
327 posts since Nov 2009
Reputation Points: 28
Solved Threads: 27
 

What part are you having a hard time with? Programming concepts? Logic?

Akill10
Posting Pro
575 posts since Sep 2010
Reputation Points: 115
Solved Threads: 80
 

this is a simple problem...a

for(;;)


loop is sufficient to allow the user to enter values many times; in this case upto 15.

hauda67
Newbie Poster
15 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
 

I am having trouble with collecting the information in the format shown and requesting a user entered amount of input requests. I know how to do it if there was a set amount but not if its a number chosen by the user.

The instructions are

Create a Java Class to define a list of exams class, „NetID_ExamList‟, that stores a list of „double‟ values input by the user and processes the data in the list. The exam list class will contain a method, „getScores‟, to prompt the user to enter up
to 15 double values. These values should be stored in an array. Because the user may enter “up to” 15 values, the class must have an instance variable to hold the actual number of values input.

I don't know howto do create an array to request input after being asked how many inputs the user would like to submit. And how to get that information into the method to collect the information

sateal8
Newbie Poster
7 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
 

when you use a for loop you store each number entered by user into an array.

hauda67
Newbie Poster
15 posts since Nov 2009
Reputation Points: 10
Solved Threads: 0
 

Well,
1. first you need to declare and initialise an array with space for 15 numbers max.
I assume you know how to do that part.
2.Ask the user for how many numbers they want to input
3. Store this value in some variable. i.e. your count variable
4. Now, you could create your getScores method that takes a double as a parameter. Therefore, you can pass in the value of count. And then you can use that value in your method.

//to call getScores method using your count variable
getScores(count);
public void getScores(double count)
{
// You will need some sort of loop, which loops 'count' times. Ask for input inside the loop, then add the value to the array on each iteration of the loop

}
Akill10
Posting Pro
575 posts since Sep 2010
Reputation Points: 115
Solved Threads: 80
 
import java.io.*;
class ip1
{
public static void main(String args[]) throws IOException
{

DataInputStream in=new DataInputStream(System.in);
String str;
int n;
int i;
System.out.println("Welcome to the Exam Scores Program");
System.out.println( "How many scores will you enter (enter up to 15):");
str=in.readLine();
n=Integer.parseInt(str);
for (i=0;i<= n;i++)
{
System.out.println("Enter Score \n" );
str=in.readLine();
i=Integer.parseInt(str);
if(i> n)
System.out.println("wrong input!enter number up to 15.\n" );
}

}}

-----------------

the output is:
C:\Java1\jdk1.6.0\bin>java ip1
Welcome to the Exam Scores Program
How many scores will you enter (enter up to 15):
15
Enter Score
1
Enter Score
2
Enter Score
3
Enter Score
4
Enter Score
5
Enter Score
6
Enter Score
7
Enter Score
8
Enter Score
9
Enter Score
10
Enter Score
11
Enter Score
12
Enter Score
13
Enter Score
14
Enter Score
15
C:\java1\jdk1.6.0\bin>
C:\Java1\jdk1.6.0\bin>java ip1
Welcome to the Exam Scores Program
How many scores will you enter (enter up to 15):
15
Enter Score
18
wrong input!enter number up to 15.
C:\Java1\jdk1.6.0\bin>

sathya_s
Newbie Poster
6 posts since Jan 2011
Reputation Points: 10
Solved Threads: 1
 

import java.io.*; class ip1 { public static void main(String args[]) throws IOException {

DataInputStream in=new DataInputStream(System.in); String str; int n; int i; System.out.println("Welcome to the Exam Scores Program"); System.out.println( "How many scores will you enter (enter up to 15):"); str=in.readLine(); n=Integer.parseInt(str); for (i=0;i<= n;i++) { System.out.println("Enter Score \n" ); str=in.readLine(); i=Integer.parseInt(str); if(i> n) System.out.println("wrong input!enter number up to 15.\n" ); }

}}

-----------------

the output is: C:\Java1\jdk1.6.0\bin>java ip1 Welcome to the Exam Scores Program How many scores will you enter (enter up to 15): 15 Enter Score 1 Enter Score 2 Enter Score 3 Enter Score 4 Enter Score 5 Enter Score 6 Enter Score 7 Enter Score 8 Enter Score 9 Enter Score 10 Enter Score 11 Enter Score 12 Enter Score 13 Enter Score 14 Enter Score 15 C:\java1\jdk1.6.0\bin> C:\Java1\jdk1.6.0\bin>java ip1 Welcome to the Exam Scores Program How many scores will you enter (enter up to 15): 15 Enter Score 18 wrong input!enter number up to 15. C:\Java1\jdk1.6.0\bin>

What is this for?

Akill10
Posting Pro
575 posts since Sep 2010
Reputation Points: 115
Solved Threads: 80
 

Here is what I have now, but when I go to compile it stops at the process scores method, can anyone show me where I am going wrong?

import java.util.Scanner;

 public class steal_ExamList
 {
	    private final static int MAX_SCORE =15;
    	double number,small,large,average;
    	int count;
		private double[] list;
    	
        public steal_ExamList()
        {
        	System.out.print("Welcome to the Exam Scores Program \n");
        	System.out.print ("\n");
        }
        
        void getScores()
        {
        	
        	Scanner input=new Scanner(System.in);
        	System.out.print ( "How many scores will you enter (up to 15):");
            count = input.nextInt();
            System.out.print ("\n");
            
            while (count > MAX_SCORE)
            {
                    System.out.print("Error: You may onlyenter upto 15 scores - Please        try again:");
                    count=input.nextInt();
                    System.out.print ("\n");
            }
            
            double list[] = new double[count]; 
            
            for(int i = 0;i< list.length; i++)
            {
            System.out.println("Enter score " + (i+1) + ": ");
            number = input.nextDouble();
            }                              
        }
        
        void processScores()
        {
        	double small = list [count];
        	
        	for (double number : list)
        	{
        		if (number < small)
        			small = number;
        	} 
        	
        	double large = list [count];
        	
        	for (double number : list)
        	{
        		if (number > large)
        			large = number;
        	}
			
        	double total = 0;
        	
        	for(double number : list)
        		total += number;
        	
        	average = total/number;

        }
        
        void displayResults()
        {
        	System.out.println("\nAverage Score = " + average);
        }
 }
sateal8
Newbie Poster
7 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
 

What do you mean it 'stops' there?

Akill10
Posting Pro
575 posts since Sep 2010
Reputation Points: 115
Solved Threads: 80
 

This is the error that comes up when i debug.

steal_ExamListTest [Java Application]
steal_ExamListTest at localhost:60890
Thread [main] (Suspended (exception NullPointerException))
steal_ExamList.processScores() line: 42
steal_ExamListTest.main(String[]) line: 8
C:\Program Files\Java\jre6\bin\javaw.exe (Jan 27, 2011 11:26:49 AM)

my test file looks like this

public class steal_ExamListTest 
{
	    public static void main ( String args[] )
	    {
	    steal_ExamList foo = new steal_ExamList();
	    foo.getScores();
	    foo.processScores();
	    foo.displayResults();
	    }
	}
sateal8
Newbie Poster
7 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
 

print out the value of count
edit: you haven't specified a size to your array.

Akill10
Posting Pro
575 posts since Sep 2010
Reputation Points: 115
Solved Threads: 80
 

That's where i am having problems, is assigning values to the array's. I can not figure out how to do it, and the book i have for class is terrible and i can't apply the examples i have found online to my program.

sateal8
Newbie Poster
7 posts since Sep 2010
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: