• Post Reply Bookmark Topic Watch Topic
  • New Topic

not printing everything I want  RSS feed

 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys I am using a buffered Reader to read text from a file into my program so I can use that data I have a text file with the following contents

1,HELLO,2,HI,3 HALLO,4 GUTEN TAG

I want the while loop to basically create a new print File until there is no more text

but what I find is I only get the output 1 and Hello and nothing else is printed I have no idea what I'm doing wrong


 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where is the code for the print class?

Where is the loop to go through the chunks array from the split method?


Note: Java conventions say class names should begin with an uppercase letter.
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
print class

 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where is the code that loads the contents of the String into the list?
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thats the new code I removed i and j but it still only prints the first entry 1 and hello not the rest of the text

and the code that loads the content into the string is in the static block

 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1,HELLO,
2,HI,
3,HALLO,
4,GUTEN TAG

I noticed when I change the contents of my file to display top down it works fine and prints all the output,so my question is why can't the text be beside each other I'm guessing because it reads from line to line? but how to solve this?
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but how to solve this?

Write code that reads a line, parses/splits it and puts its values into the list.

The code needs to know if there are multiple tuples on a line
of if there is only one tuple on each line.

Use the results of the split to determine which. 2 elements == one tuple
otherwise number of tuples = array.length/2
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Write code that reads a line, parses/splits it and puts its values into the list. [/code]

hey Norm thanks for the reply,thats what my code is currently doing I'm looking for a way to get input without using nextLine because nextLine reads the whole line
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I could do it without a while loop and assign multiple values and multiple add methods but that would take a lot of code and defeat the whole purpose
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are several ways to do it.  You have tried two: All on one line vs each tuple on its own line.  Its up to you how you want it done.
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for the help Norm I figured out a way that works in my case

 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why have three variables: x,i,j?  It can be done with one index that is incremented by 2.
Value at the index is a number
value at index+1 is the word
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
true I could just increment i each time after I add some data
 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I could just increment i each time

Incrementing the loop variable is the job of the third expression in the for loop.
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
there we go tidied it a lot, a lot of unnecessary code that made it look more complicated than it should be

 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the purpose of x?  Why not have i be the loop variable?
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why have you got all that code in a static initialiser? Why have you indented it wrongly?
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry meant to change x with i

and I used a static block because I wanted it to run before the constructor and also so I could populate the static ArrayList list
 
Knute Snortum
Sheriff
Posts: 4279
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have some suggestions for your code.  See comments.
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Adam Chalkley wrote:. . . populate the static ArrayList list
So why have you got a static reference to the List?

OI work on the assumption that the keyword static is a mistake unless I can see a good explanation for it.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!