0

I need to store 25 different arrays with position one being the name of the person and the rest of the indexs as a int to do math operations. I have a String arrray[25][53]. How do I make array[0-25][1-52] an integer? I assuming with .parseInt but Im not really sure how they work considering I am still learning java.

String[][] volunteerNamesAndHours = new String [25][53];
    int ID = 0;
    int week;
    do{
        volunteerNamesAndHours[ID][NAME] = input.next();
            for(week = 1; week < 53; week++){
                 volunteerNamesAndHours[ID][week] = Integer.parseInt(null, ID);

EDIT: I would use OOP or a map but considering we havent got that far in the course I don't want to over step my boundaries and making my professors mad. I know it is not the most intuitive but this what I ended up coming up with any body see a problem?

public static String[][] getvolunteerChart(Scanner input){
    String[][] volunteerNamesAndHours = new String [25][53];
    int ID = 0;
    int week;
    do{
        volunteerNamesAndHours[ID][NAME] = input.next();
            for(week = 1; week < 53; week++){
                 volunteerNamesAndHours[ID][week] = Integer.toString(input.nextInt());
            }
        ID++;    
    }
    while(ID <= 24);

    return volunteerNamesAndHours;        

            }
2
  • Haven't got to OOP yet. Im sure I will be doing that in the next two weeks. Commented Oct 30, 2014 at 12:35
  • Look at the answers using a Map Commented Oct 30, 2014 at 12:37

5 Answers 5

2

You need to use a Map structure for this use-case.

Map<String, Integer[]> personIDs = new HashMap<>(25);

personIDs.put("Peter", new Integer[]{5,22,7734});

personIDs.get("Peter");//returns the array 5,22,7734
Sign up to request clarification or add additional context in comments.

Comments

1

I would suggest to use hashmap to store name as key and arraylist of integers as value like

Map<String, ArrayList<Integer>> volunteerNamesAndHoursMap = new HashMap<String, ArrayList<Integer>>();

Comments

0

You should follow an object-oriented approach and encapsulate your volunteers data in a class like this:

public class Volunteer {

    private int id;

    private String name;

    private int hours;

    .
    .
    .

    public in getId() {
        return id;
    }

    public void setid(int id) {
        this.id = id;
    }

    .
    .
    .

}

Now you are able to store them in a List or an array. Othe than arrays, Lists usually have no predefined size limitation.

List<Volunteer> volunteers = new ArrayList<>();

// Or as an array

Volunteer[] volunteers = new Volunteer[25];

You can then iterate over the list and do your math using the getters and setters like this:

for (Volunteer volunteer : volunteers) {
    int oldHours = volunteer.getHours();
    int newHours = oldHours + 8;

    volunteer.setHours(newHours);
}

Comments

0

First of all, I think you'd need an array of Strings and then the multi-dimensional array of ints with length 52. Java is a very static language, and it won't let you do crazy things like storing an Integer in a String array.

String[] volunteerNames = new String [25];
int[] volunteerHours = new int [25][52];

Second of all, I think you could really use some nice Object orientation here, making one class for Volunteer and storing in it a string with the name and an array of int's for those hours.

(PS on that: Try searching about getters and setters later. It's common practice to declare class variables with private scope and define methods to access them, but I used them with public scope here just to keep the example short)

public class Volunteer {

    public String name;
    public int[] hours;

    Volunteer(String name, int[] hours) {
        this.name = name;
        this.hours = hours;
    }
}

Third of all, if you really want to do it like you're doing, I think you should probably use a Map.

Map<String, Integer[]> volunteersHours = new HashMap<String, Integer[]>();
volunteersHours.put("John", new Integer[] {1, 2, 3, 4, etc...});

volunteersHours.get("John"); // will return the hours array

Comments

-1

If you insist on using a String array you will need to use the toString function of the Integer class.

E.g.

Integer.toString(42);

However, this will cause pain when trying to perform Math operations on them later.

2 Comments

I need the first index of the each array ( array[0][0],array[1][0]...etc) to be a name thus making me declare it as a string array.
Ok I missed that. You still want to use an integer array though. Putting a bunch of ints you wish to do math operations on in a String array will cause pain down the line. So using a Map as suggested in the other answers is probably the way to go here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.