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

Thread: Need help with project that uses arrays

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need help with project that uses arrays

    I have a project due that I need some help with. I have no idea where to start with this one, so here's a description of the requirements:

    -Get 10 grade percentages from user, example: 79,82,85,94,52,71,88,97,56,65
    These numbers should be stored in an array.
    -The program should then ask for a letter grade input from the user.
    -It should then output how many of of that grade there are. Example: B, There are 3 Bs.

    Thanks for the help in advanced, I"m really lost on this one.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,267
    Thanks
    176
    Thanked 821 Times in 764 Posts
    Blog Entries
    5

    Default Re: Need help with project that uses arrays

    Pleas post the code you have so far, along with questions and/or errors you are receiving. If you really don't know where to start, see Trail: Learning the Java Language (The Java™ Tutorials). Otherwise, you will get little help...especially if (and I hope not) you expect someone to just do this for you.

  3. #3
    Junior Member
    Join Date
    Oct 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help with project that uses arrays

    I'm not asking for someone to write this for me, I would rather understand how to do this. I just can't get a grasp on how to implement an array.

    import java.util.*;
     
    public class Proj4{
        public static void main(String[] args) {
            Scanner s = new Scanner(System.in);
            int count = 1;
            while (count < 10) {
                   int count++;
                   System.out.println("Enter score: ");
            int score = Integer.parseItn(s.nextLine());
        }
    }
    }

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,267
    Thanks
    176
    Thanked 821 Times in 764 Posts
    Blog Entries
    5

    Default Re: Need help with project that uses arrays


  5. #5
    Junior Member
    Join Date
    Oct 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help with project that uses arrays

    How do I set up the loop gather the 10 numbers and store them in the array?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,194
    Thanks
    49
    Thanked 2,172 Times in 2,145 Posts

    Default Re: Need help with project that uses arrays

    The tutorial given above shows how to store values into an array. Your code has the loop, but uses an int variable to save the input value. Change that to use an indexed element of the array. Use the variable: count as the index into the array. One change you'll need to make is to start the index (count) at 0 vs 1.

  7. #7
    Junior Member
    Join Date
    Oct 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help with project that uses arrays

    I know I'm doing something(or lack of) wrong, but I don't know what. Please help! Thanks for your time.

    import java.util.*;
     
    public class Proj4{
        public static void main(String[] args) {
            Scanner s = new Scanner(System.in);
            int count = 0;
            while (count < 10) {
                   int count++;
                   System.out.println("Enter score: ");
             anArray = new int[10];
                   int score = Integer.parseItn(s.nextLine());
     
        }
        System.out.println("Which grade would you like to count? ");
        char letter = (s.nextLine()).charAt(0);
        if (A)
        System.out.println("There are " + score + "As");
        else if (B)
        System.out.println("There are " + score + "Bs");
        else if (C)
        System.out.println("There are " + score + "Cs");
        else if (D)
        System.out.println("There are " + score + "Ds");
        else if (F)
        System.out.println("There are " + score + "Fs");
        else
        System.out.println("Error, restart program and enter a valid grade.");
    }
    }

  8. #8
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: Need help with project that uses arrays

    There are quite a few problems in your code! You need to read about Arrays and there are also some small silly mistakes..

    This code will help you move forward:

    import java.util.*;
     
    /**
     * JavaProgrammingForums.com 
     */
    public class Proj4 {
     
    	public static int score;
     
    	public static void main(String[] args) {
     
    		int count = 0;
     
    		// Setup Array
    		int[] anArray = new int[10];		
     
    		Scanner s = new Scanner(System.in);
     
    		while (count < 10) {
    			System.out.println("Enter score: ");
    			score = Integer.parseInt(s.nextLine());
    			// Store score in Array
    			anArray[count] = score;
    			count++;
    		}
     
    		// Print contents of Array
    		for (int b = 0; b < anArray.length; b++){
    			System.out.println(anArray[b]);
    		}
    	}
    }
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,194
    Thanks
    49
    Thanked 2,172 Times in 2,145 Posts

    Default Re: Need help with project that uses arrays

    There are quite a few problems in your code!
    For example:
    you define count in two places: one outside the while loop and the other inside the loop. You should only define count in ONE place and then use it where you need it.
    The same for the array: anArray. Define it outside the loop. Then assign values to it inside the loop as the values are received from the user.
    The next part of your code needs to be completely redone. Here you need to prompt the user for what to search for: A or B etc and then you need a loop to look at each of the scores one by one to see if it is in the desired range and if it is, to count it. After the loop is done, print out the results.

Similar Threads

  1. New To Java (Help with arrays)
    By SilentPirate in forum What's Wrong With My Code?
    Replies: 6
    Last Post: September 16th, 2010, 05:48 AM
  2. for loop and arrays !
    By M7MD in forum Loops & Control Statements
    Replies: 4
    Last Post: May 7th, 2010, 07:41 AM
  3. Arrays
    By mlan in forum Java Theory & Questions
    Replies: 2
    Last Post: February 26th, 2010, 09:23 AM
  4. 2d Arrays
    By mgutierrez19 in forum Collections and Generics
    Replies: 5
    Last Post: October 27th, 2009, 04:08 PM
  5. Elegant and short way to implement my program on 2d Array
    By TheForumLord in forum Collections and Generics
    Replies: 1
    Last Post: December 8th, 2008, 03:56 PM