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 3 of 3

Thread: Array program help needed

  1. #1
    Junior Member
    Join Date
    Dec 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Array program help needed

    Java newbie here just trying to figure out the solution to this problem. The task is to create a program that asks a user the following:
    How many tests scores? 2 [enter]
    Enter test score #1:70 [enter]
    Enter test score #2:50 [enter]
    Enter a score you wish to search: 50 [enter]
    You scored 50 on test #2.
    Or, display an error message if the score is not found. The program is to store the data in the array "exams". My code so far is below.

    Exam class
    public class Exam {
    	private int[] exams;
    	public Exam(int[] exams2)
    	{
    		this.exams = exams2;
    	}
    	public void sequentialSearch(int check) 
    	{
    		for (int i = 0; i < exams.length; i++) 
    		{
    			if (check == exams[i])
    				System.out.println("You got a " + check + " on exam "+ (i+1));
    			else
    				System.out.println("The test score was not found.");
    		}
    	}
     
    }
    ExamTester class
    import java.util.Scanner;
    public class ExamTester {
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		Scanner keyboard = new Scanner(System.in);
    		System.out.println("How many exams?");
    		int numexams = keyboard.nextInt();
    		int[] exams = new int[numexams];
    		for (int a = 0; a < numexams; a++) 
    		{
    			System.out.print("Enter a score on test # "+ (a+1) + ": " );
    			exams[a] = keyboard.nextInt();
    		}
    		System.out.println("Enter a score that you want to search: ");
    		int check = keyboard.nextInt();
    		Exam e = new Exam(exams);
    		e.sequentialSearch(check);
    		}
    	}
    The program works, but it always outputs "The test score was not found" even after saying "You got x on exam y". I know it has something to do with that for loop, but I am stuck on how to fix it. Any help would be GREATLY appreciated!

    Thanks!!


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,356
    Thanks
    182
    Thanked 833 Times in 776 Posts
    Blog Entries
    5

    Default Re: Array program help needed

    You should add a flag to the loop letting you know a score was found, rather than the if/else, at least I'm guessing this is the behavior you want
    		boolean found = false;
    		for (int i = 0; i < exams.length; i++) 
    		{
    			if (check == exams[i]){
    				System.out.println("You got a " + check + " on exam "+ (i+1));
    				found = true;
    			}				
    		}
    		if ( !found ){
    			System.out.println("The test score was not found.");
    		}

  3. #3
    Junior Member
    Join Date
    Dec 2009
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Array program help needed

    Oh man I can't believe I didn't think of a boolean. Thanks so much!

Similar Threads

  1. ebooks needed
    By programmer2009 in forum The Cafe
    Replies: 3
    Last Post: September 17th, 2011, 11:26 AM
  2. Grade averaging program with array and multiple methods.
    By jeremykatz in forum What's Wrong With My Code?
    Replies: 8
    Last Post: November 9th, 2009, 09:44 PM
  3. Average program with array and loop and JOptionPane.
    By jeremykatz in forum AWT / Java Swing
    Replies: 6
    Last Post: October 25th, 2009, 03:33 PM
  4. help me to do this Array Program
    By fahdsaad in forum Member Introductions
    Replies: 1
    Last Post: June 30th, 2009, 03:19 AM
  5. Replies: 4
    Last Post: May 22nd, 2008, 11:59 AM