Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Beginning Java and the fly likes Using Other Classes in my Main? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Using Other Classes in my Main?" Watch "Using Other Classes in my Main?" New topic
Author

Using Other Classes in my Main?

andi miami
Greenhorn

Joined: Oct 05, 2011
Posts: 10
Hello! I am a student (obviously, haha), and I am confused about how I need to do something in my driver class. I have a text file that I am reading in, then I decided to use a string tokenizer to parse the data. Now, I need to store the data to something, and then I have two other classes that I had to create in accordance with lab specs, and use them to get a specific output. I know the classes don't interact, but once I parse this string, how should I store the data and then call the other classes to use this data to get my output? I can't find an example of what I am trying to do. Which means I obviously don't know wth I am doing, because I am sure there are PLENTY of examples.
input file:
Chocolate Cake,260,3,12,8
Apple Pie,420,15,4,40
Lemon Meringue,80,0,2,11


Expected Output once executed correctly:

Dessert: Chocolate Cake
Cals: 260.0
Fat: 3.0g
Protein: 12.0g
Carbs: 8.0g

Dessert: Apple Pie
Cals: 420.0
Fat: 15.0g
Protein: 4.0g
Carbs: 40.0g

Dessert: Lemon Meringue
Cals: 80.0
Fat: 0.0g
Protein: 2.0g
Carbs: 11.0g

DONE

Main:



Dessert Class:



FoodFacts Class



Thanks, if anyone can help!
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3071
    
  33

You skip using StringTokenizer (its a old class, which is not encouraged to be used anymore) and instead use the split() function present in the String class- it does the same operation and returns a string array of tokens.

You are doing it right- Having a Desert class and FoodFacts class.

Now you need to collect the instances of Desert and FoodFacts somewhere so that you can later use it to print the data.

So think of using a List<Desert> and then once you create the instance of Desert class you add it to the list. Then once you are done with the complete parsing of the file then you can loop through the list and then print out the contents (basically override toString of both the classes as you have done in the code provided and then just pass the instance name in the System.out.println() which would invoke the toString()).


Mohamed Sanaulla | My Blog
andi miami
Greenhorn

Joined: Oct 05, 2011
Posts: 10
Thanks for the reply! I actually have to use the tokenizer as part of my lab specs. I don't know why, but it is part of the lesson, I guess. The part where you say to collect the instances is the part I guess I don't understand how to do. Can you give me an example? Or do I just create the list? After I declared the use if the FoodInfo and Dessert class? Sorry for being such an airhead. I think I looked up so many different ways to do this I have gotten myself quite confused, and my textbook doesn't actually seem to get this advanced. It isn't any help at all.
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3071
    
  33

Ok you can do something like this:


Note: Can you please edit the code you have posted earlier. There seems to be extra code copies which is increasing the length of each line.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39415
    
  28
andi miami wrote:. . . I actually have to use the tokenizer as part of my lab specs. I don't know why, . . .
Because your teacher has read an old book? It is worth pointing out that you are using legacy code which you ought to replace with something else. If you are reading from a text file, the java.util.Scanner class is better still than String#split().

And did you really mean to post that illegible block of text, without obvious new lines in? It took me ages to get it legible; the code tags keep it as one line, so you can't read it without scrolling left and right. By the way, never use tabs in indentation. Use spaces and set your editor to convert tabs to 4 spaces automatically. You have gone one worse than tabs for indenting: mixing tabs and multiple spaces.

And welcome to the Ranch
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39415
    
  28
Another thing lots of people don’t seem to know is that you ought not to use \n. It is explained briefly here. I would change the toString method to readThe link I gave you explains what the better-known % tags mean.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39415
    
  28
Mohamed Sanaulla wrote: . . . Note: Can you please edit the code you have posted earlier. There seems to be extra code copies which is increasing the length of each line.
I have done that. At least you can now read the duplicates
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3071
    
  33

Campbell Ritchie wrote:
Mohamed Sanaulla wrote: . . . Note: Can you please edit the code you have posted earlier. There seems to be extra code copies which is increasing the length of each line.
I have done that. At least you can now read the duplicates

Thanks a lot Campbell.
There's one more line which is causing to scroll sideways to read the posts. May be that line has to be wrapped to new line as well.
andi miami
Greenhorn

