Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-09-2008, 02:34 AM
Member
 
Join Date: Apr 2008
Posts: 1
bunbun is on a distinguished road
Arrays
I'm pretty sure that I'm creating the array correctly but I dont know why the items won't add to the cart. And how would I print what the contents of the array? Please help! thanks



package shopping;

// ************************************************** *************
// Shop.java
// Uses the Item class to create items and add them to a shopping
// cart stored in an ArrayList.
// ************************************************** *************

// import ArrayList here
import java.text.NumberFormat;
import java.util.Scanner;
import java.util.ArrayList;

public class Shop {
public static void main(String[] args) {

//Declare and instantiate a variable cart as an ArrayList that can hold Item objects
Object cart[] = new Object[6];

Item item;
String itemName;
double itemPrice;
int quantity;
double totalPrice;

cart.add(item);
cart.add(itemName);
cart.add(itemPrice);
cart.add(quantity);
cart.add(totalPrice);

System.out.println("Welcome to Shopper's Paradise");
System.out.println();

Scanner scan = new Scanner(System.in);

String keepShopping = "y";

do {
System.out.print("Enter the name of the item: ");
itemName = scan.nextLine();

System.out.print("Enter the unit price: ");
itemPrice = scan.nextDouble();

System.out.print("Enter the quantity: ");
quantity = scan.nextInt();

// *** create a new item and add it to the cart
Object newItem ="Hammer";
cart[6] = "Hammer";


totalPrice = 0;
System.out.println("\nCurrent Cart");

// *** print the contents of the cart object

// *** print the total price of the cart
System.out.println(totalPrice += item.getQuantity() * item.getPrice());

System.out.println();
System.out.print("Continue shopping (y/n)? ");
scan.nextLine();
keepShopping = scan.nextLine();
} while (keepShopping.equals("y"));

}//end of main method
}//end of Shop class
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 04-09-2008, 04:24 AM
CaptainMorgan's Avatar
Moderator
 
Join Date: Dec 2007
Location: NewEngland, US
Posts: 841
CaptainMorgan will become famous soon enoughCaptainMorgan will become famous soon enough
Send a message via AIM to CaptainMorgan
I'm afraid without seeing the Item class, I can't see what's wrong with your printout. You make the calls to the methods that add, and you attempt to print out the number of items... so we'll need to see your Item class. Please make an attempt at writing the printing code. From there, we can critique and/or correct you. Help yourself before we help you.

Welcome to the Java Forums!

Please have a look at the FAQ before you post again. And, please make use of the [ code] [ /code] tags when posting code- and to see if you used the tags correctly, use the "preview" option when posting.

See you around!
__________________

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
to our beloved Java Forums!
(closes on September 4, 2008)
Want to voice your opinion on your IDE/Editor of choice?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
!
Got a little Capt'n in you? (drink responsibly)
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
new to arrays jimJohnson New To Java 1 04-08-2008 04:45 PM
question about arrays broganm1 New To Java 3 02-13-2008 04:29 AM
2D-Arrays kbyrne New To Java 1 02-08-2008 12:08 AM
arrays help Warren New To Java 6 11-23-2007 09:23 PM
Problems with arrays Marcus New To Java 2 07-04-2007 10:10 AM


All times are GMT +3. The time now is 10:48 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org