• Post Reply Bookmark Topic Watch Topic
  • New Topic

Printing an ArrayList  RSS feed

 
Sam Pauken
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to print out each piece from the txt file (data below). In my print method, I have it set to divide by three to get the average, but I'm not sure how to get the first number from each line down the line. I have the below, but something tells me I'm off the mark.



1245.67,1490.07,1679.87,2371.46,1783.92,1461.99,2059.77
2541.36,2965.88,1965.32,1845.23,7021.11,9652.74,1469.36
2513.45,1963.22,1568.35,1966.35,1893.25,1025.36,1128.36
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37507
548
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sam,
You have the String
1245.67,1490.07,1679.87,2371.46,1783.92,1461.99,2059.77
and want to get
1245.67 out of it, right?

This has two steps. Get "1245.67" as a string out of it (take a look at the split method) and then convert it a numeric value. (BigDecimal is a good choice.)

Try to code something with these pointers and post back. Or ask follow up questions.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alternative technique (you will quickly learn there are several ways to do just about anything):- Use a Scanner object. I think you can easily adapt the example in the Java Tutorials to your requirements; I think there is enough information there that you can even use commas. A Scanner will return a BigDecimal directly;; look through its . If you are not familiar with BigDecimal, try this thread and the other link I posted there. Read the whole thread, because some of the earlier version shown there don't work properly.
 
Sam Pauken
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne, I have a text file (I've already imported the data), and I'm trying to get 1245.67,2541.36, and 2513.45, the first number from each line until I reach the end. This was not originally a String.
 
Tapas Chand
Ranch Hand
Posts: 614
9
BSD Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can probably do a split to get the first number from each line
In your for loop :
 
Tyson Lindner
Ranch Hand
Posts: 211
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think first you have to do a split with the new line character "\n". Then for each String you have a few different choices:

1. Just use substring() to get the first 7 characters
2. use a regex pattern
3. get the location of the first comma and get the substring() before it

Or you could use a regex pattern to get every 8th number in the total file, but that would obviously cause problems if some lines are less than 7 numbers.
 
Tyson Lindner
Ranch Hand
Posts: 211
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sam Pauken wrote:Jeanne, I have a text file (I've already imported the data), and I'm trying to get 1245.67,2541.36, and 2513.45, the first number from each line until I reach the end. This was not originally a String.


Actually I think I see the problem now. So each Sales object is a single number in the text file?

Again, IF you're sure all your lines are exactly 7 numbers, you can call size() on your array and then iterate through it, So the number in every 8th interation in your ArrayList will be the first on the line (except for the very first number). So you can create new 7 member arrays and perform the calculation on them as you go.

Another option is to have a nested for loop for every 7 numbers.

If your lines aren't of the same size however, you're going to have to re-parse the file to get that "\n" character back.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not convinced, I am afraid.

The \n character does not constitute a line end. On Windows® it constitutes only part of a line end, and older Macs (hardly any of them still in use) didn't use \n at all. You might and might not get errors from the last token on the line, don't know.
You don't know that the first 7 characters represent the first number; it might be 6 or 8 characters.
Other suggestions:-
You can read the whole line (=a String) with a Scanner (see Scanner methods). You can pass the string to the constructor of another Scanner and use a nextXXX method on it. You may have to change the delimiter to use commas, but I have already shown you where you can find that.

You can read a whole line and split it into a String[] with a method of the String class. Again you would need a delimiter. I would suggest comma followed by any amount of whitespace.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote: . . . A Scanner will return a BigDecimal directly;; look through its . If . . .
That should have said look through its methods.
 
Sam Pauken
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for all the responses. Just to clarify what I'm trying to do. I have n lines of data, each which are KNOWN to be seven piece of data long. I want to get data piece number 1 from each line, then piece 2, etc. until I reach data piece number 7. Here is what I have now. (I'm cramming this in during my lunch break at work so I haven't been able to test this yet.)

>
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use the delimiter it said in the tutorials, then you can have comma‑space.
 
Tyson Lindner
Ranch Hand
Posts: 211
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
The \n character does not constitute a line end. On Windows® it constitutes only part of a line end, and older Macs (hardly any of them still in use) didn't use \n at all. You might and might not get errors from the last token on the line, don't know.


I use a class from a textbook that uses "\n" and it seems to work fine. But yeah its probably way better to just use Scanner then try and mess with it yourself.
 
Sam Pauken
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's what I have now. I don't know if I'm using the delimiter correctly or not. I also haven't been able to get the "s" into my print statement either. I'm pretty sure I should be able to remove the .split(",") from my print statement and I'm also pretty sure I won't need the private method at the end, but here's what I've got so far. What does everyone think?

>
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where does the List come from? You said earlier the details were in a text file.
 
Sam Pauken
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
They are in a text file. I have readText and writeText methods written. Is what I've written not getting me to what I want to do?

I've also written this as an alternative.

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!