I am working on a basic classified ad system that when used prompts the user with several questions. The user input answering those questions is then printed out when the user enters a show command. I am trying to figure out a way to store the previous input while still saving future input so that when the user calls the method to "show" all the ads, all the ads print out. Here is the code I have for creating the ad:

   /**
     * 
     */
    private String addItem()
     {
       Scanner scanRead = new Scanner(System.in);
       System.out.println("What is your username?");
       username = scanRead.nextLine();
       System.out.println("What is the title of the ad you are posting?");
       title = scanRead.nextLine();
       System.out.println("Type your description here: ");
       description = scanRead.nextLine();
       System.out.println("How much are you asking for the item?");
       price = scanRead.nextLine();
       ad = username + title + description + price;
       return null;
     }

Then I just have a simple print method to display the ads:

    /**
     * Display the details of this post.
     * 
     */
    public void display()
    {
        System.out.println(ad);
    }

Recommended Answers

All 4 Replies

You can use a String array to hold all the previous answers and print them later. An ArrayList of Strings would be even better, if you have got that far in your Java course.

Thank you for the help! I just have another question that I hope you or someone else will be able to answer. How do I print each ad with its respective parts rather than printing all the usernames together, all the titles together, etc?
This is the code I have now:

 static ArrayList <String> newName = new ArrayList <String>();
 static ArrayList <String> newTitle = new ArrayList <String>();
 static ArrayList <String> newDescription = new ArrayList            <String>();
 static ArrayList <String> newPrice = new ArrayList <String>();
 static ArrayList <String> contact = new ArrayList <String>();



public static void ClassifiedAd (String[] args)
    {
       Scanner scanRead = new Scanner(System.in);
       System.out.println("What is your username?");
       newName.add(scanRead.nextLine());
       System.out.println("What is the title of the ad you are                                 posting?");
       newTitle.add(scanRead.nextLine());
       System.out.println("Type your description here: ");
       newDescription.add(scanRead.nextLine());
       System.out.println("How much are you asking for the                                     item?");
       newPrice.add(scanRead.nextLine());
       System.out.println("Enter your email address or phone                                   number.");
       contact.add(scanRead.nextLine());
    }

Then:

    public void display()
    {
        System.out.println(newName);
        System.out.println(newTitle);
        System.out.println(newDescription);
        System.out.println(newPrice);
        System.out.println(contact);
    }

This is where object oriented programming comes in.
Rather than have those separate arrays, in Java you would define an Ad class, with name, title. description etc as its instance variables. As the user enters data you create instances of that class, and add them to an array or ArrayList of Ads

Thanks!

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.