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

Thread: Using Arrays to make a scoring program

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Using Arrays to make a scoring program

    I'm very new to Java since im only in my first semester of it in highschool but I am trying to create a program that can be used to score a competition.
    The program will ask you how many schools are attending, then ask you the name of each school which will be stored in an array. Then it needs to ask you what each school will be participating in, there is a platoon category, color guard category, and squad category. They can aslo have more then one team in each category and can participate in any or all categories.

    For example, Henderson County Highschool wants to participate in the squad and platoon categories, they will have 1 team for squad and 2 teams for platoon.

    Anyway, after asking all that from the user, it will ask what score did each team from each school get and then it will calculate all the scores up and tell who the winners are in each category and a overall winner.

    So far I have made the program ask how many schools are attending and then ask the names of the schools and store them but I'm having trouble with the teams.. How do i get each team to be assigned to a certain school when the school names are stored in a array? I hope you guys understand what I am trying to do here.. and this is my code so far


    import java.util.Arrays;
    import java.util.Scanner;
     
    public class TextLab02st
    {
    	public static void main(String args[])
    	{
    		StartSchoolEntry.GetNumberOfSchools();
    		StartSchoolEntry.GetNamesOfSchools();
    	}
    }
     
     
    class StartSchoolEntry
    {
    		static Scanner scanner =  new Scanner(System.in);
    		static int numberofschools;
    		static String names[];
     
    		public static void GetNumberOfSchools()
    		{
    			System.out.print("How many schools are attending? ");
    			numberofschools = scanner.nextInt();
    			System.out.println("Good, then there will be "+numberofschools+" schools attending.");
    			System.out.println(" ");
    			System.out.println(" ");
    			System.out.println("#############################################");
    			System.out.println(" ");
    			System.out.println(" ");
    		}
     
    		public static void GetNamesOfSchools()
    		{
    			String names [] = new String[numberofschools];
    			for(int i = 0; i<names.length; i++)
    			{
    			int realnumber = i+1;
    			System.out.print("What is the name of school "+realnumber+" ? ");
    			names[i] = scanner.next();
    			}
    			System.out.println(" ");
    			System.out.println(" ");
    			System.out.println("#############################################");
    			System.out.println(" ");
    			System.out.println(" ");
    //			for (int i = 0; i<names.length; i++)				         // I only created this for loop
    //			{								                                 // to make sure that the school 
    //				int realnumber = i+1;					                 // names were being stored, 
    //				System.out.print("School number "+realnumber+" is ")  // and that i could make it print
    //				System.out.println(names[i]);				                 // their names out.
    //			}
     
    		}
     
    }


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Using Arrays to make a scoring program

    Void methods don't return anything.

    The values entered will be lost when you exit the method.

    Unless you change the return type to int or String or whatever you need to.

  3. #3
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Using Arrays to make a scoring program

    Your getSchoolNames should be of type String[].

    You're storing them but since names in just defined inside that method, it goes out of scope when that method ends.

  4. #4
    Junior Member
    Join Date
    Jan 2011
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Using Arrays to make a scoring program

    That's why when I created a different method and tried calling them it gave an error, thanks!
    But that doesn't answer my original question =/

  5. #5
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Using Arrays to make a scoring program

    For tallying up the scores, parallel arrays would be a good idea.

    Make an index, say index 0, for a school in a String array.

    Then make a bunch of int arrays to store scores for each category and put all the scores for that school at index 0 of those arrays.

    Have another array store the final scores.

    (Just add up the values at each index for that school at that index and put the final total at that index of this array.)

    If it weren't possible to have a tie, then you could just compare every score against every other one and loop through it with a for loop to determine the winner.

    However, as it is, you could have a setPlace(int place) method or something.

    1st place would be where no other score is higher, could be equal though, to it.
    2nd is where there is at least 1 score higher than it that is at first place.
    3rd is if there is a score that is second that is higher than it.
    And so on.

    Use a int getPlace() method to check to see for that.

    In fact, have a school class.
     
    public class School
    {
     
    private String name;
    private int place =0;
     
    public School(String name)
    {
    this.name = name;
    setName(name);
    }
     
    public void setName(String name)
    {
    this.name =name;
    }
     
    public String getName()
    {
    return name;
    }
     
    public void setPlace(int place)
    {
    this.place = place;
    }
     
    public int getPlace()
    {
    return place;
    }
    }
    Also, your
    static String names[];

    should be
    static String[] names;

    Also, you should pass values to your methods in your class that doesn't have the main method.

    Use the console only in the class with the main method.

    In fact, you should probably put all the methods from StartSchoolEntry into TextLab02st.

    You call the methods you need in the main method.

    You can have console in methods other than the main method, but you should only use console for classes that actually have a main method.

    Also, once you get your array of names, use a for loop to get an array of School by creating a new School

    School[] schools = new School[getSchoolNames().length];
    Then iterate through, and

    schools[i] = new School(names[i]);

  6. #6
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Using Arrays to make a scoring program

    Also, I was wrong. Your name array won't be lost. What will be lost however will be the number of schools you got in getNumberOfSchools(). If you changed the return type to int, then

    names = new String[getNumberOfSchools()];

  7. #7
    Junior Member
    Join Date
    Jan 2011
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Using Arrays to make a scoring program

    Wow, I wasn't expecting a response so quickly, thanks! ^.^
    I'm going to test what you've just told me, i need to figure out what parallel arrays are though, as a said i'm very new to java and just took about arrays so i thought i would make a program using them

  8. #8
    Junior Member
    Join Date
    Jan 2011
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Using Arrays to make a scoring program

    You've helped a lot, but I do have another question..
    I'm not sure what you mean by making an index for a string array, is there a tutorial or website you could tell me about that could explain it?

  9. #9
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Using Arrays to make a scoring program

    woodcutterni, I'd recommend taking the advice given with a grain of salt. Creating all soirts of arrays all over the place - amongst other things - completely ignores the object oriented nature of the Java programming language, and doesn't seem to really answer your original question in the first place. Suggested reading: Classes and Objects. Use this tutorial as a guide to create your data model using classes, which are much more readily adapted, changed, and readable then 'parallel arrays'

  10. The Following User Says Thank You to copeg For This Useful Post:

    javapenguin (January 7th, 2011)

  11. #10
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Using Arrays to make a scoring program

    Ahhhhh...I see what copeg is getting at. You could make a Team class that keeps track of the score for that Team.

    //code removed by moderator

    I'm hoping that no two schools have the exact same name, or else the boolean method above could go astray.

    If none have the exact same name, all you need to do for a school is check to see if a the name there is the same as the school name of the school the team was assigned to.

    If it is, add the points to the total for that school. If not, don't.

    Team t = new Team(...);
    say names[0] = "AnywhereVille High";
    schools[0] = new School(names[0]);
    t.setSchool(schools[0]);
    Last edited by copeg; January 7th, 2011 at 05:30 PM.

  12. #11
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Using Arrays to make a scoring program

    javapenguin, if you have not read my private message, please do so now. And in addition please refrain from handing over all sorts of code like this. You could ultimately lead the original poster in the completely wrong direction and be more of a hinderance than a help. Take it one step at a time and refrain from trying to fully complete someone else's assignment.

    woodcutterni, has your original question been addressed?

  13. #12
    Junior Member
    Join Date
    Jan 2011
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Using Arrays to make a scoring program

    Not really, im still stuck on how to assign teams to the schools stored in the array of names[]. I'm trying to work out javapenguins code, but not really making any progress, im a newbie so i can't handle too much at one time lol

  14. #13
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Using Arrays to make a scoring program

    Quote Originally Posted by woodcutterni View Post
    Not really, im still stuck on how to assign teams to the schools stored in the array of names[]. I'm trying to work out javapenguins code, but not really making any progress, im a newbie so i can't handle too much at one time lol
    Another reason I said to take that with a grain of salt. Its easier (and more beneficial for you) to build and learn one step at a time then to have something thrown at you and expect it to come together. Here's a different approach: create a class called School, each School contains information on the school and the team (Team could be just the information, or it could be another class). This inherently associates the team with the school. Using this approach you will then have to change the array names[] to an array containing objects of the type School (as opposed to string). If you don't know how to do this I recommend thoroughly reading the link I posted above and do a bit of research on object oriented programming to give you a good idea for how to accomplish this task. And if you have problems or questions along the way post them for more help.
    Last edited by copeg; January 7th, 2011 at 07:02 PM.

  15. The Following User Says Thank You to copeg For This Useful Post:

    woodcutterni (January 7th, 2011)

  16. #14
    Junior Member
    Join Date
    Jan 2011
    Posts
    8
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Smile Re: Using Arrays to make a scoring program

    I see what your getting at, I guess thats what javapenguin was trying to show me aswell but I just couldnt see that easily.
    I always thought I had to use an array of objects lol
    I guess this is why your a super moderator, thank you for the help
    Last edited by woodcutterni; January 7th, 2011 at 07:05 PM.

Similar Threads

  1. how do i make my program to do......
    By andys in forum Object Oriented Programming
    Replies: 6
    Last Post: November 29th, 2010, 07:44 AM
  2. how do i make my program to....
    By andys in forum Object Oriented Programming
    Replies: 2
    Last Post: November 26th, 2010, 10:31 AM
  3. how to make a program take time...
    By DLH112 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 10th, 2010, 07:09 PM
  4. Help with a program to make a landscape picture
    By noseeds in forum Java Theory & Questions
    Replies: 1
    Last Post: December 15th, 2009, 10:25 PM
  5. Java program to write game
    By GrosslyMisinformed in forum Paid Java Projects
    Replies: 3
    Last Post: January 27th, 2009, 03:33 PM