I have created this thread to ask for help in java programming.

I also would wish that other users can post help and/or problems that they would need assistance with here as well.
Great idea, j-man! Are you starting to learn Java now, or have you worked with it before? What book are you using (if any), if you're learning it now?
I have written this code playing around with some of java's features for my CS class.


Code:

import java.io.*;
import java.util.Scanner;
public class ProgrammingAssignment3 {
 public static void main(String[] args) throws FileNotFoundException {
  int perLine = 0;//How many X's per line
  int Lines = 0;//How lines of X's
  Scanner keyboard = new Scanner(System.in);
  //Ask for X's per line
  System.out.println("How many X's would you like in each line?");
  perLine = keyboard.nextInt();
  //Ask for number of lines
  System.out.println("How many lines do you want?");
  Lines = keyboard.nextInt();
  //Ask for name of file to save to
  String fileName = "fileName";
  System.out.println("What would you like the file to be named?");
  fileName = keyboard.next();
  //Create file adding .txt file extension
  PrintWriter outputFile = new PrintWriter(FileName + ".txt");
  //Declare i,j as loop increment variables
  int i = 1;
  int j = 1;
  //Generate the matrix of X's,
  //Display matrix and save to file
  while (j <= Lines) { //Add more lines of X's
   i = 1;
   while (i <= perLine) { //Create each line of X's
    System.out.print("X");
    OutputFile.print("X");
    i++;
   }
    System.out.print("\n");
    outputFile.print("\n");
    j++;
  }
  //Close file to editing
  outputFile.close();
 }
}


This code is suppose to ask for user input for the number of X's in each line, the number of lines of X's and the name of a file to output the X's to, adding .txt to the filename.

When I compile and execute this code it does everything but place the X's in different lines in the .txt file. when I use 5 X's in each line and 3 lines of X's; it stores something like this:

XXXXXXXXXXXXXXX

I would like it to store something like this:

XXXXX
XXXXX
XXXXX

when I use the
I have looked at java code in the past but have not been able to make much since of it. I am learning as part of my CS class so I am using the book the class asks for.

Here is the ISBN Number : 9780136080206 of the book.
I am extensively experienced in java. I'll type up a more detailed reply when I get a free moment.
Are you using Windows, Linux, or MacOS X? Different operating systems have different line endings:

- Windows: \r\n
- Linux: \n
- MacOS X: \r

But there's an even better solution. According to this thread, you can use this and not have to worry about what type of system you're working on:


Code:
System.getProperty("line.separator");
Depending on whether I am able to use my desktop, running Windows 7, or have to use my laptop, running Ubuntu Linux.

This particular instance i am using windows and will most of the time.
j-man-calc-proger wrote:
Depending on whether I am able to use my desktop, running Windows 7, or have to use my laptop, running Ubuntu Linux.

This particular instance i am using windows and will most of the time.
Then you would want \r\n. But the portable solution that I mentioned would be preferable.
would it work if i used:

Code:

System.out.println(" ");
outputFile.println(" ");

in place of:

Code:

System.out.print("\n");
output.print("\n");

because println() automatically adds a newline??
It would, but it's not the correct solution. This is what you want, I believe:


Code:
System.out.print(System.getProperty("line.separator"));
output.print(System.getProperty("line.separator"));
Thank you. The assignment is not due until next week but the early bird gets the worm.
LOL
System.getProperty("line.separator") is one of the ugliest things I've ever seen if you're using it more than once. Just declaring a constant elsewhere would be a lot easier.

If you're trying to write a newline to a file, you could simply use BufferedWriter's newLine() method.
JoeYoung wrote:
System.getProperty("line.separator") is one of the ugliest things I've ever seen if you're using it more than once. Just declaring a constant elsewhere would be a lot easier.

If you're trying to write a newline to a file, you could simply use BufferedWriter's newLine() method.
That sounds like a very Java-y solution to me, and one of the reasons I am not hugely fond the language. It seems a bit counterproductive to me to drag in the BufferedWriter when the original application doesn't really need buffering added on top for anything. I'm fine with declaring a string and filling it with System.getProperty("line.separator") for reuse, but I'd be opposed to switching to a BufferedWriter.
KermMartian wrote:
JoeYoung wrote:
System.getProperty("line.separator") is one of the ugliest things I've ever seen if you're using it more than once. Just declaring a constant elsewhere would be a lot easier.

If you're trying to write a newline to a file, you could simply use BufferedWriter's newLine() method.
That sounds like a very Java-y solution to me, and one of the reasons I am not hugely fond the language. It seems a bit counterproductive to me to drag in the BufferedWriter when the original application doesn't really need buffering added on top for anything. I'm fine with declaring a string and filling it with System.getProperty("line.separator") for reuse, but I'd be opposed to switching to a BufferedWriter.


Java-y? While BufferedWriter isn't particularly important in this example, it's still a very common solution for more complicated file-writing routines (and worth learning) and doesn't suffer from these cross-platform inconsistencies because of its Java-y-ness.

By the way, I have never had a problem using just "\n" for Printwriter on ANY system. Does this depend on the IDE or what?

I forgot to mention, Java 7 now has a System.lineSeparator() method.
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement