Writing file input in a matrix
Hey everyone, I'm new to this forums so I don't really know if this is a beginner or an advanced question....
For a school assignement I need to get input from a file(a drawing) and put it in a matrix so that i can edit it. However, I created a code which should do the trick but doesn't... It stops at around halve the height of the file.... can someone please tell me what I do wrong?
Here is the code:
Code:
public class Landscape {
public void getLandscape() {
try {
FileInputStream in = new FileInputStream("YourFile");
BufferedReader br = new BufferedReader(new InputStreamReader(in));
Scanner scanner = new Scanner("Yourfile");
int height=0, width=0;
String currentLine;
while (scanner.hasNextLine()){
height++;
currentLine = scanner.nextLine();
width = Math.max(width,currentLine.length());
}
char[][] myArray;
myArray = new char[height][width];
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
myArray[i][j] = (char)br.read();
}
}
for(char[] rij: myArray){
for (char c: rij){
System.out.print(c); }
System.out.println();
}
in.close();
} catch (Exception e) {
System.err.println("Error: " + e.getMessage());
}
}
}
Thanks in advance
greetings,
Neo
Re: Writing file input in a matrix
Are all lines the same length (ie width)?
Re: Writing file input in a matrix
Don't think so, this is the drawing I'm using, it only displays the first 4 rows, (starting from the top), but at the forth row it suddenly stops.
Code:
#
#####
#######
######### #
############ ##
################ ###
B #################### #### A
##########################################################
Re: Writing file input in a matrix
Quote:
It stops at around halve the height of the file.
What does "it stops" mean?
If there are messages printed out, copy and paste them here.
Your catch block should include a call to printStackTrace() while you are debugging the code. The printed trace is useful in tracking down errors.
Re: Writing file input in a matrix
well, I don't get any errors, when I execute my file this is my output:
Code:
#
#####
#######
#########
So, my thought is, it just doesn't read the whole file?
Re: Writing file input in a matrix
What is this supposed to do?
Code:
while (scanner.hasNextLine()){
height++;
currentLine = scanner.nextLine();
width = Math.max(width,currentLine.length());
}
Re: Writing file input in a matrix
Do some debugging by adding some printlns to show the logic flow as the code executes.
Are you trying to have two classes reading from the same file?
What does the Scanner read? Print out what it reads.
Re: Writing file input in a matrix
You have created a two dimensional array based on the number of lines in the file and the MAXIMUM line width.
You then populate each "cell" in the array with a read() from the file.
If not all lines are the maximum width then you will not fill the array.
Using your code:
1234567
123456
12345
1234
123
12
1
will become (in the array):
1234567
1234561
2345123
412312.
.......
.......
.......
where the dots are blanks.
Now, from your output this does not seem to be the case.
Anyway, I'd print out the values of height and width just before you create the array and compare that to what you would expect.
Re: Writing file input in a matrix
Quote:
Originally Posted by
diamonddragon
What is this supposed to do?
Code:
while (scanner.hasNextLine()){
height++;
currentLine = scanner.nextLine();
width = Math.max(width,currentLine.length());
}
That was supposed to calculate the height and width from the file.
but I've done some debugging now and i get these resulst:
height :1
width:125
so doesnt seem to work :s
Re: Writing file input in a matrix
Quote:
Originally Posted by
Neovenator
That was supposed to calculate the height and width from the file.
but I've done some debugging now and i get these resulst:
height :1
width:125
so doesnt seem to work :s
And what this supposed to do?
Code:
Scanner scanner = new Scanner("Yourfile");
Re: Writing file input in a matrix
Quote:
Originally Posted by
Norm
Do some debugging by adding some printlns to show the logic flow as the code executes.
Are you trying to have two classes reading from the same file?
What does the Scanner read? Print out what it reads.
Thanks for that, My height and width are wrong, my height is 1 and my width is 125 :s
and i know i'm doing double work by using a scanner AND a fileinputstream, but i just couldnt get it to work without using them both. I use the scanner to calculate the height and width and the bufferreader to print the chars in my matrix.
Re: Writing file input in a matrix
Quote:
Originally Posted by
diamonddragon
And what this supposed to do?
Code:
Scanner scanner = new Scanner("Yourfile");
It's used to read in a file in the "scanner" variable.
Re: Writing file input in a matrix
Did you try printing out what was read by the Scanner class?
Re: Writing file input in a matrix
Quote:
Originally Posted by
Neovenator
It's used to read in a file in the "scanner" variable.
Does it?
Re: Writing file input in a matrix
Quote:
Originally Posted by
Norm
Did you try printing out what was read by the Scanner class?
ow yeah, I just got this:
Code:
java.util.Scanner[delimiters=\p{javaWhitespace}+][position=0][match valid=false][need input=false][source closed=false][skipped=false][group separator=\.][decimal separator=\,][positive prefix=][negative prefix=\Q-\E][positive suffix=][negative suffix=][NaN string=\Q�\E][infinity string=\Q∞\E]
which I can't make any sense of :s
Re: Writing file input in a matrix
Quote:
Originally Posted by
diamonddragon
Does it?
I suppose not, trying to figure out how to fix that atm.
Re: Writing file input in a matrix
Quote:
Originally Posted by
Neovenator
ow yeah, I just got this:
Code:
java.util.Scanner[delimiters=\p{javaWhitespace}+][position=0][match valid=false][need input=false][source closed=false][skipped=false][group separator=\.][decimal separator=\,][positive prefix=][negative prefix=\Q-\E][positive suffix=][negative suffix=][NaN string=\Q�\E][infinity string=\Q∞\E]
which I can't make any sense of :s
And what this supposed to do?
Code:
FileInputStream in = new FileInputStream("YourFile");
Re: Writing file input in a matrix
Quote:
Originally Posted by
diamonddragon
And what this supposed to do?
Code:
FileInputStream in = new FileInputStream("YourFile");
it reads that same file in the varibale 'in'. I use two different ways of reading the file because they both have other properties which I need, I'm sure I can do it with just one fo the two methods but I haven't figured out how to do that either.
Re: Writing file input in a matrix
What was the variable that you printed out?
The Scanner class is used to read a String into a variable: currentLine
What was the contents of that variable after you read something into it using the Scanner class?
Re: Writing file input in a matrix
Quote:
Originally Posted by
Neovenator
it reads that same file in the varibale 'in'. I use two different ways of reading the file because they both have other properties which I need, I'm sure I can do it with just one fo the two methods but I haven't figured out how to do that either.
Does it?
I thought it creates a new File instance.
My appologies, didn't see it is FileInputStream. :(