12 Replies - 1423 Views - Last Post: 18 August 2008 - 03:04 AM Rate Topic: -----

#1 tombfighter  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 14-August 08

Saving grid of numbers into a txt file, when button is pressed

Posted 16 August 2008 - 02:46 AM

Hi guys, need some on my code please. The problem I was given to do was: when the user wants to save the current grid of numbers to a file, thy enter the file name in the filenameT on the bottom right of the Sudoku window, and press the SAVEF button. The current Sudoku should be written to the file.

So the target respone I suppose to get was something like this:
Sudoku - pwon071

-----------------------
| - - - | -1 9 | - 4 8 |
| - - - | 2 3 8 | 6 - - |
| 9 - 8 | - 6 - | 1 - 2 |
-----------------------
| - - 1 | - 7 - | 2 6 - |
| 3 - 2 | - - - | - - - |
| 7 - 9 | 8 - 4 | 5 - - |
-----------------------
| 2 7 3 | - 4 1 | - - 5 |
| - 8 - | 9 5 - | - 3 - |
| 5 - - | 7 - 3 | - 2 - |
-----------------------

But the actual respone I got was:
Sudoku - pwon071 ----------------------- | - - - | - 1 9 | - 4 8 | | - - - | 2 3 8 | 6 - - | | 9 - 8 | - 6 - | 1 - 2 | -----------------------| - - 1 | - 7 - | 2 6 - | | 3 - 2 | - - - | - - - | | 7 - 9 | 8 - 4 | 5 - - | -----------------------| 2 7 3 | - 4 1 | - - 5 | | - 8 - | 9 5 - | - 3 - | | 5 - - | 7 - 3 | - 2 - | -----------------------



I don't really know what went wrong! Supposingly t should work. So I hop someone can give me an answer. Thanks!


[/b] 	private void saveSudokuToFile() {
		FileWriter fw = null;
		writeSudokuToFile(fw);
----------------------------------------(another method)----------------------------------------------------------------------
	private void writeSudokuToFile(PrintWriter fw) {
		String fileName = fileNameT.getText();
		int i,j;

		if (fileName.length() <= 0) {
			fileName = DEFAULT_FILE_NAME;
			fileNameT.setText("" + DEFAULT_FILE_NAME);
		}

		currentGridOfValues = getGridOfValues(numberFields);

		try {
			fw = new FileWriter (fileName + ".txt");
			fw.write("Sudoku - pwon071\n\n  ----------------------- \n| ");

			for (i=0;i<9;i++){
				for(j=0;j<9;j++){


					if(currentGridOfValues[i][j] != 0){
						fw.write(currentGridOfValues[i][j] + " ");
					}else{
						fw.write("- ");
					}
					if ((j==2)||(j==5)){
						fw.write("| ");
					}else if(j==8){
						fw.write("| \n");
					}
				}


				if ((i==2)||(i==5)||(i==8)){
					fw.write(" -----------------------\n");
				}
				if (i!=8){
					fw.write("| ");
				}

			}

			fw.close();
		}catch(IOException e){
			System.out.println(e);
		}
	}
[b]


Is This A Good Question/Topic? 0
  • +

Replies To: Saving grid of numbers into a txt file, when button is pressed

#2 lordms12  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 30
  • View blog
  • Posts: 339
  • Joined: 16-February 08

Re: Saving grid of numbers into a txt file, when button is pressed

Posted 16 August 2008 - 04:25 AM

What do you mean with response?
Was This Post Helpful? 0
  • +
  • -

#3 tombfighter  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 14-August 08

Re: Saving grid of numbers into a txt file, when button is pressed

Posted 16 August 2008 - 04:41 PM

View Postlordms12, on 16 Aug, 2008 - 04:25 AM, said:

What do you mean with response?


I mean output. When the save button is pressed the grid of numbers should saved into the txt file in that kind of format
Was This Post Helpful? 0
  • +
  • -

#4 thenovices  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 74
  • Joined: 19-January 08

Re: Saving grid of numbers into a txt file, when button is pressed

Posted 16 August 2008 - 07:25 PM

okay you have basically three ways to fix this problem:

1. Keep using FileWriter, and when you want to insert a new line, you can either use "\r\n" (I think that should work on Windows), or to be absolutely sure that it will work cross-platform, you can use System.getProperties(line.separator)

2. Use BufferedWriter, which is a wrapper class for the FileWriter class. BufferedWriters have a new method called newLine(), which automatically takes you to a new line.

BufferedWriter bw = new BufferedWriter(new FileWriter("input.txt"));
bw.newLine();


3. Use PrintWriter. PrintWriter has a println(String s) method which basically acts the same way as System.out.println(String s)

PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("input.txt")));
pw.print("blah");
pw.println("blah");


