Click to See Complete Forum and Search --> : Arrays
Jesdisciple
10-24-2006, 09:32 PM
I'm trying to write a code to read a file (done), count lines in a block until the last line of the block (all last lines are uniform) then take a substring of the last line to append to the previous lines and move to the next block. I have experience with JavaScript and recognized that I would use arrays for this type of thing in that language, so I typed Array into JCreator and got the JDK Help on it.
The JDK Helps don't tend to be very comprehensive so this didn't help me much, but I have figured out how to give the JVM the idea of something being and array. My problem comes with an "incompatible types" error (followed by 7 other errors) for the array's instantiation line. Following is the entire code (a large block is commented because I was trying the thing above it).
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class Read{
public static void main(String[]args){
BufferedReader reader;
String text;
String append;
int colon;
int count1=0;
int count2=0;
String that;
try{
reader=new BufferedReader(new FileReader("file.txt"));
text=reader.readLine();
that=new String[50];
while((text=reader.readLine())!=null){
if(text==""){
count1++;
}else{
if(text.indexOf(": ")==-1){
that[count1][count2]=text;
count2++;
}else{
colon=text.indexOf(":");
that[count1][append]=text.substring(colon+1);
}
}
}
while(that[count1]!=-1){
while(that[count1][count2]!=-1){
that[count1][count2]+=that[count1][append];
System.out.println(that[count1][count2]);
count2++;
}
count1++;
}
// while((text=reader.readLine())!=null){
// colon=text.indexOf(":");
//
// that=new String[count];
//
// if(text.indexOf(": ")==-1 && colon!=-1){
// that[count]=text;
// count+=1;
// }else if(text.indexOf(": ")!=-1){
// append=text.substring(colon+1);
// for(int i=0;i<count;i++){
// that[i]+=append;
// }
// }else if(text.indexOf(":")==-1){
// count=0;
// }
//
// for(int i=0;i<count;i++){
// System.out.println(that[i]);
// }
// }
reader.close();
}catch(FileNotFoundException e){
System.out.println("file not found");
}catch(IOException e){
System.out.println("i/o");
}
}
}
Along with the syntax error for the array, I'm not sure where to start the second array (the list of lines inside each block).
chazzy
10-24-2006, 09:59 PM
first thing that jumps out at me is this:
String that;
try{
reader=new BufferedReader(new FileReader("file.txt"));
text=reader.readLine();
that=new String[50];
you're casting from String to String[], which isn't allowed. you should define that to be an array in the beginning with String[] that, instead of just String.
then further down your code you have
that[count1][count2]=text; which is now trying to cast that into a 2 dimensional string array. so which is it you want? a string, string[] or string[][]?
Jesdisciple
10-26-2006, 06:23 PM
Thanks for that! You got me past the 2D array announcement but I have no idea why I'm having this new problem (after the example).
I am wanting the 2-dimensional, but I thought I had to make an index into a new array, not make the original array 2-dimensional. I fixed the array problem so the program indexes each block and each line of each block, but now I have to reference each line previous to its block's last line to append the substring.
FYI, each block's last line contains a colon followed by the substring to be appended (which begins with a space), while each previous line ends with the colon. The first block begins on the first line and each subsequent block is separated from the previous by an empty line. Example:
first line:
second line:
third line:
last line: first block
first line:
second line:
third line:
fourth line:
last line: second block
becomes
first line: first block
second line: first block
third line: first block
last line: first block
first line: second block
second line: second block
third line: second block
fourth line: second block
last line: second block
I editted the code so that I thought it would work, and I got past compilation without errors, but the Command Prompt gives me an ArrayIndexOutOfBoundsException on line 40 (read[count1][count2]=text;) with a value equal to the second dimension's maximum. Following is the code so far.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class Read{
public static void main(String[]args){
BufferedReader reader;
String text;
String append;
int colon;
int count1=0;
int count2=0;
String[][] read;
try{
reader=new BufferedReader(new FileReader("file.txt"));
text=reader.readLine();
// read[x][10] is always the last line's
// substring to be appended to previous lines.
read=new String[100][12];
while((text=reader.readLine())!=null){
if(text==""){
count1++;
for(int i=0;i<count2;i++){
read[count1][count2]+=read[count1][10];
}
for(int i=0;i<count1;i++){
for(int j=0;j<count2;j++){
System.out.println(read[count1][count2]);
}
System.out.println("");
}
}else{
read[count1][count2]=text;
if(text.indexOf(": ")==-1){
count2++;
}else{
colon=text.indexOf(":");
read[count1][10]=text.substring(colon+1);
}
}
}
reader.close();
}catch(FileNotFoundException e){
System.out.println("file not found");
}catch(IOException e){
System.out.println("i/o");
}
}
}
chazzy
10-26-2006, 07:34 PM
your indexes start at 0 and go to less than the maximum
so
String[100][10] str;
for(int i=0;i<100;i++){
for(int j=0;j<10;j++){
System.out.println(str[i][j]);
}
}
you're incrementing your counters before they read. i think in this situation you want to do the incrementing afterwards.
Jesdisciple
10-26-2006, 09:06 PM
I'm not understanding your last post. I know array indices are ranged 0-x, but how am I incrementing my counters before they read? The second dimension's maximum is 12 and none of my blocks approach this number of lines, so it's not one more than I intend on the last cycle (in case that's what you're saying).
chazzy
10-26-2006, 09:36 PM
your code looks like this
while((text=reader.readLine())!=null){
if(text==""){
count1++;
for(int i=0;i<count2;i++){
read[count1][count2]+=read[count1][10];
}
which means that the very first value of count1 is 1, not 0. it should be 0. move the increment of count1++ to after these for loops.
At least, that's my impression of this.
Also, since text is a string, you want text.equals("") instead of ==. !=null still works though.
now that i'm thinking about it, this is likely your error. try this first, then the moving of the incrementer if it still isn't working.
Jesdisciple
10-26-2006, 09:54 PM
I moved count1++; and the error remained (although it might still have helped an unseen problem) but, when I changed '=="" ' to '.equals(""),' it displayed millions of copies of some values, ending with read[8][10] and displaying the same error. It deleted all values before read[8][10] to make room so I don't know what those values were.
chazzy
10-27-2006, 07:03 AM
to be honest, and maybe it's something i'm missing while reading your code, i don't get why you do this:
for(int j=0;j<count2;j++){
System.out.println(read[count1][count2]);
}
so let's say that count2=5
that means that this line will print out, the exact same thing, 5 times in a row. is that the desired effect?
Jesdisciple
10-27-2006, 08:22 AM
Oops. No, that's supposed to be read[i][j] to reference the lines to be appended to.
It now displays the appendix and "null" around each block, generally imitating a right triangle with the hypotenuse reaching upward and right from the far left but, while the display is consistent (not to mention it's Java) and so there must be logic to it, I find no relation between one block and the next except they get larger as they go down. (Placements of the two strings, "null" and the appendix, are erratic from one block to the next.) The first two triangles are purely repetitions of "null" and independent of the blocks.
EDIT: The ArrayIndexOutOfBoundsException persists. It is displayed after all of the blocks and triangles. Also, only blocks 1-7 (read[0][] through read[6][]) are displayed.
EDIT_2: I decided to write the output to a file as well as the command prompt so the new code is below. file2.txt fails to receive the data, so I'm guessing the ArrayIndexOutOfBoundsException is interfering.
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class Read{
public static void main(String[]args){
BufferedReader reader;
BufferedWriter writer;
String text;
String append;
int colon;
int count1=0;
int count2=0;
String[][] read;
try{
reader=new BufferedReader(new FileReader("file.txt"));
writer=new BufferedWriter(new FileWriter("file2.txt"));
text=reader.readLine();
// read[x][10] is always the last line's
// substring to be appended to previous lines.
read=new String[100][12];
while((text=reader.readLine())!=null){
if(text.equals("")){
count1++;
}else{
if(text.indexOf(": ")==-1){
read[count1][count2]=text;
count2++;
}else{
colon=text.indexOf(":");
read[count1][10]=text.substring(colon+1);
for(int i=0;i<count1;i++){
for(int j=0;j<count2;j++){
read[i][j]+=read[i][10];
System.out.println(read[i][j]);
writer.write(read[i][j]);
writer.newLine();
}
System.out.println("");
writer.newLine();
}
}
}
}
reader.close();
writer.flush();
writer.close();
}catch(FileNotFoundException e){
System.out.println("file not found");
}catch(IOException e){
System.out.println("i/o");
}
}
}Thanks again!
Jesdisciple
10-27-2006, 08:50 PM
Apparently, the while((text=reader.readLine())!=null) loop is, for some reason, acting like a while([something]<[arrayDim1]*[arrayDim2]) loop or something similar. I changed read[100][12] to read[100][100] and the prompt went on its way to those 10,000 somethings so I was able to close it before it reached the ArrayIndexOutOfBoundsException, which causes the BufferedWriter's output to be null. I decided to spare you from the entirety of the massive output, but the output I was receiving with read[100][12] is below. (The strings are Latin and Greek roots and modern synonyms.)nullnull
nullnull
nullnull
nullnullnull
nullnullnull
nullnullnull
nullnull
nullnull
cour: course
cur: course
curr: course
null course
null course
nullnullnullnull
nullnullnullnull
nullnullnullnull
nullnullnull
nullnullnull
nullnull
cour: course course
cur: course course
curr: course course
null course course
null course course
null course
null say
null say
null say
dic: say
dict: say
null say
nullnullnullnullnull
nullnullnullnullnull
nullnullnullnullnull
nullnullnullnull
nullnullnullnull
nullnullnull
nullnull
cour: course course course
cur: course course course
curr: course course course
null course course course
null course course course
null course course
null course
null say say
null say say
null say say
dic: say say
dict: say say
null say say
null say
null teach
null teach
null teach
null teach
null teach
doc: teach
null teach
nullnullnullnullnullnull
nullnullnullnullnullnull
nullnullnullnullnullnull
nullnullnullnullnull
nullnullnullnullnull
nullnullnullnull
nullnullnull
nullnull
cour: course course course course
cur: course course course course
curr: course course course course
null course course course course
null course course course course
null course course course
null course course
null course
null say say say
null say say say
null say say say
dic: say say say
dict: say say say
null say say say
null say say
null say
null teach teach
null teach teach
null teach teach
null teach teach
null teach teach
doc: teach teach
null teach teach
null teach
null idea
null idea
null idea
null idea
null idea
null idea
dog: idea
null idea
nullnullnullnullnullnullnull
nullnullnullnullnullnullnull
nullnullnullnullnullnullnull
nullnullnullnullnullnull
nullnullnullnullnullnull
nullnullnullnullnull
nullnullnullnull
nullnullnull
nullnull
cour: course course course course course
cur: course course course course course
curr: course course course course course
null course course course course course
null course course course course course
null course course course course
null course course course
null course course
null course
null say say say say
null say say say say
null say say say say
dic: say say say say
dict: say say say say
null say say say say
null say say say
null say say
null say
null teach teach teach
null teach teach teach
null teach teach teach
null teach teach teach
null teach teach teach
doc: teach teach teach
null teach teach teach
null teach teach
null teach
null idea idea
null idea idea
null idea idea
null idea idea
null idea idea
null idea idea
dog: idea idea
null idea idea
null idea
null suitable
null suitable
null suitable
null suitable
null suitable
null suitable
null suitable
dec: suitable
null suitable
nullnullnullnullnullnullnullnull
nullnullnullnullnullnullnullnull
nullnullnullnullnullnullnullnull
nullnullnullnullnullnullnull
nullnullnullnullnullnullnull
nullnullnullnullnullnull
nullnullnullnullnull
nullnullnullnull
nullnullnull
nullnull
cour: course course course course course course
cur: course course course course course course
curr: course course course course course course
null course course course course course course
null course course course course course course
null course course course course course
null course course course course
null course course course
null course course
null course
null say say say say say
null say say say say say
null say say say say say
dic: say say say say say
dict: say say say say say
null say say say say say
null say say say say
null say say say
null say say
null say
null teach teach teach teach
null teach teach teach teach
null teach teach teach teach
null teach teach teach teach
null teach teach teach teach
doc: teach teach teach teach
null teach teach teach teach
null teach teach teach
null teach teach
null teach
null idea idea idea
null idea idea idea
null idea idea idea
null idea idea idea
null idea idea idea
null idea idea idea
dog: idea idea idea
null idea idea idea
null idea idea
null idea
null suitable suitable
null suitable suitable
null suitable suitable
null suitable suitable
null suitable suitable
null suitable suitable
null suitable suitable
dec: suitable suitable
null suitable suitable
null suitable
null lead
null lead
null lead
null lead
null lead
null lead
null lead
null lead
duc: lead
null leadThe file being read iscred: trust
cour:
cur:
curr:
curs: course
dic:
dict:
dit: say
doc:
doct: teach
dog:
dox: idea
dec:
dign: suitable
duc:
duct: lead
ev:
et: time
fac:
fact:
fec:
fic:
fas:
fea: do
fer: bear
fict:
feign:
fain: make
fid: faith
fig: image
flu:
fluct:
flux: flow
form: image
fract:
frag:
frai: break
gen:
gin: birth
geo: earth
gor: to gather
grad:
gress:
gree: step
graph:
graf: draw
her:
hes: to stick
jac:
ject:
jet: to throw
jug:
junct:
just: to join
lex:
leag:
leg: law
lect:
leg:
lig: choose
loc: place
log: reason
luc:
lum:
lust: light
man: do
mem: recall
ment: mind
min: small
mit:
miss: send
mob:
mov:
mot: move
nasc:
nat:
gnant:
nai: to be born
nom:
nym: name
nov: new
oper: work
pat:
pass: feel
path: feel
ped: foot
pod: foot
pel:
puls: drive
pend:
pond: to hang
phan:
phas:
phen:
fan:
phant:
fant: show
phil: love
phon: sound
pict: depict
port: carry
pli:
ply: fold
pon:
pos: to place
psych: mind
quir:
quis:
quest:
quer: seek
rupt: break
sci:
scio: to know
scrib:
scrip: write
sent:
sens: think
sequ:
secut:
sue: follow
sist: to stand up
soci: (to ac)company
sol: alone
solv:
solu:
solut: loosen
spec:
spi:
spic:
spect: look
spir: breath(e)
stab:
stat: stand
strain:
strict:
string:
stige: pull
stru:
struct:
stroy: build
tact:
tang:
tig:
ting: touch
tele: distant
tend:
tens: stretch
tain:
ten:
tent:
tin: have
term: limit
terr: earth
test: see
therm: heat
tor:
tors:
tort: twist
tract:
trai:
treat: attract
uni: one
vac: empty
ven:
vent: come
ver: true
verb:
verv: word
vers:
vert: change
vid:
vie:
vis: see
vit:
viv: live
voc:
voke: call
volv:
volt:
vol: turnYou might have noticed that the blocks aren't in triangles. This changed when I noticed I hadfor(int i=0;i<count2;i++){
for(int j=0;j<count1;j++){
...
}
}instead offor(int i=0;i<count1;i++){
for(int j=0;j<count2;j++){
...
}
}