0

I am new to programming and I would like some tips. I have a program that reads from a text file using the the following method:

BufferedReader reader = new BufferedReader(new FileReader(file));
.....

This works but the problem is that some lines are long and I can't see all the text in the GUI. How should I tell Java to go to next line if length is equal to a certain amount?

Example text:

A week after Mozart died, his ghost was discovered trying to erase his music. When asked why, it said "I'm decomposing.

Instead of two lines, I want to parse it into four lines. Any help would be appreciated.

smac89
  • 39,374
  • 15
  • 132
  • 179
Salah Klein
  • 380
  • 3
  • 12
  • `\n` is the newline character in Java. String manipulation is somewhat complex, but you can get the length of a String, subsequences of a String and build new Strings from them. I suggest you check the API for your version of Java -- [_Java 7 API_](http://docs.oracle.com/javase/7/docs/api/). – adamdc78 Dec 19 '14 at 02:06
  • "tell java to go to next line" means ignore the rest of the line and read the next line from the Text-File or you want to make line break in the read string? – Rami.Q Dec 19 '14 at 02:13
  • 1
    @Rami.Q i think he wants to add \n to the sentence so that it displays as 4 lines. I'm thinking the problem can actually be solved using word wrap in the java console ;) – softwarenewbie7331 Dec 19 '14 at 02:15
  • It sounds like your problem is with the presentation of the information, not the reading from the file. – Jason Dec 19 '14 at 02:18
  • if the line has for example 250 alphabets , i want java to show 100 and than go to the next line and show another 100 and than show 50. Not ignore the rest. yes in GUI. thank you – Salah Klein Dec 19 '14 at 02:18
  • What do you mean with GUI? Does your program have a graphical user interface? If so, what graphical element are you using to display the text? A text field should wrap the text automatically. Actually, so should do your terminal emulator in case you are not programming a graphical interface yourself (which I'm pretty sure you are not). Maybe all you need to do is change some settings of your terminal emulator? – 5gon12eder Dec 19 '14 at 02:23
  • possible duplicate of [Large string split into lines with maximum length in java](http://stackoverflow.com/questions/7528045/large-string-split-into-lines-with-maximum-length-in-java) – smac89 Dec 19 '14 at 02:28
  • I made JPanel and added 2 Jlabels to it. in one of them a question , and the answer in the other. Some answers are to long, and i dont know how to make it go to a new line after a certian amount of charackters. – Salah Klein Dec 19 '14 at 02:29

2 Answers2

0

as per your comments, you are using JLabel for displaying the data.

to show multi-line text in JLabel just wrap your text in <html> tag like:

JLabel label = new JLabel("<html>your long text here ....</html>");

example:

String theReadLineFromTextFile = ....;

theReadLineFromTextFile = "<html>"+theReadLineFromTextFile+"</html>";
JLabel label = new JLabel(theReadLineFromTextFile);
//or 
JLabel label = new JLabel();
label.setText(theReadLineFromTextFile);
Rami.Q
  • 2,486
  • 2
  • 19
  • 30
0

If you can't do it in the gui, then some of the answers here will help: Wrap the string after a number of characters word-wise in Java

Community
  • 1
  • 1
ccleve
  • 15,239
  • 27
  • 91
  • 157