Hey everyone, It's 3am and I'm pulling my hair out trying to get this to work. I'm reading data from a file that looks like so:

James
20
John
30
Peter
25

and I am storing it in an arraylist then outputting the data. It outputs exactly as it is in the text file, what I want to do now is display the data with the name and age side by side like so:

James 20
John 30
Peter 25

Here is the code I have so far

BufferedReader reader = new BufferedReader(new FileReader(
					"files/temperatures.txt"));

			String line = "";

			while ((line = reader.readLine()) != null) {
				arrayOfStrings.add(line);
			}

			for (String readline : arrayOfStrings) {
				System.out.println(readline);
			}

			reader.close();

If you have any idea as to how to do this I thank you in advance for your reply

well surely if you read each line sequentially and print each array item sequentially, it will be exactly as it was originally. if you are not concerned about robustness (i.e. always expect the input file to be of your chosen format), you can manipulate your foreach loop to print returns only when needed.

Yes, it prints out the data as I expected. Now I need to show it in the format I need. How would I go about manipulating the loop to display them as needed?

It's very late/ early, I'm going to get some sleep then maybe I can give a better explanation and have another shot and fixing this.

if you do not care about validating the input file, you can simply print a carriage return on each second iteration.

This article has been dead for over six months. Start a new discussion instead.