As always, look at the API for more information about these classes.


In conclusion, not all systems use only the '\n' character to return. Some systems use "\r\n" and others, I don't know. But basically using System.getProperties(line.separator), BufferedWriter.newLine(), or PrintWriter.println() under my knowledge should guarantee that you get a newline out.

This post has been edited by thenovices: 16 August 2008 - 07:27 PM

Was This Post Helpful? 1
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8027
  • View blog
  • Posts: 31,161
  • Joined: 06-March 08

Re: Saving grid of numbers into a txt file, when button is pressed

Posted 16 August 2008 - 07:54 PM

Or use an ObjectWriter and save your all "Sudoku" object to a file
Was This Post Helpful? 0
  • +
  • -

#6 tombfighter  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 14-August 08

Re: Saving grid of numbers into a txt file, when button is pressed

Posted 16 August 2008 - 08:25 PM

View Postthenovices, on 16 Aug, 2008 - 07:25 PM, said:

okay you have basically three ways to fix this problem:

1. Keep using FileWriter, and when you want to insert a new line, you can either use "\r\n" (I think that should work on Windows), or to be absolutely sure that it will work cross-platform, you can use System.getProperties(line.separator)

2. Use BufferedWriter, which is a wrapper class for the FileWriter class. BufferedWriters have a new method called newLine(), which automatically takes you to a new line.

BufferedWriter bw = new BufferedWriter(new FileWriter("input.txt"));
bw.newLine();


3. Use PrintWriter. PrintWriter has a println(String s) method which basically acts the same way as System.out.println(String s)

PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("input.txt")));
pw.print("blah");
pw.println("blah");


As always, look at the API for more information about these classes.


In conclusion, not all systems use only the '\n' character to return. Some systems use "\r\n" and others, I don't know. But basically using System.getProperties(line.separator), BufferedWriter.newLine(), or PrintWriter.println() under my knowledge should guarantee that you get a newline out.


hi
I think I would use method 1 to fix my problem. Just several question I don't really get it:
1. So are you saying that for every \n I type, instead of just using \n I should type \r\n?
2. If I use \r\n will it work for OS such as mac or linyx?
3. Can you please teach me how to use the System.getProperties(line.separtor) ? Because I never used it before
and don't know how to use it as well. I would really like to use this method, just to play safe (if you know what I meant lol).

thanks!
Was This Post Helpful? 0
  • +
  • -

#7 tombfighter  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 14-August 08

Re: Saving grid of numbers into a txt file, when button is pressed

Posted 16 August 2008 - 08:32 PM

View Postpbl, on 16 Aug, 2008 - 07:54 PM, said:

Or use an ObjectWriter and save your all "Sudoku" object to a file


ummm what do you mean? I'm a beginner in java, so it is very hard for me to understand how/what to use/imply certain commands when I was just given a name. hahaha
Was This Post Helpful? 0
  • +
  • -

#8 thenovices  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 74
  • Joined: 19-January 08

Re: Saving grid of numbers into a txt file, when button is pressed

Posted 17 August 2008 - 03:02 AM

Quote

hi
I think I would use method 1 to fix my problem. Just several question I don't really get it:
1. So are you saying that for every \n I type, instead of just using \n I should type \r\n?
2. If I use \r\n will it work for OS such as mac or linyx?
3. Can you please teach me how to use the System.getProperties(line.separtor) ? Because I never used it before
and don't know how to use it as well. I would really like to use this method, just to play safe (if you know what I meant lol).

thanks!


okay, this website should explain it all very well: Explanation of line separators

Unix/Linux uses "\n", Windows uses "\r\n" and Mac uses "\r". So if you use println() or newLine(), Java takes care of the differences for you.


View Posttombfighter, on 16 Aug, 2008 - 08:32 PM, said:

View Postpbl, on 16 Aug, 2008 - 07:54 PM, said:

Or use an ObjectWriter and save your all "Sudoku" object to a file


ummm what do you mean? I'm a beginner in java, so it is very hard for me to understand how/what to use/imply certain commands when I was just given a name. hahaha


Google and the Java API are your best friends. Use them.

ObjectWriter
Was This Post Helpful? 0
  • +
  • -

#9 tombfighter  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 14-August 08

Re: Saving grid of numbers into a txt file, when button is pressed

Posted 17 August 2008 - 03:10 AM

View Postthenovices, on 17 Aug, 2008 - 03:02 AM, said:

Quote

hi
I think I would use method 1 to fix my problem. Just several question I don't really get it:
1. So are you saying that for every \n I type, instead of just using \n I should type \r\n?
2. If I use \r\n will it work for OS such as mac or linyx?
3. Can you please teach me how to use the System.getProperties(line.separtor) ? Because I never used it before
and don't know how to use it as well. I would really like to use this method, just to play safe (if you know what I meant lol).

thanks!


okay, this website should explain it all very well: Explanation of line separators

Unix/Linux uses "\n", Windows uses "\r\n" and Mac uses "\r". So if you use println() or newLine(), Java takes care of the differences for you.


View Posttombfighter, on 16 Aug, 2008 - 08:32 PM, said:

View Postpbl, on 16 Aug, 2008 - 07:54 PM, said:

Or use an ObjectWriter and save your all "Sudoku" object to a file


ummm what do you mean? I'm a beginner in java, so it is very hard for me to understand how/what to use/imply certain commands when I was just given a name. hahaha


Google and the Java API are your best friends. Use them.

ObjectWriter


ohh! ok I know what it is now! just few more question:
1. so where do I put the
static final String lineSeparator = System.getProperty ( "line.separator" );
?
2. do I still need /r/n or any of these if I use the System.getProperty ( "line.separator" )?
3. so instead of just System.out.print(), I use System.getProperty ( "line.separator" ) right?

This post has been edited by tombfighter: 17 August 2008 - 03:15 AM

Was This Post Helpful? 0
  • +
  • -

#10 thenovices  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 74
  • Joined: 19-January 08

Re: Saving grid of numbers into a txt file, when button is pressed

Posted 17 August 2008 - 03:14 AM

hmm... thats weird... well I basically summarized what the webpage said except for one more thing:

If you really want to get the lineSeparator on your own, use this:

static final String lineSeparator = System.getProperty ( "line.separator" );


And also, don't use the ObjectWriter. Use ObjectInputStream/ObjectOutputStream instead. I believe they should work better.
Was This Post Helpful? 0
  • +
  • -

#11 tombfighter  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 14-August 08

Re: Saving grid of numbers into a txt file, when button is pressed

Posted 17 August 2008 - 03:35 AM

View Postthenovices, on 17 Aug, 2008 - 03:14 AM, said:

hmm... thats weird... well I basically summarized what the webpage said except for one more thing:

If you really want to get the lineSeparator on your own, use this:

static final String lineSeparator = System.getProperty ( "line.separator" );


And also, don't use the ObjectWriter. Use ObjectInputStream/ObjectOutputStream instead. I believe they should work better.


if I use the lineseparator do I still need to use the /r/n fuction?
Was This Post Helpful? 0
  • +
  • -

#12 thenovices  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 74
  • Joined: 19-January 08

Re: Saving grid of numbers into a txt file, when button is pressed

Posted 17 August 2008 - 08:03 PM

