• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Peter Rooke
  • Himai Minh
Bartenders:
  • Piet Souris
  • Mikalai Zaikin

not printing everything I want

 
Ranch Hand
Posts: 545
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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


 
Master Rancher
Posts: 4957
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 545
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
print class

 
Norm Radder
Master Rancher
Posts: 4957
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Where is the code that loads the contents of the String into the list?
 
Adam Chalkley
Ranch Hand
Posts: 545
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 545
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Master Rancher
Posts: 4957
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 545
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 545
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Master Rancher
Posts: 4957
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 545
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for the help Norm I figured out a way that works in my case

 
Norm Radder
Master Rancher
Posts: 4957
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 545
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
true I could just increment i each time after I add some data
 
Norm Radder
Master Rancher
Posts: 4957
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 545
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Master Rancher
Posts: 4957
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the purpose of x?  Why not have i be the loop variable?
 
Marshal
Posts: 77903
373
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 545
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have some suggestions for your code.  See comments.
 
Campbell Ritchie
Marshal
Posts: 77903
373
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic