Reputation: 29
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;
}
Upvotes: 0
Views: 1719
Reputation: 24354
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.
Upvotes: -1
Reputation: 1518
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
Upvotes: 0
Reputation: 5694
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, List
s 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);
}
Upvotes: 0
Reputation: 1890
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
Upvotes: 2
Reputation: 1961
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>>();
Upvotes: 1