Am I using Scanner and File class correctly? - java

I am creating a Game of Life program that accepts user input patterns, using java.util.Scanner and java.io.File
The main issue is that I cannot seem to get the program to read the pattern.txt file...
I do not see any issue, the pattern.txt is in the same folder as the .java and .class files when I compile them.
Am I using File and Scanner correctly?
I've tried reordering the import statements, changing try & catch structure, and creating a new File(//..Path/../pattern.txt) to directly call the file via the relative path
1.txt is the pattern file:
20 20
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
`
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
public static void main(String [] args) throws FileNotFoundException{
String inputFileName = "1.txt"; // Directly setting relative path
int [][] initStates = getInitialStates(inputFileName);
Grid gd = new Grid(initStates);
gd.display();
gd.getStats();
gd.run();
}
public static int [][] getInitialStates(String fileName) {
try {
File file = new File(fileName);
// checks to see if file was created
// if (file.exists())
// System.out.println("FILE EXISTS");
// else
// System.out.println("FILENOTFOUDN");
Scanner input = new Scanner(file); // Create scanner to read file
int[][] states = null;
int rows = input.nextInt(); // get rows and cols values from first values of text file
int cols = input.nextInt();
// states = new int[rows][cols]; // Create 2d array w/ rows and cols
states = new int[rows][cols];
// for (int i = 0; i < rows; i++) {
// states[i] = new int[cols];
// }
// Read initial states from the input file
for (int i = 0; i < states.length; i++) {
for (int j = 0; j < states[0].length; j++) {
states[i][j] = input.nextInt();
}
}
return states;
} catch (FileNotFoundException e) {
e.printStackTrace();
return null;
}
// return states;
}
`
Error output is FileNotFoundException printing the stack trace:
java.io.FileNotFoundException: 1.txt (No such file or directory)
at java.base/java.io.FileInputStream.open0(Native Method)
at java.base/java.io.FileInputStream.open(FileInputStream.java:219)
at java.base/java.io.FileInputStream.<init>(FileInputStream.java:157)
at java.base/java.util.Scanner.<init>(Scanner.java:639)
at client.getInitialStates(client.java:67)
at client.main(client.java:24)
Exception in thread "main" java.lang.NullPointerException
at Grid.<init>(Grid.java:14)
at client.main(client.java:26)
I get the NullPointerException because the getInitialStates() method isn't returning the 2d array because it cannot read the file.

If as per comment a FileNotFoundException is thrown, it means that the working directory of your program (i.e. directory where java YourMainClassName is invoked) is different from location of the file you're trying to open by its name.
You can check the working directory:
System.out.println("Working Directory = " + System.getProperty("user.dir"));
As described in this answer: https://stackoverflow.com/a/7603444/1083697

Related

How to change the values of a multidimensional array in a range?

I want to change the values of a 2d array from a given starting position (rowPos and colPos) for a certain amount of rows and columns.
So far I have the code below:
int[][] block = new int[10][10];
int rowPos = 3, colPos = 3;
int rows = 4, columns = 4;
for (int i = rowPos; i < rows; i++)
for (int j = colPos; j < columns; j++)
block[i][j] = 1;
for (int[] x : block) {
for (int y : x)
System.out.print(y + " ");
System.out.println();
}
However this gives me the following output:
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 1 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
It is only setting the value at rowPos and colPos. This is my expected output:
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 1 1 1 1 0 0 0
0 0 0 1 1 1 1 0 0 0
0 0 0 1 1 1 1 0 0 0
0 0 0 1 1 1 1 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
I feel like I'm close but missing something small, please help me!
Change the loops to this:
for (int i = rowPos; i < rows + rowPos; i++)
for (int j = colPos; j < columns + colPos; j++)
block[i][j] = 1;
In your code, you let i and j start at the row and column you want to begin changing the values. That's fine, but it also means your condition doesn't work anymore. Let me show you what happens:
You declare i and set it's value to 3.
You check if i (3) is less than rows (4). Success!
You declare j and set it's value to 3.
You check if j (3) is less than columns (4). Success!
You set block[3][3] to 1.
j is incremented by 1 and is now 4.
You check if j (4) is less than columns (4). Fail!
i is incremented by 1 and is now 4.
You check if i (4) is less than rows (4). Fail!
You print the array's contents.
The problem is that because you start i and j at values greater than 0, you need to keep that in mind when performing the check.