Joined: Oct 05, 2011
Posts: 10
Thanks you guys SO much! I think part of my problem with this language is that are really just SO many ways you can go about doing something, so I think I was ttring to incorporate way too much stuff into my code. I read what you said about the \n. I didn't know that at all! I also didn't know string tokenizer was outdated. Can you guys recommend a book to me that I can use as a reference? I heard the Java Cookbook or something like the works well. Anyway, after HOURS of suffering (Haha), this is what I came up with... I know it probably isn't the best way, but it's good enough for a grade.
And I'm sorry about how my code came out! Don't know what I did to make that happen...OOPS!!!
andi miami
Greenhorn

Joined: Oct 05, 2011
Posts: 10
Also, with the spaces and indentation, I just did the CTL+i thing on Eclipse and let it do the "proper indentation" for me. Do you think that's ok? And what do you mean legacy code? OMG...Java is so tricky. It is very self-esteem lowering. I think I like C++ better. The way my old professor taught was very rigid. Rules! Rules! Rules! This more "free" way my new prof. teaches leaves too much guess work! Ha! But I guess that is the point... Thanks again for all your help...
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3071
    
  33

There are 2 things I would like to convey:
1. I dont think you should be iterating through the Vector in the while loop. Try to populate the vector with the required information. And once you have completed reading the file and populated the Vector then you can iterate through it and get the elements from the vector.
For that move the Iteration through the vector out of the outermost While loop.
Also you are creating a Vector for each of the token. So you can create the vector at the beginning of the main, i.e before the outermost while loop

2. You need not use an iterator. Instead read about for-each loop (advanced for loop), that way you can iterate through the Vector like:
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3071
    
  33

And for the books to learn Java- If you are a beginner in Java Head First Java is good (SearchFirst regarding java books for beginners would give you lot of results).
Also checkout the JavaRanch Bunkhouse for loads of Books reviews.

Ideally for queries like- How to learn Java, which books to refer to - The easiest way to find suitable responses is to - SearchFirst in the forum.
andi miami
Greenhorn

Joined: Oct 05, 2011
Posts: 10
Whew!!! I have a lot to learn!!! Thanks for the advice! I have a lab every week, so I will be referring to this site a lot. I will check out the links and books. Thanks for the corrections on the loop as well.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39415
    
  28
Mohamed Sanaulla wrote: . . . Thanks a lot Campbell.
There's one more line which is causing to scroll sideways to read the posts. May be that line has to be wrapped to new line as well.
You’re welcome I didn't notice that line; maybe my screen is wider than yours. There was some redundant whitespace on some lines, which I got rid of. Hope that will help.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39415
    
  28
Why are you using Vector rather than ArrayList? Again Vector is usually (but not officially) regarded as legacy code.
Also that main() method looks too long to me. You should divide it into two methods at least, one which reads the file and puts the Dessert objects into the List, and the other which reads them from the List and displays them. Put them in another class, maybe Menu. Then you initialise your List, and possibly a location for the file, in the constructor. Now move the main() method into another class. In the main method you create a Menu object, and call its methods. That gets the main() method down to three lines. My ideal is for the main() method to contain one line. I have never got an application to run correctly when the main method had fewer lines than one in .
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3071
    
  33

Campbell Ritchie wrote:... I didn't notice that line; maybe my screen is wider than yours ...

yeah, I was viewing it in a 10 inch screen space
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3071
    
  33

Campbell Ritchie wrote:Why are you using Vector rather than ArrayList? Again Vector is usually (but not officially) regarded as legacy code.
Also that main() method looks too long to me. You should divide it into two methods at least, one which reads the file and puts the Dessert objects into the List, and the other which reads them from the List and displays them. Put them in another class, maybe Menu. Then you initialise your List, and possibly a location for the file, in the constructor. Now move the main() method into another class. In the main method you create a Menu object, and call its methods. That gets the main() method down to three lines. My ideal is for the main() method to contain one line. I have never got an application to run correctly when the main method had fewer lines than one in .

agree with both the points. More over the methods of the Vector class are synchronized and those that of ArrayList are not. More differences can be found here.

Also moving the related code into methods or may be into classes of their own will help keep the methods simple. So if there is any issue say in reading from file, you can go to that method and then rectify it, instead of searching through the main(). Also keeping limiting classes to Single responsibility is a good approach ( Read SOLID OO Design principles)

Hope this helps you in designing the code for your future assignments. Its better to start learning good coding practices from the initial coding years so that it becomes a habit and your coding life becomes easier when you go on to work or write code for a larger code base.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39415
    
  28
I’ve got an 11-inch screen. Size matters
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4347
    
    2

i posted a question here once about an old homework i was improving. someone asked the same question. why are you using a Vector? the answer is because i was told to.


SCJP
Visit my download page
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using Other Classes in my Main?