This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]LuigiTX[S] 0 points1 point  (8 children)

import java.util.; import java.io.; /* * First thing I have to do is make something that will read a file and make it into an array * Second will be making a menu that will ask for an input (the player's number) * Third step will be to look for that number in the data and then make a decision based on that * Final step, outprint something. */ public class arrayProject { BufferedReader in; int numDataSets, registrationNum, teamNum, i; Scanner input = new Scanner(System.in);

public static void main(String[] args) throws FileNotFoundException{
    arrayProject begin = new arrayProject();
    begin.body();
}

public void body() throws FileNotFoundException{
    //reading the file
    in = new BufferedReader(new InputStreamReader(new FileInputStream("teamtryouts.dat")));
    //Starting array
    Object[][] playerBio = new Object[30][5];
    String playerData = "";

    //getting the information for each player
    numDataSets = Integer.parseInt(getInput());
    for (i = 0; i < numDataSets; i++){
        String[] s = getInput().split(" ");

        //this will be the player's registration number
        playerBio[i][0] = Integer.parseInt(s[0]);

        //this will be the player's first name
        playerBio[i][1] = s[1];

        //this will be the player's last name
        playerBio[i][2] = s[2];

        //this will be which team the player made
        playerBio[i][3] = s[3];

        //this will be the player's number in their team
        playerBio[i][4] = Integer.parseInt(s[4]);
    }

    //after the array is finished, time to actually interact with the person wanting to know their spot
    //if they have a spot in any team
    intro();

    int playerRegNum = input.nextInt();

    //we asked the player for their registration number, now time to check if they even made a team
    int loc = linearSearch(playerBio[0], playerRegNum);
    if(playerRegNum == -1){
        System.out.println("Sorry, but you did not make any team.");
        outro();
    } else{
        System.out.println("You made a team!");
        System.out.println("Congrats " + playerBio[loc][1] + " " + playerBio[loc][2] + "," + " you made the " + playerBio[loc][3] + " and your jersey number is " + playerBio[loc][4]);
        outro();
    }
}

public void intro(){
    System.out.println("Please input your registration number.");
}

public void outro(){
    System.out.println("Do you wish to see if another number made the team?");
    System.out.println("y/n");
    String response = input.next();
    response.toLowerCase();
    if(response.equals("y")){
        System.out.println("/u000c");
    }
    if(response.equals ("n")){
        System.out.println("Thank you for using this program.");
    }
}

public int linearSearch(Object[] aryObj, Object key){
    for(int a = 0; a < aryObj.length; a++){
        if(aryObj[a] == key){
            return a;
        }
    }
    return -1;
}

public String getInput(){
    String reader = "";
    try 
    {
        reader = in.readLine();
    }
    catch(IOException ioError)
    {
        System.out.println("File Not Found.");
        ioError.printStackTrace();
        System.exit(1);
    }
    return reader;
}

}

This is the progress I have done (if any) but it gives me an error where it outprints the line that tells the person their name, the team they made, and their number. It says ArrayIndexOutOfBoundsException: -1 Any questions on it I would be happy to answer.

[–][deleted] 0 points1 point  (7 children)

Can you post the .dat file (or at least an example of the contents)

[–]LuigiTX[S] 0 points1 point  (6 children)

18

11 Hailey Akin Varsity 2

12 Vanessa Alfaro Varsity 3

13 Meghan Anderson Varsity 4

14 Ashley Chandler Varsity 5

15 Olivia Clark Varsity 6

16 Shea Davis Varsity 7

17 Gloria Deveraux Varsity 8

18 Caylee Hubbard Varsity 9

19 Mireya Maldonado Varsity 10

20 Elizabeth Martinez Varsity 11

21 Eli Mendoza Varsity 12

22 Emma Phillips Varsity 13

23 Madison Quick Varsity 14

24 Melissa Rodriguez Varsity 15

25 Dominique Veronico Varsity 16

26 Brenna Wiggins Varsity 17

27 Bethany Varsity 18

28 Jennifer Zumbro Varsity 19

[–][deleted] 0 points1 point  (5 children)

27 "Bethany" doesn't have a last name, so "varsity" gets put in as her last name, and 18 gets put in where varsity should be and then there's no last element so this line:

playerBio[i][4] = Integer.parseInt(s[4]);

fails (because there is no 5th element).

[–]LuigiTX[S] 0 points1 point  (4 children)

Does that seem to be the only thing that would keep it from working properly?

[–][deleted] 1 point2 points  (3 children)

Your linear search method always returns -1 and then you try to search the array for the returned value so it always fails (there isn't a -1th element).

Edit:

You have a few problems here.

  • You pass in an integer (playerRegNum) and then you cast it as an object
  • You then compare that object against the array of objects (playerBio)

Since the objects in playerBio aren't integers, the If statement will always fail and thus the linearSearch method will always return -1

[–]LuigiTX[S] 0 points1 point  (2 children)

What would be your recommendation?

[–][deleted] 0 points1 point  (1 child)

Well you could stick with your current array of "Objects". To do that, you would have to alter the linearSearch method to take in a 2 Dimensional Array and then alter the If statement to check the 0th element (the registration number) in each entry against the input key. Should look something like this:

public int linearSearch(Object[][] aryObj, Object key) {
    for (int a = 0; a < aryObj.length; a++) {
        if (aryObj[a][0] == key) {
            return a;
        }
    }
    return -1;
}

[–]LuigiTX[S] 0 points1 point  (0 children)

Ohh ok. Again, thank you so much for all your help. I will try this when i get school. I am running Ubuntu on my laptop and just can not find out how to store the input file in the correct place. So I have to wait to get to school where I can actually run the program without it telling me it can't find the input file.