Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Creating a new object from an ArrayList of objects

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Creating a new object from an ArrayList of objects

    I have pasted my code below. I am attempting to create an Invoice object from an object stored in an ArrayList. I get the error message "not a statement" and "; expected" on line 61: Invoice newInvoice = new Invoice(clientList.get(count));

    Can't figure out what I'm doing wrong.

    import java.util.Scanner;
    import java.util.ArrayList;

    public class InvoiceDemo {

    public static void main(String[] args) {

    // VARIABLES ----------------------------------------------------
    int number, id, hours, clientID, count;
    String name, address;


    // --------------------------------------------------------------
    // Create scanner
    Scanner keyboard = new Scanner(System.in);

    // Create arraylist to hold client objects
    ArrayList<Client> clientList = new ArrayList<Client>();

    // Prompt user for selection - loop until valid input is received
    System.out.println("Please make a selection [1 - Add Client / 2 - Create Invoice / 3 - Quit]: ");
    number = keyboard.nextInt();

    // Prompt user for selection
    while (number != 3) {

    // If user selects 1, create a client
    if (number == 1) {
    // Get user input
    System.out.print("Please enter the client's name: ");
    keyboard.nextLine();
    name = keyboard.nextLine();
    System.out.print("Please enter the client's address: ");
    address = keyboard.nextLine();
    System.out.print("Please enter the client's numerical ID: ");
    id = keyboard.nextInt();
    System.out.print("Please enter the number of hours worked: ");
    hours = keyboard.nextInt();

    // Create client object
    clientList.add(new Client(name, address, id, hours));
    }

    // If user selects 2, create an invoice for the given client id
    else if (number == 2) {

    System.out.print("Please enter the client's numerical ID: ");
    clientID = keyboard.nextInt();

    for (count = 0; count < clientList.size(); count++ ) {

    if ((clientList.get(count).getID()) == clientID)
    Invoice newInvoice = new Invoice(clientList.get(count));
    }

    }

    // Prompt user for selection
    System.out.println("\nPlease make a selection [1 - Add Client / 2 - Create Invoice / 3 - Quit]: ");
    number = keyboard.nextInt();

    }
    }
    }


  2. #2
    Member
    Join Date
    Feb 2012
    Posts
    58
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default Re: Creating a new object from an ArrayList of objects

    You are missing the open bracket { for the if statement, just before the line 61.

Similar Threads

  1. Bubblesort of an ArrayList with objects
    By bondage in forum Algorithms & Recursion
    Replies: 9
    Last Post: January 4th, 2012, 10:51 AM
  2. Creating and implementing class for creating a calendar object
    By kumalh in forum Object Oriented Programming
    Replies: 3
    Last Post: July 29th, 2011, 08:40 AM
  3. Minimum value of objects in ArrayList
    By Sputnik in forum Loops & Control Statements
    Replies: 2
    Last Post: October 23rd, 2010, 01:20 PM
  4. Arraylist Objects to Database
    By frankycool in forum JDBC & Databases
    Replies: 3
    Last Post: November 15th, 2009, 07:01 PM
  5. Object creation from a input file and storing in an Array list
    By LabX in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: May 14th, 2009, 03:52 AM

Tags for this Thread