Hey all, I've got a file that I'm reading into my program to create fruit objects. Each fruit object contains different nutritional information, so I have 23 different apple objects with one containing calories, another protein info, and so on... The same is true for each type of fruit. I am trying to add these objects of fruit to an arraylist so that each fruit object containing the same nutritional information will be at the same arraylist index. Is this even possible? If so I would greatly appreciate the help in figuring this out. I have included the piece of code that I'm having issues with.
Why are you using StringTokenizer? Even tokenizer itself says not to use it.
Then you are mixing it with a Scanner, which is probably what you should use. Why are you changing the delimiter on the Scanner? If you want it line by line, with the nextLine method, then you don't need to use the delimiter because you are always using line ends. You can instead use the Scanner to find next, nextInt, nextInt, etc etc for fruit name, calories, mg of Vitamin C etc. But that depends on your text file having a certain format.
There is something wrong about the loop with hasNextFloat. You shoudn't use floats in the first place; use doubles instead unless something forces you to use a float. I think you can dispense with that loop altogether because you probably get 1 floating‑point number per line, so the loop is unnecessary. Otherwise, if you have several floats together, the loop will create several Fruit objects with the same name but different calories and add them all to the List.
You cannot add things with the same nutritional content to a List at the same index. Lists don't work that way. There is however another sort of data structure which allows you to map calorie content to fruit. Or even better to a List of Fruit. You can read about it in the Java™ Tutorials.
But I am at a bit of a loss to understand about different types of nutritional information. Do you mean you want lots of data structures, or do you want to sort your List so you get all the fruit in order of calories, or in order of Vitamin C content? That is easy enough to do, and you can have several copies of the List and sort them differently. The elegant way to do that is with Streams but you have probably never seen a Stream in use. If you have enough time, you will probably find all those things in the Java™ Tutorials link I gave you.
I'm not very good with java which is why my code is all over the place . All of the nutritional values are to be in float form and I am trying to sort my list so that all fruit is in the order of nutritional info (calories, vitamin C, etc.). Each nutritional information is assigned a number 1-23 so that a user can decide which type of nutritional info they want to see and then I print out all the fruits with just that piece of nutritional information for each. Really do appreciate the help!
Knute Snortum wrote:You can use a regex to parse the line, although regexes are hard to use at first. If you want to go this way I can help you along.
I really do appreciate the help/offer. Been at this for the past week and hate asking for help prior to trying everything I can think of to solve the problem, unfortunately ran out of time with this and had to submit it as is. But would still love to try and figure it out just not right this second since I've been here for the past 11 hrs working on this. I'm really hoping that I get better at this stuff as I go cause right now I feel like a deer in headlights and not something I want to feel about something that I plan on using in a career.
Again thanks for the help guys and will keep working at this stuff for sure. However, if you can recommend any helpful sites to practice my coding I would REALLY, REALLY appreciate it!
Good grief! That file is enough to put me off my beer.
So, you have a name followed by a (
Try splitting that line (got with Scanner#nextLine) on (; you can probably do it withString.split("\\("); If you look up String#split, you find it takes aString called regex as its parameter. That is a regular expression for a ( but ( is a metacharacter so you must escape it with \ and then you have to escape the \ again. Hence "\\(". You now have a 2‑element array and use String#trim to get rid of leading and trailing spaces. Repeat the procedure for the 2nd element of the array and you get amount. You can probably get a 3‑element array with String.split("\\(|\\)"); instead.
Pass the remainder of the line to a Scanner and call nextInt/nextDouble then you might try setCalories, setFibre, setSodium, setSun, setMoon, setFast, setSquare, setVitaminC etc.
Let's start this way. Create code that will read the file line line at a time. You can do that with Scanner without setting any custom delimiters.
Now how can we describe each line after the header? Something like:
1) a word 2) a phrase surrounded by parentheses
3) 23 numbers
Each of these tokens is surrounded by whitespace. The phrase is harder because it has whitespace in it, but we can get around that. Do we know if it will always be "(" + digit + whitespace + word + ")"?
Now some things to know about java.util.regex.Pattern (simplified): \d matches a digit, \w matches an alphabetic character (and underscore, but we can ignore that). \s matches whitespace. A dot means "any character" so if you want a literal dot, you write "\." (without quotes). Parentheses are a way of grouping matches together so you can get individual values. You have to "escape" a parenthesis if you want a literal: \( or \). Putting a * next to something means "zero or more of these." Putting a + next to something means "one or more of these."
So regex to match one or more word characters would look like this: \w+ If we put that in a Java String, we have to escape the \ because Java Strings have their own escape characters, so it would look like this:
Next we need whitespace, so our pattern looks like this:
We want to "capture" just the word, not the whitespace, so we surround the pattern that matches the word with parentheses. (These are not literal parentheses.)
Can you build more of the pattern from here? Maybe try a pattern that matches a number with a decimal point. You can test your patterns like this:
Give it a try and post what you get.
All things are lawful, but not all things are profitable.