Writing 2d array into file that needs to look like array - java

I have the following code that creates and writes to the file but it writes everything out in one solid line.
Example: 11111111111111111111111111
I need it look like the following(space in between every number):
1 1 1 1 1 1
1 1 1 1 1 1
1 1 1 1 1 1
Here is my code:
public void saveFile()
{
String save = "Testing";
JFileChooser fc = new JFileChooser();
int returnVal = fc.showSaveDialog(null);
if (returnVal == JFileChooser.APPROVE_OPTION) {
try {
FileWriter bw = new FileWriter(fc.getSelectedFile()+".txt");
for(int row = 0; row < gameArray.length; row++)
{
for(int col =0; col < gameArray[row].length; col++)
{
bw.write(String.valueOf(gameArray[row][col]));
System.out.println(gameArray[row][col] + " ");
System.out.println();
}
}
bw.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}

This is because you are not putting space between them.
bw.write(String.valueOf(gameArray[row][col]));
bw.write(" ");
and at the end of inner loop, again,
bw.write("\n");

write bw.write("\n") at the end of your inner loop.
Just like :
for(int row = 0; row < gameArray.length; row++)
{
for(int col =0; col < gameArray[row].length; col++)
{
bw.write(String.valueOf(gameArray[row][col]));
System.out.println(gameArray[row][col] + " ");
System.out.println();
}
bw.write("\n")
}

This should work for you:
public void saveFile()
{
String save = "Testing";
JFileChooser fc = new JFileChooser();
int returnVal = fc.showSaveDialog(null);
if (returnVal == JFileChooser.APPROVE_OPTION) {
try {
FileWriter bw = new FileWriter(fc.getSelectedFile()+".txt");
for(int row = 0; row < gameArray.length; row++)
{
for(int col =0; col < gameArray[row].length; col++)
{
bw.write(String.valueOf(gameArray[row][col]);
if ((col+1)!=gameArray[row].length)
bw.write(" ");
System.out.println(gameArray[row][col] + " ");
System.out.println();
}
bw.write(System.getProperty("line.seperator"));
}
bw.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}

Related

How to read "Vertically" from data sets such as CSV

So my issue right now is that I'm really unsure of how to read data "vertically". As an example:
A B C D E F
1 4 1 3 5 8
2 3 1 3 6 4
3 2 1 3 7 5
4 1 1 3 7 4
I have to manipulate this data in a few ways. I'm pretty fine with anything on the same line although I'm clueless as to how to compare items from the same column. One objective I am trying to achieve is to re-write this with only attributes that have more than one domain. So in this instance, C, D, and all the numbers below them should be omitted.
public void dataProcessing(){
File file = new File("insertFilePathHere");
try {
Scanner input = new Scanner(file);
//Please ignore these, they are more relevant to the actual data set
String titles = input.nextLine();
String types = input.nextLine();
int i = 0;
while (input.hasNext()){
String line = input.nextLine();
//This is refering to a line that is incomplete, they are to be removed
if (line.contains("?")){
continue;
}
//For printing what i have back into a table to check results
String[] row = line.split(" ");
for(String index : row){
System.out.printf("%10s", index);
}
System.out.println();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
I'd solve the problem this way:
use a 2D Array (new XXX[][])
read/scan the file LineByLine (two for/while-loops)
write those lines into the array
and then you should be able to work with that array
It should look something like this (I'm sorry if there are any wrong Functions or Errors because I'm coding this without an IDE with SyntaxHighlighting or IntelliSense - there shouldn't be any Errors though):
char[][] arr = new char[rows][cols];
Scanner scanCol = new Scanner(file);
//if you know the #rows and #cols
for(int col = 0; x < cols; x++) {
Scanner scanRow = new Scanner(scanCol.nextLine());
for(int row = 0; y < rows; y++) {
//incase an Exception comes up
try {
char currChar = scanRow.next().charAt(row);
arr[col][row] = currChar;
} catch (Exception e) { //you could also write IndexOutOfBoundsException e
break;
}
}
}
//if you only know the #cols
for(int col = 0; x < cols; x++) {
Scanner scanRow = new Scanner(scanCol.nextLine());
int row = 0;
while(true) { //unfortunately there's no function called hasNextChar()
try {
char currChar = scanRow.next().charAt(row);
arr[col][row] = currChar;
} catch (Exception e) { //you could also write IndexOutOfBoundsException e
break;
}
row++;
}
}
//the top one but in a cleaner way if inline ifs work in while-Loops
int totalRows = 0;
for(int col = 0; x < cols; x++) {
Scanner scanRow = new Scanner(scanCol.nextLine());
int row = 0;
while(row=0?true:(row < totalRows)) {
try {
char currChar = scanRow.next().charAt(row);
arr[col][row] = currChar;
} catch (Exception e) { //you could also write IndexOutOfBoundsException e
break;
}
row++;
}
totalRows = row;
}
//if you just know the file exists ;)
while(scanCol.hasNextLine()) {
Scanner scanRow = new Scanner(scanCol.nextLine());
int row = 0;
while(true) {
try {
char currChar = scanRow.next().charAt(row);
arr[col][row] = currChar;
} catch (Exception e) { //you could also write IndexOutOfBoundsException e
break;
}
row++;
}
}
//to find out how many cols and rows there are
scanCol = new Scanner(file);
int cols = 0;
while(scan.hasNextLine()) {
cols++;
scan.nextLine();
}
scanRow = new Scanner(new Scanner(file).nextLine());
while(true) {
try {
scanRow.nextChar();
rows++;
} catch (Exception e) {
break;
}
}
Please feel free to correct me or ask questions about my solution!

Imported data only showing in first column in JTable (Java)

I tried to make a simple GUI where I can add the First/Last Name of a person and their date of birth. After adding the data to the JTable I can save it in a TxT File and Load it back into the JTable again.
Part where Data is saved:
private void saveListener(){
jb1.addActionListener(e -> {
JFileChooser fileChooser = new JFileChooser();
int returnVal = fileChooser.showSaveDialog(PeopleGUI.this);
if (returnVal == JFileChooser.APPROVE_OPTION) { // Datei Explorer
try {
File file = fileChooser.getSelectedFile();
PrintWriter o = new PrintWriter(file); // o steht für Output
for (int col = 0; col < peopleModel.getColumnCount(); col++) {
o.print(peopleModel.getColumnName(col) + ";");
}
o.println("");
for (int row = 0; row < peopleModel.getRowCount(); row++) {
for (int col = 0; col < peopleModel.getColumnCount(); col++) {
o.println(peopleModel.getValueAt(row, col));
}
}
o.close();
// Output in der Konsole
System.out.println("Success!");
} catch (IOException c) {
c.printStackTrace();
}
}
});
}
Part where Data is loaded:
public void loadListener() {
jb2.addActionListener(e -> {
final JFileChooser fileChooser = new JFileChooser();
int response = fileChooser.showOpenDialog(PeopleGUI.this);
if (response == JFileChooser.APPROVE_OPTION) {
try {
BufferedReader br = new BufferedReader(new FileReader("jlist.txt"));
// Erste Linie sind Kolonnen Beschriftungen
String firstLine = br.readLine().trim();
String[] columnsName = firstLine.split(";");
DefaultTableModel model = (DefaultTableModel) peopleList.getModel();
model.setColumnIdentifiers(columnsName);
// Daten vom TxT holen
Object[] tableLines = br.lines().toArray();
// Reihen mit Daten
for (int i = 0; i < tableLines.length; i++) {
String line = tableLines[i].toString().trim();
String[] dataRow = line.split("/");
model.addRow(dataRow);
}
}
catch (IOException b) {
b.printStackTrace();
}
}
});
}
The problem is, that the imported data is only showing in the first row:
This is how it looks right now
Does anyone now how to fix this issue?
Thanks in advance!
The problem is how you save your data to a file
for (int row = 0; row < peopleModel.getRowCount(); row++) {
for (int col = 0; col < peopleModel.getColumnCount(); col++) {
o.println(peopleModel.getValueAt(row, col));
}
}
Here you save each cell of the JTable in a new line. You want to save each row in a new line with values separated by /
for (int row = 0; row < peopleModel.getRowCount(); row++) {
String r = "";
for (int col = 0; col < peopleModel.getColumnCount(); col++) {
r += peopleModel.getValueAt(row, col);
if (col < peopleModel.getColumnCount() - 1) {
r += "/";
}
}
o.println(r);
}
EDIT: As #camickr stated use StringJoiner is better
for (int row = 0; row < peopleModel.getRowCount(); row++) {
StringJoiner stringJoiner = new StringJoiner("/");
for (int col = 0; col < peopleModel.getColumnCount(); col++) {
stringJoiner.add(peopleModel.getValueAt(row, col).toString());
}
o.println(stringJoiner.toString());
}

How to write into csv sheet with below mentioned specific format

We need to get below format
Redemption Reference Code|Status|Delivery company|Shipper Tracking Number|Comments
2006995040|Shipped|USPS|ABCD12345|Order SHIPPED
2006995042|Cancelled|||INVALID Address
2006995048|Ordered|USPS|ABCD12345|Order SHIPPED
I am using below code
private void accumulateOrdersFromPlacement(){
int count = 0;
for (int i = 0; i < orderIds.size(); i++) {
if (count == 0) {
outPutLineData.add(orderIds.get(i));
outPutLineData.add("Cancelled");
outPutLineData.add("");
outPutLineData.add(" ");
outPutLineData.add(" ");
cancelledStatusLineItems.add(orderIds.get(i));
count++;
} else if (count == 1) {
outPutLineData.add(orderIds.get(i));
outPutLineData.add("Shipped");
if (outPutLineData.contains("Shipped")) {
outPutLineData.add("USPS");
outPutLineData.add("order SHIPPED");
outPutLineData.add("");
}
shippedStatusLineItems.add(orderIds.get(i));
count++;
} else if (count == 2) {
outPutLineData.add(orderIds.get(i));
outPutLineData.add("No Longer Available");
outPutLineData.add("");
outPutLineData.add(" ");
outPutLineData.add(" ");
count++;
nlaStatusLineItems.add(orderIds.get(i));
} else if (count == 3) {
outPutLineData.add(orderIds.get(i));
outPutLineData.add("Ordered");
outPutLineData.add("");
outPutLineData.add(" ");
outPutLineData.add(" ");
orderedStatusLineItems.add(orderIds.get(i));
count = 0;
}
}
I am using below code for file creation. This is the detailed coding . This has more readability to understand code.Here i got confused about the code.We are taking order id count andbased on that this code is working.
private File createFile(final File directory) {
FileWriter fw = null;
File tempFile = null;
try {
directory.mkdir();
tempFile = new File(".//FidelityFulfillment//" + generateFileName(countyThreeLetterCode, "FidelityFulfillment", ".csv", date));
logReport(GenericConstants.NEW_LINE + "Fulfillment file creating:", tempFile.getName());
fw = new FileWriter(tempFile, true);
try (BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile, true))) {
writer.write(generateHeaderLine());
writer.newLine();
for (int y = 1; y < outPutLineData.size(); y++) {
if (y % 5 < 4) {
writer.write(outPutLineData.get(y-1) + fieldSeperator);
logReport(outPutLineData.get(y - 1) + fieldSeperator);
}
else {
writer.write(outPutLineData.get(y-1));
logReport(outPutLineData.get(y));
}
if (y % 5 == 0) {
writer.newLine();
logReport("newline");
}
}
writer.close();
} catch (final IOException e) {
e.printStackTrace();
final String err = "Unable to write file due to : " + e;
logReport(GenericConstants.NEW_LINE + "Unable to create temp local File");
} finally {
fw.close();
}
}catch (Exception e){
e.printStackTrace();
}
return tempFile;
}
Getting response as
Redemption Reference Code|Status|Delivery company|ShipperTrackingNumber|Comments
2006964032|Cancelled|| |
newline
2006964034|Shipped|USPS||
newline
2006964036|No Longer Available||
Last line one pipline is getting missing
First, you loop in a strange way :
for (int y = 1; y < outPutLineData.size(); y++) {
In general, we start at 0.
But you tried to correct that with the condition :
if (y % 5 < 4) {
//System.out.print("size:"+y);
writer.write(outPutLineData.get(y-1) + fieldSeperator);
logReport(outPutLineData.get(y - 1) + fieldSeperator);
}
else {
//System.out.print("size noseperator:"+y);
writer.write(outPutLineData.get(y-1));
logReport(outPutLineData.get(y));
}
Instead, simply use an iterator to read the values, then on read the correct amount of values :
Iterator<String> it = outPutLineData.iterator();
while(it.hasNext()){
for (int j = 0; j < columnCount; ++j) {
writer.write(it.next());
if( j < columnCount - 1)
writer.write(fieldSeperator);
}
writer.newLine();
}
Example with a StringBuilder to print in console :
int columnCount = 2;
String fieldSeperator = "|";
List<String> list = Arrays.asList("foo", "1", "bar", "2");
Iterator<String> it = list.iterator();
//Safe guard !
if(list.size() % columnCount != 0)
throw new RuntimeException("The list does have the correct amount of data");
while(it.hasNext()){
for (int j = 0; j < columnCount; ++j) {
sb.append( it.next() );
if( j < columnCount - 1)
sb.append(fieldSeperator );
}
System.out.println(sb.toString());
sb.setLength(0);
}
foo|1
bar|2
Use a POJO
You are using a List<String> to hold the values, you need to know how many column you need to read to get the value. Instead, use a POJO :
public class Redemption{
String redemptionReference;
String code;
String status;
String deliveryCompany;
String shipperTrackingNumber;
String comments;
}
And create the instance in your first loop :
List<Redemption> list...
That way, you just need to iterate each instance to build your row :
for(Redemption r: list){
writer.write(r.getRedemptionReference() + fieldSeperator);
...
writer.write(r.getComments());
writer.newLine();
}
Of course, you need to use getter and setter but this is just to show what you should do.
CSV API
Big warning, writing your own CSV writer is dangerous. If you have a comment like "This is a | character". You will end up with a line like :
2006995040|Shipped|USPS|This is a | character|Order SHIPPED
That one column to many... because you should have
2006995040|Shipped|USPS|"This is a | character"|Order SHIPPED
But you are not checking that case, and this is only one case. Using a CSV API is safer and simpler.
See Any good library to read and write csv files?

java image processing sobel edge detection

I have a problem with java program, in which i'm using sobel operator for edge detection, but when I'm trying to use that funcion, console says:
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 262144
at sun.awt.image.ByteInterleavedRaster.getPixels(ByteInterleavedRaster.java:1015)
at Obrazek.jButtonSobelActionPerformed(Obrazek.java:566)
And the code for that is:
FileInputStream inFile = null;
try {
long beginTime = (new java.util.Date()).getTime();
int i, j;
double Gx[][], Gy[][], G[][];
inFile = new FileInputStream("D://lenacsmall.bmp");
BufferedImage bi = ImageIO.read(inFile);
int width = bi.getWidth();
int height = bi.getHeight();
int[] pixels = new int[width * height];
int[][] output = new int[width][height];
int[] raster = bi.getRaster().getPixels(0,0,width,height,pixels);
int counter = 0;
for(i = 0 ; i < width ; i++ )
{
for(j = 0 ; j < height ; j++ )
{
output[i][j] = pixels[counter];
counter = counter + 1;
}
}
Gx = new double[width][height];
Gy = new double[width][height];
G = new double[width][height];
for (i=0; i<width; i++) {
for (j=0; j<height; j++) {
if (i==0 || i==width-1 || j==0 || j==height-1)
Gx[i][j] = Gy[i][j] = G[i][j] = 0;
else{
Gx[i][j] = output[i+1][j-1] + 2*output[i+1][j] + output[i+1][j+1] -
output[i-1][j-1] - 2*output[i-1][j] - output[i-1][j+1];
Gy[i][j] = output[i-1][j+1] + 2*output[i][j+1] + output[i+1][j+1] -
output[i-1][j-1] - 2*output[i][j-1] - output[i+1][j-1];
G[i][j] = Math.abs(Gx[i][j]) + Math.abs(Gy[i][j]);
}
}
}
counter = 0;
for(int ii = 0 ; ii < width ; ii++ )
{
for(int jj = 0 ; jj < height ; jj++ )
{
pixels[counter] = (int) G[ii][jj];
counter = counter + 1;
}
}
BufferedImage outImg = new BufferedImage(width,height,BufferedImage.TYPE_BYTE_GRAY);
outImg.getRaster().setPixels(0,0,width,height,pixels);
FileOutputStream outFile = null;
try {
outFile = new FileOutputStream("D://copyTop.bmp");
} catch (FileNotFoundException ex) {
}
try {
ImageIO.write(outImg,"BMP",outFile);
} catch (IOException ex) {
}
JFrame TheFrame = new JFrame("obrazek " + width + " " + height);
JLabel TheLabel = new JLabel(new ImageIcon(outImg));
TheFrame.getContentPane().add(TheLabel);
TheFrame.setSize(600, 600);
TheFrame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
TheFrame.setVisible(true);
} catch (FileNotFoundException ex) {
Logger.getLogger(Obrazek.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Obrazek.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
inFile.close();
} catch (IOException ex) {
Logger.getLogger(Obrazek.class.getName()).log(Level.SEVERE, null, ex);
}
}
I'm really need some help. I hope someone will answer for that post, thank you.
Greetings :)
Try this, edge detection using sobel approach, I just did this yesterday.
`
static BufferedImage inputImg,outputImg;
static int[][] pixelMatrix=new int[3][3];
public static void main(String[] args) {
try {
inputImg=ImageIO.read(new File("your input image"));
outputImg=new BufferedImage(inputImg.getWidth(),inputImg.getHeight(),TYPE_INT_RGB);
for(int i=1;i<inputImg.getWidth()-1;i++){
for(int j=1;j<inputImg.getHeight()-1;j++){
pixelMatrix[0][0]=new Color(inputImg.getRGB(i-1,j-1)).getRed();
pixelMatrix[0][1]=new Color(inputImg.getRGB(i-1,j)).getRed();
pixelMatrix[0][2]=new Color(inputImg.getRGB(i-1,j+1)).getRed();
pixelMatrix[1][0]=new Color(inputImg.getRGB(i,j-1)).getRed();
pixelMatrix[1][2]=new Color(inputImg.getRGB(i,j+1)).getRed();
pixelMatrix[2][0]=new Color(inputImg.getRGB(i+1,j-1)).getRed();
pixelMatrix[2][1]=new Color(inputImg.getRGB(i+1,j)).getRed();
pixelMatrix[2][2]=new Color(inputImg.getRGB(i+1,j+1)).getRed();
int edge=(int) convolution(pixelMatrix);
outputImg.setRGB(i,j,(edge<<16 | edge<<8 | edge));
}
}
File outputfile = new File("your output image");
ImageIO.write(outputImg,"jpg", outputfile);
} catch (IOException ex) {System.err.println("Image width:height="+inputImg.getWidth()+":"+inputImg.getHeight());}
}
public static double convolution(int[][] pixelMatrix){
int gy=(pixelMatrix[0][0]*-1)+(pixelMatrix[0][1]*-2)+(pixelMatrix[0][2]*-1)+(pixelMatrix[2][0])+(pixelMatrix[2][1]*2)+(pixelMatrix[2][2]*1);
int gx=(pixelMatrix[0][0])+(pixelMatrix[0][2]*-1)+(pixelMatrix[1][0]*2)+(pixelMatrix[1][2]*-2)+(pixelMatrix[2][0])+(pixelMatrix[2][2]*-1);
return Math.sqrt(Math.pow(gy,2)+Math.pow(gx,2));
}
`
In your for loops when you check
if (i==0 || i==width-1 || j==0 || j==height-1)
you should probably be checking for i >= width-2 rather than i==width-1.
For example, if width is 10 it falls into the statement if i == 9.
you want to catch if i == 8 since you later check [i+1], which in your code would be out of the bounds of your image array, since the maximum you can have is 9 (width-1).
Obviously the same applies for j.

What's wrong with my loop? Keep getting NoSuchElementException

I keep getting a NoSuchElement Exception at the line maze[r][c]=scan.next();. How can I resolve that?
try {
Scanner scan = new Scanner(f);
String infoLine = scan.nextLine();
int rows=0;
int columns=0;
for(int i = 0; i<infoLine.length();i++){
if(Character.isDigit(infoLine.charAt(i))==true){
rows = (int)infoLine.charAt(i);
columns = (int)infoLine.charAt(i+1);
break;
}
}
String [][] maze = new String[rows][columns];
int r = 0;
while(scan.hasNextLine()==true && r<rows){
for(int c = 0; c<columns;c++){
maze[r][c]=scan.next();
}
r++;
}
return maze;
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Look at this part of your code:
while(scan.hasNextLine()==true && r<rows){ // 1
for(int c = 0; c<columns;c++){ // 2
maze[r][c]=scan.next(); // 3
} // 4
r++; // 5
} // 6
In line 1 you are checking to make sure that scan has another line available. But in line 3, you read that line - inside the 2:4 loop. So if there are more than 1 columns, you will be asking for the next scan more than once - and you only checked to see if there was one next line. So on the second column, if you're at the end of scan, you try to read from scan even though it's run out.
Try this:
try {
Scanner scan = new Scanner(f);
String infoLine = scan.nextLine();
int rows = 0;
int columns = 0;
for (int i = 0; i < infoLine.length();i++) {
if (Character.isDigit(infoLine.charAt(i))) {
rows = Character.digit(infoLine.charAt(i), 10);
columns = Character.digit(infoLine.charAt(i + 1), 10);
break;
}
}
String [][] maze = new String[rows][columns];
int r = 0;
while(scan.hasNextLine() && r < rows) {
int c = 0;
while(scan.hasNextLine() && c < columns) {
maze[r][c]=scan.next();
c++
}
r++;
}
return maze;
} catch (FileNotFoundException e) {
e.printStackTrace();
}

Categories

Resources