honestly, if you can please avoid using the lineSeparator. It just makes code messier and more confusing. If it is allowed for your assignment, use a PrintWriter, or an ObjectOutputStream, both of which will be much more convenient to use! (Especially the ObjectOutputStream, it sounds like a really cool thing!)

but I'll answer your question anyways. Once you have the lineSeparator, then "\n" or "\r" or "\r\n" should never appear in your code. It should always be replaced by lineSeparator. What System.getProperty(line.separator) returns is the proper backslash characters for a new line. (New Lines are different for Macs, Windows, and Linux/Unix).

Therefore this
 FileWriter fw = new FileWriter("output.txt");
fw.write("blah\n");


should be replaced by this:
 FileWriter fw = new FileWriter("output.txt");
fw.write("blah" + lineSeparator);



However, ideally, this would be a better option:
 PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("output.txt")));
pw.println("blah");



Or in this case, you can make an Object representing a Sudoku grid (if you haven't already) and use ObjectInputStream/ObjectOutputStream to read/write Sudoku objects from a file. I don't know much about these classes, so you'll have to do some researching yourself if you want to use it. However, using Object(in/out)putStream will save yourself A LOT of I/O code and is very easy to read.
Was This Post Helpful? 0
  • +
  • -

#13 tombfighter  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 14-August 08

Re: Saving grid of numbers into a txt file, when button is pressed

Posted 18 August 2008 - 03:04 AM

View Postthenovices, on 17 Aug, 2008 - 08:03 PM, said:

honestly, if you can please avoid using the lineSeparator. It just makes code messier and more confusing. If it is allowed for your assignment, use a PrintWriter, or an ObjectOutputStream, both of which will be much more convenient to use! (Especially the ObjectOutputStream, it sounds like a really cool thing!)

but I'll answer your question anyways. Once you have the lineSeparator, then "\n" or "\r" or "\r\n" should never appear in your code. It should always be replaced by lineSeparator. What System.getProperty(line.separator) returns is the proper backslash characters for a new line. (New Lines are different for Macs, Windows, and Linux/Unix).

Therefore this
 FileWriter fw = new FileWriter("output.txt");
fw.write("blah\n");


should be replaced by this:
 FileWriter fw = new FileWriter("output.txt");
fw.write("blah" + lineSeparator);



However, ideally, this would be a better option:
 PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("output.txt")));
pw.println("blah");



Or in this case, you can make an Object representing a Sudoku grid (if you haven't already) and use ObjectInputStream/ObjectOutputStream to read/write Sudoku objects from a file. I don't know much about these classes, so you'll have to do some researching yourself if you want to use it. However, using Object(in/out)putStream will save yourself A LOT of I/O code and is very easy to read.


alright! I didn't use the objectinputstream because I don't know what it was, so instead I did it this way. I wounder is there a better or simplier way??

private void writeSudokuToFile() {
		String fileName = fileNameT.getText();
		FileWriter fw = null;
		int i,j;

		if (fileName.length() <= 0) {
			fileName = DEFAULT_FILE_NAME;
			fileNameT.setText("" + DEFAULT_FILE_NAME + ".txt");
		}

		currentGridOfValues = getGridOfValues(numberFields);

		try {
			fw = new FileWriter (fileName + ".txt");
			fw.write("Sudoku - pwon071\r\n\r\n  -----------------------\r\n| ");

			for (i=0;i<9;i++){
				for(j=0;j<9;j++){


					if(currentGridOfValues[i][j] != 0){
						fw.write(currentGridOfValues[i][j] + " ");
					}else{
						fw.write("- ");
					}
					if ((j==2)||(j==5)){
						fw.write("| ");
					}else if(j==8){
						fw.write("| \r\n");
					}
				}


				if ((i==2)||(i==5)||(i==8)){
					fw.write(" -----------------------\r\n");
				}
				if (i!=8){
					fw.write("| ");
				}

			}

			fw.close();
		}catch(IOException e){
			System.out.println(e);
		}
	}


I just hope the marker won't run it through mac or linux, lol
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1