how to store data input best java

i communicate to ilog via java and get a textfile output which looks like this:
18
3
PRODUKT:1:RESSOURCE:1:0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ;
RESSOURCE:2:0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0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1:0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ;
RESSOURCE:2:0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ;
RESSOURCE:3:0 0 0 0 0 0 10 0 0 0 0 0 0 0 0 12 0 0 0 0 0 0 0 0 0 0 23 0 0 0 0 0 0 0 0 0 0 0 0 0 ;
and etc.
i want to turn one row into one int[];
and then put this int[] fields into a map or something.
So there would be a map with keys like "Produkt:4Ressource:1" and values with the int[];
i just want to know which is the best collection for me to store data like this?
Map<"String",int[]> is that even possible? or do i need an object class which contains an []? Maybe thats better way of coding. Not sure.
Hashmap would be a good option to store. You can retrieve the values from it when you need.

Problems adding to two dimensional list in java

I don't understand why this cannot work, if anyone could help that would be great;
for(int i = 0; i < 10 ; i++){
lines = fileL[i];
for(int j = 0; j < lines.length(); j++){
enemySpawningL[i][j] = fileL[i].substring(j*2, 1);
}
}
where enemySpawning[][] has been set as a string and fileL is set as this;
private String[] fileL = {
"1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1",
"1 0 0 0 0 1 0 0 0 0 1 0 0 2 0 1 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1 0 2 0 0 1 0 0 0 0 1 0 0 0 0 1",
"1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1",
"1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1",
"1 0 0 0 0 1 0 0 2 0 1 0 0 0 0 1 0 2 0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 2 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 2",
};
(I tried using a textfile for it, but I didn't think there was a point cause it's not too important)
EDIT: I'm just trying to make the enemySpawning list be [line number][number in line]
EDIT2: Also the error I'm getting is java.lang.NullPointerException
The error is probably because you didn't initialize enemySpawningL.
String[][] enemySpawningL = new String[rowsNum][columnsNum];
And if I understand correctly what you are trying to do change your code to
for(int i = 0 ; i < 10 ; ++i) {
String[] digits = fileL[i].split(" ");
for(int j = 0 ; j < digits.length ; ++j) {
enemySpawningL[i][j] = digits[j];
}
}
That will give you one digit in every cell. Currently your substring() is wrong as the start point is bigger than the end point for j > 0 and if you change the order you insert string with several digits every time.
"1"
"1 0"
"1 0 0"
....
The problem is, as #guy mentioned, not initializing the array, but there is also another error in here:
enemySpawningL[i][j] = fileL[i].substring(j*2, 1);
Because 'j' can have maximal value of fileL[i] length, if you request index j*2, in half of the loop it will be out of bound. You are also using the substring method in incorrect way. How you want you loop to look like is:
for(int j = 0; j < lines.length(); j += 2){
enemySpawningL[i][j] = fileL[i].substring(j, j+1);
}
The j variable is now incremented by two every iteration (to skip spaces) and substring will return one character from position j of the string file[L]. Which is what you wanted.

BufferedReader Not Reading Correctly

For some reason, When using my buffered reader to read a .mcf (Map Configuration File), It will not correctly find the characters. This is what I'm using for testing purpose.
RendererPanel.java:
File file = new File("src/net/PlatformPeril/resources/Railroad.mcf");
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(file));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
String line;
try {
while ((line = br.readLine()) != null) {
if(line.equalsIgnoreCase("0")){
System.out.println("Air Block!");
}else if(line.equalsIgnoreCase("1")){
System.out.println("Rock Block!");
}else if(line.equalsIgnoreCase(" ")){
System.out.println("Empty Line Char; Ignoring");
continue;
}else System.out.println("What!");
}
} catch (IOException e) {
e.printStackTrace();
}
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
MCF:
1 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0
0 1 1 0 1 0 1 1 1 1 0 0 1 0 0 0 1 1 0 1 0 1 0 1 1 0 1
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
And, I'm getting alot of 'What!'....
Any idea of what I'm doing wrong?
You are comparing the entire line when you should be comparing the first token, look at string.split to help
This line.equalsIgnoreCase(" ") may cause problems, try line.trim().isEmpty() instead
Let the file contains the following-
0
1
0
1
consider a blank line
1
The below program works fine, same as yours but with certain refinements.
package com.kvvssut.misc;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class BufferReaderTester {
public static void main(String[] args) {
processFile("C:\\Users\\srimanta_sahu\\Desktop\\MyText.mcf");
}
private static void processFile(String string) {
BufferedReader bufferedReader = null;
FileReader fileReader = null;
try {
fileReader = new FileReader(new File(string));
bufferedReader = new BufferedReader(fileReader);
String currentLine;
while ((currentLine = bufferedReader.readLine()) != null) {
currentLine = currentLine.trim();
if(currentLine.equals("0")){
System.out.println("Air Block!");
} else if (currentLine.equals("1")){
System.out.println("Rock Block!");
} else if(currentLine.equals("")){
System.out.println("Empty Line Char; Ignoring");
}else {
System.out.println("What!");
}
}
} catch (FileNotFoundException fileNotFoundException) {
fileNotFoundException.printStackTrace();
} catch (IOException ioException) {
ioException.printStackTrace();
} finally {
if (bufferedReader != null) {
try {
bufferedReader.close();
fileReader.close();
} catch (IOException ioException) {
ioException.printStackTrace();
}
}
}
}
}
Plz dont use unnecessary equalsIgnoreCase for checking integers and dont use continue if it doesnt have to do anything!

Java JPG DCT coefficients alter by themselfs after I modify them

I have a program that changes the DCT coefficients of a JPG image.
This is the code that gives me the DCT coefficients
public int[] quantizeBlock(double inputData[][], int code) {
int outputData[] = new int[blockSize * blockSize];
int i, j;
int index;
index = 0;
for (i = 0; i < 8; i++) {
for (j = 0; j < 8; j++) {
// The second line results in significantly better compression.
outputData[index] = (int) (Math.round(inputData[i][j]
* (((double[]) (Divisors[code]))[index])));
// outputData[index] = (int)(((inputData[i][j] * (((double[])
// (Divisors[code]))[index])) + 16384.5) -16384);
index++;
}
}
return outputData;
}
This is a DCT matrix before modifications
-43 7 0 0 0 0 0 0
-8 1 2 -1 0 0 0 0
-1 -1 -1 1 0 0 0 0
-2 1 0 -1 0 0 0 0
6 0 0 0 0 0 0 0
-2 0 1 0 0 0 0 0
-1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
This is after the modifications
-42 8 0 0 0 0 0 0
-7 1 3 0 0 0 0 0
0 0 0 1 0 0 0 0
-1 1 0 0 0 0 0 0
7 0 0 0 0 0 0 0
-1 0 1 0 0 0 0 0
0 0 0 0 0 0 0 0
After I save the image using image Buffer,I use the created image to get back the modified DCT from it but all I get is:
-41 9 0 0 0 0 0 0
-6 1 4 0 0 0 0 0
0 0 0 1 0 0 0 0
0 1 0 0 0 0 0 0
8 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
I've seen a question where the user using a library in IOS did the same thing and had the same problem.Apparently the library recopressed the image and the hidden message was destroyed.
I don't know if this is the case for me.I use Image Buffer to create the image.
A couple things off the top that could be happening. The first is rounding errors. The JPEG process introduces small errors. All your values are one off. This could come from rounding.
The second is quantization. Your values may be quantized (divided). Your example does not indicate the compression stages that may be taking place in between your examples.

Categories

Resources