Jump to content

Print everything except first line

- - - - -

  • Please log in to reply
2 replies to this topic

#1
Serialcek

Serialcek

    Learning Programmer

  • Members
  • PipPipPip
  • 72 posts
Hello I'm trying to read a file.txt and print everything except the first line in TextArea because I need to save the first line as a String and I don't want to add it to the text area. The problem is that I print two newline at the beginning which I don't want to.

Instead of print:
"Hello
How are you?"

I print:
"


Helo
How are you?"


So far I did something like that:



		try {


			URL url = new URL(server);


			BufferedReader in = new BufferedReader(new InputStreamReader( url.openStream()));


			String strLine;


			int count = 1;


			while ((strLine = in.readLine()) != null ) {


                                String str = strLine;

				String read;


				if (count == 1) {

					String mail = str;  // Here I save the first line as mail

					count++;

				}


		                      if (count > 1) {


					if (count == 2) {

								read = "";

								jTextArea1.setText(read);

								count++;


							}


							else if (count >= 3) {


								read = jTextArea1.getText();

								jTextArea1.setText(read + "\n" + str); 

								count++;

							}

							

							count++;

					}





#2
gregwarner

gregwarner

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 853 posts
  • Location:Arkansas
You've got way too many count++ statements in there. There should be only one at the very end of your while loop. For instance, consider this sample from your code:



				if (count == 1) {

					String mail = str;  // Here I save the first line as mail

					count++;

				}


		                      if (count > 1) {


On the first iteration, because you increment count in the body of your first if statement, your 2nd if statement immediately evaluates to true and enters the body of that statement, which is not what you want to do. (As I understand it, on the first iteration of the while loop, you store the first line in 'mail' and return to the beginning of the loop.)

In pseudocode, what you want is something like this:

count = 1.

while (read next line):

    if count == 1:

        store line in 'mail'.

    else:

        append line to text area.

    end if.

    count++

end while.


At the end of loop execution, when it finally exits the while loop, 'count' will be equal to n + 1, where n is the number of lines successfully read. (Including the first header line, stored in 'mail'.)
Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.

– Douglas Hofstadter, Gödel, Escher, Bach: An Eternal Golden Braid


#3
Serialcek

Serialcek

    Learning Programmer

  • Members
  • PipPipPip
  • 72 posts
I guess I just needed a pause :)
I have a solution (it's actually so easy ).
If someone will have the same problem you need just too add a new condition at the end.

So now it look like:


		while ((strLine = in.readLine()) != null) {


				String str = strLine;

				String preberi;


				if (count == 1) {

					mail = str;


					count++;

					System.out.println(mail);

				}


				if (count > 1) {


					if (count == 2) {

						System.out.println("enako je");

						preberi = "";

						jTextArea1.setText(preberi);


						count++;


					}


					else if (count >= 3) {


						preberi = jTextArea1.getText();


						if (count == 3) {

							jTextArea1.setText(str);


						}


						else if (count > 3) {

							jTextArea1.setText(preberi + "\n" + str);

						}


						count++;

					}


				}






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users