Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

constructor by reading from a file and array in class problem

807601May 30 2008 — edited May 30 2008
Ok so I have to make an overloaded constructor which takes input from this file called "scrabdata.txt"
this is the format of the file..
English
A 1
B 3
C 3
D 2
E 1
F 4
G 2
H 4
I 1
J 8
K 5
L 1
M 3
N 1
O 1
P 3
Q 10
R 1
S 1
T 1
U 1
V 4
W 4
X 8
Y 4
Z 10
#
Afrikaans
A 1
B 8
C 0
D 1
E 1
F 8
G 2
H 2
I 1
J 10
K 3
L 2
M 4
N 1
O 1
P 5
Q 0
R 1
S 1
T 1
U 4
V 5
W 3
X 0
Y 4
Z 0
#
Catalan
A 1
B 3
C 2
D 2
E 1
F 4
G 3
H 8
I 1
J 8
K 10
L 1
M 2
N 1
O 1
P 3
Q 8
R 1
S 1
T 1
U 1
V 4
W 10
X 10
Y 10
Z 8
#
Dutch
A 1
B 3
C 5
D 2
E 1
F 4
G 3
H 4
I 1
J 4
K 3
L 3
M 3
N 1
O 1
P 3
Q 10
R 2
S 2
T 2
U 4
V 4
W 5
X 8
Y 8
Z 4
#
Finnish
A 1
B 8
C 10
D 7
E 1
F 8
G 8
H 4
I 1
J 4
K 2
L 2
M 3
N 1
O 2
P 4
Q 7
R 4
S 1
T 1
U 3
V 4
W 0
X 0
Y 4
Z 2
#
French
A 1
B 3
C 3
D 2
E 1
F 4
G 2
H 4
I 1
J 8
K 10
L 1
M 2
N 1
O 1
P 3
Q 8
R 1
S 1
T 1
U 1
V 4
W 10
X 10
Y 10
Z 10
#
Italian
A 1
B 5
C 2
D 5
E 1
F 5
G 8
H 8
I 1
J 0
K 0
L 3
M 3
N 3
O 1
P 5
Q 10
R 2
S 2
T 2
U 3
V 5
W 0 
X 0
Y 0
Z 8
#
Malaysian
A 1
B 3
C 8
D 3
E 1
F 10
G 3
H 4
I 1
J 5
K 1
L 2
M 1
N 1
O 4
P 4
Q 0
R 1
S 2
T 1
U 1
V 0
W 8
X 0
Y 5
Z 10
#
Portuguese
A 1
B 3
C 2
D 2
E 1
F 4
G 4
H 4
I 1
J 5
K 3
L 2
M 1
N 3
O 1
P 2
Q 6
R 1
S 1
T 1
U 1
V 4
W 0
X 8
Y 0
Z 8
#
Romanian
A 1
B 9
C 1
D 2
E 1
F 8
G 9
H 10
I 1
J 10
K 0
L 1
M 4
N 1
O 1
P 2
Q 0
R 1
S 1
T 1
U 1
V 8
W 0
X 10
Y 0
Z 10
#
here is my class Scrabble with a default and an overloaded constructor
import java.util.Random;
import java.io.*;
import java.util.Scanner;


public class Scrabble
{
	// instance variables
	
	private int [][] wordValues = new int [10][26];
	private String [] alphabets = {"A","B","C","D","E","F","G","H","I",
	                               "J","K","L","M","N","O","P","Q","R",
											 "S","T","U","V","W","X","Y","Z"};
	private String [] languages = {"Lang1","Lang2","Lang3","Lang4",
											"Lang5","Lang6","Lang7","Lang8",
											"Lang9","Lang10"};
											
	// default constructor
	
	public Scrabble()
	{
	
	 Random random = new Random();
	 
		for(int i=0;i<wordValues.length;i++)
		{
			for(int j=0;j<wordValues.length;j++)
{
wordValues[i][j]= random.nextInt(11);
}

}
}

// overloaded constructor

public Scrabble(String filename)
{
try
{
File file = new File(filename);
Scanner scan = new Scanner(file);

int i = 0;

while(i < 10)
{
languages[i] = scan.nextLine();
for(int j=0;j<26;j++)
{
int num = scan.nextInt();
wordValues[i][j]= num;


}
String trash = scan.nextLine();
i++ ;

}
}

catch(FileNotFoundException fnfe){}

}

//accessor

public int[][] getWordValues()
{
int[][] temp = new int[10][26];

for(int i=0;i<wordValues.length;i++)
{
for(int j=0;j<wordValues[i].length;j++)
{
wordValues[i][j] = temp[i][j];
}
}
return temp;
}

}


and this is my main where I try to make a class Scrabble using the overloaded constructor, return the wordValues array to a temp array and try to print it
public class ScrabbleClient
{
public static void main(String[]args)
{
Scrabble scrab = new Scrabble();
int[][] temp =scrab.getWordValues();
for(int i=0;i<10;i++)
{
for(int j=0;j<26;j++)
{
System.out.println(temp[i][j]);
}
}


}
}

everything compiles and runs. the problem is that instead of printing the array values I put in from the file it just prints a lot of zeros... I need to complete this today so plz help..                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 27 2008
Added on May 30 2008
16 comments
260 views