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.

8
  • \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. Commented Dec 19, 2014 at 2: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? Commented Dec 19, 2014 at 2: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 ;) Commented Dec 19, 2014 at 2:15
  • It sounds like your problem is with the presentation of the information, not the reading from the file. Commented Dec 19, 2014 at 2: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 Commented Dec 19, 2014 at 2:18

2 Answers 2

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); 
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.