Problems adding to two dimensional list in java - 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 0 1 0 0 0 0 1 0 0 2 0 1 0 0 0 0 1 0 2 0 0 1 0 0 0 0 1 0 2",
"1 0 0 0 1 0 2 0 1 0 0 0 1 0 2 0 1 0 0 0 1 0 2 0 1 0 0 0 1 0 2 0 1 0 0 0 1 0 2 0 1 0 0 0 1 0 2 0 1 0 0 0 1 0 2 0 1 0 0 0 1 0 2 0 1 0 0 0 1 0 2 0 1 0 0 0 1 0 2 0 1 0 0 0 1 0 2 0 1 0 0 0 1 0 2 0 1 0 0 0 1 0 2 0 1 0 0 0 1 0 2 0 1 0 0 0 1",
"1 0 3 0 1 0 2 0 1 0 0 0 1 0 2 0 1 0 3 0 1 0 2 0 1 0 0 0 1 0 2 0 1 0 3 0 1 0 2 0 1 0 0 0 1 0 2 0 1 0 3 0 1 0 2 0 1 0 0 0 1 0 2 0 1 0 3 0 1 0 2 0 1 0 0 0 1 0 2 0 1 0 3 0 1 0 2 0 1 0 0 0 1 0 2 0 1 0 3 0 1 0 2 0 1 0 0 0 1 0 2 0 1 0 3 0 1",
"1 0 0 0 2 0 0 0 1 0 0 0 3 0 0 0 1 0 0 0 2 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 3 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 2 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 3 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 2 0 0 0 1",
"2 0 0 3 0 2 0 0 3 0 2 0 0 3 0 2 0 0 3 0 2 0 0 3 0 2 0 0 3 0 2 0 0 3 0 2 0 0 3 0 2 0 0 3 0 2 0 0 3 0 2 0 0 3 0 2 0 0 3 0 2 0 0 3 0 2 0 0 3 0 2 0 0 3 0 2 0 0 3 0 2 0 0 3 0 2 0 0 3 0 2 0 0 3 0 2 0 0 3",
"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 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.

Related

Am I using Scanner and File class correctly?

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

Implementing Dijkstra's algorithm in Java?

