This site requires JavaScript, please enable it in your browser!
Greenfoot back
someone1
someone1 wrote ...

2015/10/13

Need help with assigning variable in constructor

someone1 someone1

2015/10/13

#
Ok so i have my main class and i have a constructor My main method looks like this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public static void main(String[] args) throws IOException, FileNotFoundException {
   File inFile = new File ("Games.txt");
   Scanner file = new Scanner(inFile);
   String str;
   String[] space;
   while(file.hasNext()) {
     str = file.nextLine();
     space = str.split(",");
     for(int i=2; i < space.length; i++) {
       System.out.println(space[0]);
     
     }
   }
  
 }
In the games file there are 3 values on each line there are 12 lines The values are separated by a comma The values on one line go like this Game name, Developer Name, Game Rating This is my constructor
1
2
3
4
5
6
7
8
9
10
11
12
13
14
public class gameConst {
 
private String gameName;
private String gameDeveloper;
private String gameRating;
 
 
public gameConst(String gName, String gDev, String gRate) {
  gameName = gName;
  gameDeveloper = gDev;
  gameRating = gRate;
}
 
}
What i want is to assign the value of the array space in address 0 to the game name and space address 1 to game developer and space address 2 to game rating in the constructor, right now all i can do is print out the values, how would i do this
ElNo ElNo

2015/10/13

#
If I understand it right you want to get 12 objects of the class gameConst, each holding the values of one of the lines in your file as attributes. (By the way: note that classes and constructors should begin with an capital letter like GameConst!) First, I don't think you need the
1
for(int i=2; i < space.length; i++)
as the length of space will always be three and therefore your for will only run once. I would suggest to replace the part with your for and the println with the following methodcall of the constructor of your class gameConst giving it the threevalues of your array space as parameters:
1
2
3
4
5
while(file.hasNext()) {
     str = file.nextLine();
     space = str.split(",");
     gameConst(space[0], space[1], space[2]);
}
someone1 someone1

2015/10/13

#
Hi Thanks for the reply, the thing is, when i used ur code and used system print out to check the results only the last result was published, so only one line was shown since i split the string, but when i put it in the loop all the results i wanted was published
danpost danpost

2015/10/13

#
@ElNo, you need the 'new' keyword to create instances of the gameConst class; also, the gameConst objects created within your while loop are not being retained anywhere and each one is lost when the loop continues on its next cycle. A static array field might be something to consider:
1
2
3
4
5
6
7
8
9
10
11
// static array field
public static gameConst[] games;
// the while loop
int count = 0;
gameConst[] games = new gameConst[12];
while (file.hasNext() && count < 12) {
    str = file.nextLine();
    space = str.split(",");
    games[count] = new gameConst(space[0] space[1], space[2]);
    count++;
}
You need to login to post a reply.