0

newbie here

Is this the way to populate an ArrayList with objects(code below)? In the code below I've managed to read the information in the file of my choice and populate it with words. It appears in this format:

App name: Barcode Scanner

Developer name: WizardTech

Function: Scans bar-codes.

Type: Utility

Cost: 7.0

Popularity: 0

========================================================

I want to be able to get the attributes of the objects and use them like any other variable in the code. Is it possible to do this with the code I have right now?

For example, I've got an attribute named 'Cost' which belongs to the object and I want to get that attribute from the object in the ArrayList and use it for a calculation.

Here's the code:

public class c {

    public static void wholeSystem3() {
        boolean choice = true;
        int input6 = 0;
        Shop customerConfig = new Shop(new ArrayList<Customer>());
        while (choice == true) {
            System.out.println("Proceed to Customer creation by entering 1");
            Scanner myScan1 = new Scanner(System.in);
            input6 = myScan1.nextInt();
            if (input6 == 1) {
                System.out.println("Enter your information:");
                Scanner myScan0 = new Scanner(System.in);
                Customer myCustomer = new Customer();
                System.out.println("Please enter your name:");
                myCustomer.setCustomerName(myScan0.nextLine());
                System.out.println("Please enter your address:");
                myCustomer.setAddress(myScan0.nextLine());
                System.out.println("Please enter your profession:");
                myCustomer.setProfession(myScan0.nextLine());
                System.out.println(myCustomer.getCustomerName() + " " + myCustomer.getAddress());
                customerConfig.addCustomers(myCustomer);

                File CustomerList = new File("CustomerList");
                try {

                    BufferedWriter out;

                    out = new BufferedWriter(new FileWriter(CustomerList, true));

                    out.write("Customer name:" + myCustomer.getCustomerName());
                    out.newLine();
                    out.write("Customer Address:" + myCustomer.getAddress());
                    out.newLine();
                    out.write("Customer Profession:" + myCustomer.getProfession());
                    out.newLine();
                    //Close the output
                    out.close();
                } catch (Exception e) {//Catch exception if any
                    System.err.println("Error: " + e.getMessage());
                }

                System.out.println("Would you like to immediately purchase an app?");
                input6 = myScan0.nextInt();
                if (input6 == 1) {
                    String appStoreFile = "AppStore"; //"AppStore" Name of file I wish to take objects from to populate ArrayList
                    String line;
                    ArrayList testList = new ArrayList();


                    try {

                        BufferedReader input = new BufferedReader(new FileReader(appStoreFile));
                        if (!input.ready()) {
                            throw new IOException();
                        }
                        while ((line = input.readLine()) != null) {
                            testList.add(line);
                        }

                        input.close();
                    } catch (IOException b) {
                        System.out.println(b);
                    }

                }

            }

        }


    }
}
mkobit
  • 43,979
  • 12
  • 156
  • 150
  • 4
    You posted way too much code. If your question is if you can access objects you've put in a list, the answer is yes. Or is it that you want to construct objects to put in a list? – keyser Dec 15 '14 at 19:40
  • Yeah I wanted to know if I can access the objects and their variables/attributes from the ArrayList when I attempted to read them from the file. – user3479469 Dec 15 '14 at 19:45

3 Answers3

0

When you read the file line by line, you should parse it. That means to turn it into a number. Here's a link on how that works: Convert string to float?

Pass the characters that come after "Cost:" to that method. It should return a number that you can do calculations with.

Here's a simple program operating on one of your lines:

public class TextParser
{
    public static void main(String[] args)
    {
        String line = "Cost: 7.0";

        // this will not work, it tries to parse the whole line
        //float number1 = Float.valueOf(line);

        // this works and returns 7 as a number
        float number2 = Float.valueOf(line.substring(6));

        System.out.println("the number is: " + number2);

        // you can now do calculations with it:
        System.out.println(number2 + 5);
        System.out.println(number2 * 12.345);
    }
}

In the example, I hard coded the portion of the line that is representing a number. A better way to do this is to use regular expressions to filter out the parts of the String that could be a number. This is a lot more flexible but also a little complex and maybe a bit over the top for the beginning. But definitely look into RegEx, it is very useful.

Community
  • 1
  • 1
null
  • 5,207
  • 1
  • 19
  • 35
0

As keyser pointed out, you posted too much code. However, from trying to discern from what you are trying to do, you are trying to add objects into your ArrayList, and then retrieve an attribute from a specific object within the ArrayList.

To add an object into the ArrayList, you can add by using the ArrayList's add method:

yourArrayListName.add(Object name);

or

yourArrayListName.add(int index, Object name);

Note that if you use the latter, you will need to keep track of whether the index you are trying to retrieve from is null or not. The former will simply add in order starting from index 0,1,2...

To retrieve the object within the ArrayList, you can use the ArrayList's get method:

yourArrayListName.get(int index).getAttribute(); // getAttribute retrieves the value you want

Note that I used a get method to get the value you wanted. It is bad programming practice to directly retrieve a value from an object. If you really wanted to, however, you can directly use the attribute variable name in place of getAttribute(), assuming it is declared public.

Here is the documentation:

http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html

keyser
  • 18,829
  • 16
  • 59
  • 101
David Kim
  • 1
  • 1
0

you havent posted details about shop class but i assume that as there is customerConfig.addCustomers method, similarly there woule be customerConfig.getCustomers which you can use as follows. e.g. for first customer:

customerConfig.getCustomers().get(0).getCost();
Pranalee
  • 3,389
  • 3
  • 22
  • 36