Hi,

Yes, I am sure you can tell that I am new. I have a few question and hope someone will answer them. 1)I am a little confused as to where to put certain code. If I am doing inventory and I need to have an array does it go into a separate class? I need one class that just displays everything. I am not sure how to go about it and have been doing the tutorials at Oracle but still I remain confused. I would like someone to explain it without using a lot of tech terms if that is possible. 2) This is the code I have so far. The problem is when it prints the total stock value is right next to the price and not legible. I am at a loss as to how to format an array. 3)Does this satisfy having an array to show one product at a time? I appreciate any guidance that would be offered. I have read tutorials,watched tutorials and it is as if I understand a different language.

Thank you,

lynnajoe

package inventory23;
import java.text.*;
/**
 *
 * @author Lynnajoe
 */
public class inventory23 {
    private static int numInStk;
    private static int Quantity;
    private static double totalStkValue =numInStk*Quantity;

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        String[]headings;
        headings = new String [1];
        headings[0]]= "Name";
        System.out.printf("%s  %9s  %5s %8s %22s","Product","Stock#","Qty","Price","Total Stock Value\n");

        String []anArray;
        anArray = new String [4];
        anArray [0] = "Cabernet       01      5     14.99" + totalStkValue;
        anArray [1] = "Chardonnay     02      8     12.99" + totalStkValue;
        anArray [2] = "Merlot         03      10    11.99" + totalStkValue;
        anArray [3] = "Cola           04      5      6.99" + totalStkValue;


        System.out.println(""+ anArray[0]);
        System.out.println(""+ anArray[1]);
    System.out.println(""+ anArray[2]);
    }
}

1)I am a little confused as to where to put certain code. If I am doing inventory and I need to have an array does it go into a separate class?

you could, but since you're still learning and grasping at the concepts, I would advise against it, and first see if you can get your application to run in a single file before you try an object oriented approach.

2) This is the code I have so far. The problem is when it prints the total stock value is right next to the price and not legible. I am at a loss as to how to format an array.

If I understand your problem correctly, you just have to 'format' the String values you store into the array, it has nothing to do with formatting the array itself.

You must understand that a String Object, though handy, can be quite 'dumb' itself. You may think that something should appear since it looks logical to you, but unless it's stored in the Object, it will not.
If you want there to be a space between your original String and your total value, you'll have to program it in there.

for instance: change

anArray [0] = "Cabernet       01      5     14.99" + totalStkValue;

into

anArray [0] = "Cabernet       01      5     14.99   total: " + totalStkValue;

and you'll get an entirely different output.

3)Does this satisfy having an array to show one product at a time?

I honestly do not know what you mean by this question. I think you mean: do I need an array to show one product at a time.
well .. if you want the information of the elements to be available to you longer than just to read them, print them out on the screen and read in another one, and thereby overwriting the initial information, you will indeed need a list of some sort.
when you're just learning the language, an array is, IMO, a good way to start.

you can iterate over them, for instance to print. In such case, you could change your print statements to:

for ( int i = 0; i < anArray.length; i++ ){
  System.out.println("Item: " + anArray[i]);
}

keep in mind that the first element of an array has the index 0, not 1, and that the last element will always have as one less to the length of the array as index.

There are a few underlying things you need to understand.
One is that an array is a collection of items, and each element of the array is a single item.
Another thing is that you'll have an easier time of it if you handle your data, logic, and presentation separately.
So you're not going to format your array, you're going to read data out of the array and print it, and you're going to format the presentation of it. In order to make this work better, you'll want to separate your array into separate arrays for product, stock#, quantity, price. Now we have

anArray [0] = "Cabernet 01 5 14.99" + totalStkValue;

becomes

String[] productName = String[4];
int stockNumber = int [4];
int qty = int[4];
int price = int[4]; // int? yes, int - store price as cents, to preserve precision


productName[0] = "Cabernet"
stockNumber[0] = 1;
qty[0] = 5;
price[0] = 1499;

So now you can calculate stuff like the actual value of the stock of cabernet ( = qty[0] * price[0]), and you can monitor quantity, so you'll know when your stock of Cabernet goes below 3 bottles, and stuff like that.

This also allows you to format your data nicely for printing. Look up the printf method in the String class for the easiest way to make this work - you'll have to do a bit of experimenting to get the hang of it, but this will allow you to create a template string and drop values into it.

Try playing with this stuff - it might seem more complicated, but it'll actually allow you to do what you're trying to do here, which your current approach won't allow.

I'm not going to tell you this yet, but later on I'll point out that this is not a very good way to build this, but it'll work for this toy application, and it'll get you used to arrays and string formatting, which is what you need right now.

I want to thank you for answering my questions so perfectly. This is what I wanted to know. I noticed that many beginners like to just put the code in but do not really understand what is going on with the code. Your explanation has truly answered some basic but important concepts of Java. Thank you again for your help.

lynnajoe

Hi again,

I wanted to thank the other person who replied which was stultuske. Thank you and I meant the comments for both of you.

lynnajoe

Glad to help. It's always nice when someone is able to take on stuff like this and get something out of it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.