So I'm trying to implement Dijkstra's algorithm in Java. I know there are different ways to do it but here's the way that I've learned to do it. So I start with a single vertex and I find the shortest path from that vertex to every other vertex. I start with a single vertex (in my case it's zero) and then update my neighborhood by relaxing all the edges connected to that vertex. I then find out what's the smallest edge that connects to the current edge and then I add that vertex to my vertex storage. I keep doing that until all the vertices are in my vertex storage and then I should end up with a type of spanning tree that shows me all the shortest paths.
So in my code, I'm trying to find the shortest path from 0 to 1. I get the graphs from an adjacency matrix. So what I do is I start at the first row (or the 0th row) and traverse through the row and relax the vertices in a matrix called N. I then find the smallest edge coming out of whatever vertices I have stored in my vertex storage and then contnue adding vertices and relaxing the edges. So then I have two main arrays called N (where I store the weights of the shortest paths) and edgeStorage. Here's what it looks like:
static int ShortestPath(int[][] G){
int numVerts = G.length;
int totalWeight = 0;
int minWeight;
int count = 1;
int k = 0;
int l = 0;
int next = 1;
int i = 0;
int[] N = new int[numVerts];
int[] edgeStorage = new int[numVerts];
Arrays.fill(N, 2147483647); //2147483647 is my infinity to represent vertices that haven't yet been relaxed
N[0] = 0;
while (count != numVerts){
for (int j = 0; j < numVerts; j++){
if ((G[i][j] != 0) && (N[i] + G[i][j] < N[j])){
N[j] = N[i] + G[i][j];
}
}
minWeight = 2147483647;
for (int p = 0; p < count; p++){ //find min edge weight for vertices in storage
i = edgeStorage[p];
for (int j = 0; j < numVerts; j++){
if ((G[i][j] != 0) && (G[i][j] < minWeight)){
minWeight = G[i][j];
k = j;
l = i;
}
}
}
G[l][k] = 0; //remove edge since we don't need it anymore
G[k][l] = 0;
edgeStorage[next] = k; //store vertex location in array
i = k;
count++;
next++;
}
totalWeight = N[1];
return totalWeight;
}
The problem is this code works for some graphs but for others it gives me a weight that's bigger than it's supposed to be. I tested it on a graph of 25 vertices which looked like this:
0 0 0 0 0 0 418 0 0 0 0 0 0 0 472 0 0 0 0 0 0 0 0 537 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 191 375 161 0
0 0 0 0 0 0 0 0 0 0 0 0 108 0 0 0 512 0 311 0 0 0 0 0 0
0 0 0 0 0 0 0 0 612 0 0 0 0 0 0 0 0 0 0 0 583 0 0 0 0
0 0 0 0 0 0 365 0 0 0 0 0 0 0 0 262 243 0 0 0 0 617 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 581 0 0 0 0 0 0 0 0 0 0 0
418 0 0 0 365 0 0 0 0 0 0 338 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 320 0 517 0 0 0 0 0 0 0 524 0 314 0 0 0 0
0 0 0 612 0 0 0 320 0 0 0 0 0 0 0 0 577 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 145 414 0 0 35 0 0 0 0 0 0 394 0 0
0 0 0 0 0 0 0 517 0 0 0 0 0 0 0 0 0 353 0 0 0 0 0 0 0
0 0 0 0 0 0 338 0 0 145 0 0 0 0 0 0 0 0 0 344 0 0 0 0 0
0 0 108 0 0 0 0 0 0 414 0 0 0 0 0 0 0 607 0 0 0 0 0 0 0
0 0 0 0 0 581 0 0 0 0 0 0 0 0 0 0 0 0 0 609 0 0 231 0 0
472 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 478 0 0 0 0 0 0 0
0 0 0 0 262 0 0 0 0 35 0 0 0 0 0 0 0 0 0 0 280 0 0 0 0
0 0 512 0 243 0 0 0 577 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 353 0 607 0 478 0 0 0 0 0 0 0 0 594 0
0 0 311 0 0 0 0 524 0 0 0 0 0 0 0 0 0 0 0 0 0 471 0 0 306
0 0 0 0 0 0 0 0 0 0 0 344 0 609 0 0 0 0 0 0 0 0 0 0 0
0 0 0 583 0 0 0 314 0 0 0 0 0 0 0 280 0 0 0 0 0 0 0 0 214
0 191 0 0 617 0 0 0 0 0 0 0 0 0 0 0 0 0 471 0 0 0 0 0 0
0 375 0 0 0 0 0 0 0 394 0 0 0 231 0 0 0 0 0 0 0 0 0 0 0
537 161 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 594 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 306 0 214 0 0 0 0
My N array looked like this:
0 1670 1538 1799 783 2107 418 1530 1603 901 2047 756 1315 1526 472 936 1026 950 1736 1100 1216 1400 1295 537 1430
and my edgeStorage array looked like this:
0 6 11 9 15 4 16 20 24 18 2 12 7 8 19 4 22 13 1 23 21 12 21 14 17
So therefore it returned 1670 as the path between 0 and 1 when it should've returned 698. I have no idea why it gives me the wrong weight for some graphs but the right weight for others. So does anyone know what's wrong with my code?
P.S: I know my implementation is not the most efficient thing right now but I just want to get the basic implementation working and then I'll work on making it more efficient.

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 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 16 0 0 0 0 0 0 0 0 0 0 0 0 0 0 10 0 16 0 0 0 0 0 6 0 0 0 0 0 0 0 0 0 0 0 0 ;
PRODUKT:2: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 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 13 0 0 0 0 3 0 0 0 0 0 0 0 0 0 0 13 20 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ;
PRODUKT:3: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 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 0 0 0 12 0 12 0 0 0 0 0 0 0 0 0 0 21 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 ;
PRODUKT:4: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 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 0 0 0 9 8 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 23 0 0 0 0 0 0 0 0 0 0 0 0 ;
PRODUKT:5: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 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.

Java counting connected regions in a grid

I am trying to locate all four-connected regions in the grid. A four-connected region consists of a set of marked cells (value 1) such that each cell in the region can be reached by moving up, down, left or right from another marked cell in the region. The assignment states that we should use recursion.
An example input would be:
10 20
0 1 1 0 0 0 1 0 1 1 0 0 0 0 1 0 0 0 1 0
0 1 1 1 0 1 1 0 0 1 0 0 1 1 1 0 0 0 1 1
0 0 1 0 0 0 1 1 0 1 0 0 0 1 0 0 0 1 1 1
1 0 0 0 0 0 0 0 1 1 0 0 1 0 0 0 1 1 1 0
1 1 0 1 0 0 0 1 1 1 0 0 0 1 1 0 1 1 0 0
1 1 1 1 0 0 0 0 0 0 1 0 1 1 1 0 0 0 0 0
0 1 1 1 0 0 0 1 1 1 1 0 0 1 1 0 1 0 0 0
0 1 1 1 1 1 0 0 1 1 1 0 0 0 1 1 1 1 1 0
0 0 0 1 1 0 0 0 0 1 0 0 0 1 1 0 0 1 1 0
1 0 1 1 1 1 1 0 0 0 0 0 0 0 1 1 0 1 0 0
And the output should be:
0 1 1 0 0 0 2 0 3 3 0 0 0 0 4 0 0 0 5 0
0 1 1 1 0 2 2 0 0 3 0 0 4 4 4 0 0 0 5 5
0 0 1 0 0 0 2 2 0 3 0 0 0 4 0 0 0 5 5 5
6 0 0 0 0 0 0 0 3 3 0 0 7 0 0 0 5 5 5 0
6 6 0 6 0 0 0 3 3 3 0 0 0 8 8 0 5 5 0 0
6 6 6 6 0 0 0 0 0 0 9 0 8 8 8 0 0 0 0 0
0 6 6 6 0 0 0 9 9 9 9 0 0 8 8 0 8 0 0 0
0 6 6 6 6 6 0 0 9 9 9 0 0 0 8 8 8 8 8 0
0 0 0 6 6 0 0 0 0 9 0 0 0 8 8 0 0 8 8 0
10 0 6 6 6 6 6 0 0 0 0 0 0 0 8 8 0 8 0 0
Right now when I run the code I have, I get this output:
0 2 2 0 0 0 2 0 2 2 0 0 0 0 2 0 0 0 2 0
0 2 2 2 0 2 2 0 0 2 0 0 2 2 2 0 0 0 2 2
0 0 2 0 0 0 2 2 0 2 0 0 0 2 0 0 0 2 2 2
2 0 0 0 0 0 0 0 2 2 0 0 2 0 0 0 2 2 2 0
2 2 0 2 0 0 0 2 2 2 0 0 0 2 2 0 2 2 0 0
2 2 2 2 0 0 0 0 0 0 2 0 2 2 2 0 0 0 0 0
0 2 2 2 0 0 0 2 2 2 2 0 0 2 2 0 2 0 0 0
0 2 2 2 2 2 0 0 2 2 2 0 0 0 2 2 2 2 2 0
0 0 0 2 2 0 0 0 0 2 0 0 0 2 2 0 0 2 2 0
2 0 2 2 2 2 2 0 0 0 0 0 0 0 2 2 0 2 0 0
My code looks like:
package project2;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Scanner;
public class project2 {
private static int height;
private static int length;
public static void main(String[] args) {
String inputFile;
Scanner input = new Scanner(System.in);
System.out.print("Enter input file name: ");
inputFile = input.nextLine();
try {
Integer grid[][] = loadGrid(inputFile);
System.out.println("Before flood fill");
printGrid(grid);
findGroups(grid, 0, 0, 2, height, length);
System.out.println("After flood fill");
printGrid(grid);
} catch (IOException e) {
System.err.println(e.getMessage());
}
}
public static void findGroups(Integer[][] array, int column, int row,
int counter, int height, int length) {
for (int i = 0; i < height; i++) {
for (int j = 0; j < length; j++) {
if (row < 0 || row >= length || column < 0 || column >= height) {
} else {
if (array[i][j] == 1) {
array[i][j] = counter;
findGroups(array, column, row + 1, counter, height, length);
findGroups(array, column, row - 1, counter, height, length);
findGroups(array, column - 1, row, counter, height, length);
findGroups(array, column + 1, row, counter, height, length);
counter++;
}
}
}
}
}
public static Integer[][] loadGrid(String fileName) throws IOException {
FileInputStream fin;
fin = new FileInputStream(fileName);
Scanner input = new Scanner(fin);
height = input.nextInt();
length = input.nextInt();
Integer grid[][] = new Integer[height][length];
for (int r = 0; r < height; r++) {
for (int c = 0; c < length; c++) {
grid[r][c] = input.nextInt();
}
}
fin.close();
return (grid);
}
public static void printGrid(Integer[][] grid) {
for (Integer[] grid1 : grid) {
for (int c = 0; c < grid[0].length; c++) {
System.out.printf("%3d", grid1[c]);
}
System.out.println();
}
}
}
I'm not sure what I am doing wrong, I believe I am moving the counter up after each time. Does anyone have a recommendation of what the issue might be?
Thanks in advance.

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