wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes looping into an array help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "looping into an array help" Watch "looping into an array help" New topic
Author

looping into an array help

Vincent Micchia
Greenhorn

Joined: Nov 09, 2011
Posts: 10

Hey there working on learning jsp. I have this code that takes CSVs from a text area and right now seperates then by commas and \n then prints it out. Where I'm stuck is that I need to either put all the values into an array and be able to check every other value or put evey other value into a seperate array (not sure which would make more sense). So i guess the big thing is I can seperate the value and print them but I'm just not understanding how to put them into an array. Any help or pointing in the right direction is much appreciated.

Here's the code I have now:
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61094
    
  66

Why is this code in a JSP? It should be in a servlet or other Java class.

In any case, this isn't a JSP question, so it's been moved to Beginning Java.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

I don't understand your question. You seem to be aware of two different options to achieve what you want, so where are you stuck? Did you try either or both of them? Were you unable to make them work?

If you're having difficulty, provide an SSCCE (a simple standalone Java app that shows this problem, since it has nothing whatsoever to do with JSPs) and indicate clearly where you're having trouble.

I will point out, however, that just taking a slightly educated guess at what you're trying to do, it looks like both your approaches are icky.

You have something like this, I assume:

Hamburger,3.50
Cheeseburger,4.00
Mooseburger,8.95
Cup-o-java,1.50


That is, for example, a multiline string of product names and prices. And you're looking to do something like one of the following:

[Hamburger, 3.50, Cheeseburger, 4.00, ...]
OR
[Hamburger, Cheeseburger, ...], [3.50, 4.00, ...]
OR
[Hamburger, ..., Cup-o-java, 3.50, ..., 1.50]


That is, put all the data in one array, alternating product name and product price, or two arrays, one for names and one for prices, or one array with all the names followed by all the prices.

If this is the case, then don't do it. Unless you're immediately feeding the resulting arrays to some other code that expects the data to be in that form and that you can't change to take it in some other way, it will just cause you headaches, and it defeats part of the purpose of using an OO language--encapsulation.

You want to define a class, say, Product, with fields for the name and price. For each line, create a Product instance, and fill in its data with the values from the line. You then have a single array of Products. Or, if you are familiar with or ready to learn Collections, use a List<Product>.

Or am I totally off base with regard to your problem?
Vincent Micchia
Greenhorn

Joined: Nov 09, 2011
Posts: 10

No you are correct. What I want to do is then feed it into a secondary hidden form of a different layout. So basically I want to take these values and put them into seperate text boxes. I know it sounds odd but it seems the best solution to my current problem.

So it would be something like: <input type="hidden" name="prod1" value="hamburger"><input type="hidden" name="price1" value="3.00">...

So maybe the whole array idea isn't even the way to go. I guess my real problem is I can print the data out into one long string but I'm not entirely sure how to go about seperating it out into the seperate pieces.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

I still don't know what you're actually trying to accomplish or what difficulty you're having. Please provide an SSCCE that shows your best attempt. A comment is fine if you're absolutely flummoxed on what to do at some step. Also indicate clearly what you'd like the final result of executing the code to look like.
Vincent Micchia
Greenhorn

Joined: Nov 09, 2011
Posts: 10

Here's what I can get or at least what I think is right.

Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Your question has nothing to do with JSP. You shouldn't even put that code in a JSP in a first place. I asked you to show your code in a non-JSP SSCCE. You also need to indicate clearly what you'd like the end result to be. You've been told all this already. I'm sorry, but I'm not interested in looking at Java code that's in a JSP that shouldn't be, and if you ignore my requests to make it easier for me to understand your problem, I feel I'm wasting my time trying to help. I'll bow out now.

Good luck.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: looping into an array help