473,385 Members | 1,356 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

User Information into an Array (first ever array)

38
I have been reading through many of the array questions and cannot find one that addresses my issue. Maybe someone can help me out.
Same story, I am learning Java and have just written a CD Inventory application. It works, does what I want it to and all that, but now I need to put an array in there to store more than one cd at a time. Seems simple enough until I actually start coding. I want to save as much of the code as I can since I worked so hard to get it just the way I want it, but am not exactly sure of how an array will work in to it all.
All the examples I see on arrays have the information contained in that array entered when the array is created, example - int array[] = {1,2,3,4};.
That is great, but I want my array to use the fields I have already defined and store the information the user enters. Can this be done? Do I need to create a new class altogether, or can I use one of the two I already have and just add a method? Here are the two classes I currently have coded, any help would be appreciated.
Expand|Select|Wrap|Line Numbers
  1. import java.util.Scanner; //uses class Scanner
  2.  
  3. public class Inventory
  4. {// begin class Inventory
  5.     public static void main(String[] args)    
  6.     {//begin method main
  7.  
  8.     Compactdisk thisCompactdisk = new Compactdisk(); //call Compactdisk class
  9.  
  10.     Scanner input = new Scanner(System.in);  // create scanner
  11.  
  12.  
  13.             // begin display method
  14.             System.out.print("Enter CD Name or STOP to Exit: ");
  15.             thisCompactdisk.setCDName(input.next());  // read cd name
  16.  
  17.               while (!thisCompactdisk.getCDName().equalsIgnoreCase("STOP"))
  18.             {// begin main While
  19.  
  20.                 System.out.print("Enter Price of this CD: "); // prompt for price
  21.                 thisCompactdisk.setPrice(input.nextFloat());    // price input from user.
  22.                 while (thisCompactdisk.getPrice()<= 0)
  23.                 {// begin while
  24.                 System.out.print("Price Must Be Greater Than Zero. Enter Price: ");
  25.                 thisCompactdisk.setPrice(input.nextFloat()); // cd price loop from user.
  26.                 } // End while
  27.  
  28.                 System.out.print("Enter CD Item Number: "); // prompt for cd item number
  29.                 thisCompactdisk.setItemno(input.nextInt()); // cds item number input from user
  30.  
  31.                 System.out.print("Enter Number of these CDs in Stock: "); // prompt for cd stock
  32.                 thisCompactdisk.setNstock(input.nextInt()); // cds in stock input from user
  33.  
  34.  
  35.                 System.out.print("CD "+thisCompactdisk.getCDName()+", Item Number "+thisCompactdisk.getItemno()+","); // display name
  36.                 System.out.printf(" is worth %c%.2f.\n", '$', thisCompactdisk.getPrice()); // display individual price
  37.                 System.out.printf("We have %d copies in stock, making our inventory worth %c%.2f\n", thisCompactdisk.getNstock(), '$', thisCompactdisk.getValue()); //inventory value
  38.  
  39.                 System.out.print("Enter CD Name or STOP to Exit: "); // internal loop prompt
  40.                 thisCompactdisk.setCDName(input.next()); //name input from user
  41.  
  42.             } // End main While
  43.         System.out.print("Ending Program.");
  44.  
  45.  
  46.     }// end method main
  47. } // end class Payroll
and

Expand|Select|Wrap|Line Numbers
  1. public class Compactdisk
  2. {// begin class
  3.  
  4.  
  5.     //InventoryCD class has 5 fields
  6.     String cdName; //  cd name
  7.     float price; // price of cd
  8.     int itemno; // item number of cd
  9.     int nstock; // how many units in stock    
  10.  
  11.     //Compact disk class constructor
  12.     public Compactdisk()
  13.  
  14.         // 4 fields need to be set up
  15.         { 
  16.         cdName = "";
  17.         price = 0;
  18.         itemno = 0;
  19.         nstock = 0;
  20.         }
  21.  
  22.         // set values
  23.        public void setCDName(String diskName)
  24.        {
  25.        cdName = diskName;
  26.        }
  27.         public void setPrice(float cdPrice)
  28.        {
  29.        price = cdPrice;
  30.        }
  31.         public void setItemno(int cdItemno)
  32.        {
  33.        itemno = cdItemno;
  34.        }
  35.          public void setNstock(int cdStock)
  36.        {
  37.        nstock = cdStock;
  38.        }
  39.  
  40.        // return values
  41.         public String getCDName()
  42.         {    
  43.         return (cdName);
  44.         }
  45.         public float getPrice()
  46.         {    
  47.         return (price);
  48.         }
  49.         public int getItemno()
  50.         {    
  51.         return (itemno);
  52.         }
  53.         public int getNstock()
  54.         {    
  55.         return (nstock);
  56.         }
  57.  
  58.         // returns inventory value
  59.        public float getValue()
  60.        {
  61.        return(price * nstock);
  62.        }
  63.  
  64. }// end class
Jul 13 '07 #1
5 2562
JosAH
11,448 Expert 8TB
Would it be over your head to have a look at [b]ArrayList[b]s? The trouble with
arrays is that they have a fixed length (you have 101 CDs and an array of length
100; that means trouble).

Here's a hypothetical sketch of an example:
Expand|Select|Wrap|Line Numbers
  1. List<CD> inventory= new ArrayList<CD>();
  2. ...
  3. CD cd= new CD( ...);
  4. inventory.add(cd);
  5.  
Give it a try.

kind regards,

Jos
Jul 13 '07 #2
blazedaces
284 100+
Question, don't think this deserves it own thread, just a curiosity thing:

I've heard that arraylist is sort of the "new and improved" vector ... why is that? The only obvious difference I notice is that there aren't 3 methods that do the same things... perhaps it's more efficient in some way? Could someone shed some light on the subject?

Thanks for the help,

-blazed
Jul 13 '07 #3
JosAH
11,448 Expert 8TB
Question, don't think this deserves it own thread, just a curiosity thing:

I've heard that arraylist is sort of the "new and improved" vector ... why is that? The only obvious difference I notice is that there aren't 3 methods that do the same things... perhaps it's more efficient in some way? Could someone shed some light on the subject?

Thanks for the help,

-blazed
The Vector class is an old class; it already existed in Java 1.0. All of its public
methods are synchronized which seemed like a handy idea but it isn't, The
Collection Framework introduced the ArrayList class which basically did the
same thing; only a bit better: none of the methods are synchronized and the
reallocation scheme (when the array grows) is a bit better as well.

Nowadays the Vector class only exists as a retrofitted class so that it implements
the List interface, same as the ArrayList does. There's no need to use Vectors
anymore.

kind regards,

Jos
Jul 13 '07 #4
no1zson
38
Would it be over your head to have a look at [b]ArrayList[b]s? The trouble with
arrays is that they have a fixed length (you have 101 CDs and an array of length
100; that means trouble).

Here's a hypothetical sketch of an example:
Expand|Select|Wrap|Line Numbers
  1. List<CD> inventory= new ArrayList<CD>();
  2. ...
  3. CD cd= new CD( ...);
  4. inventory.add(cd);
  5.  
Give it a try.

kind regards,

Jos
Thanks for the reply. I will read anything I can get my hands on. Where is this "ArrayList" you speak of? It may be over my head but I will certainly give it a try none-the-less.
I do understand the fixed lenght confines of an array, but since this is more of a learning exercise I am not too concerned with that right now. I will not enter in more than 5 cds for what I am doing. I just want to learn how to do it.
Jul 13 '07 #5
JosAH
11,448 Expert 8TB
Here are the API documents. Note that you can download them all.
(look at the top right corner). An ArrayList is so much more convenient and using
Collection classes like this one may even influence your design and drag it away
from a Fortanesque way of thinking about programs to a more object oriented
way of thinking; do give it a try; it'll help you and ArrayLists aren't that difficult.

kind regards,

Jos
Jul 13 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

60
by: Fotios | last post by:
Hi guys, I have put together a flexible client-side user agent detector (written in js). I thought that some of you may find it useful. Code is here: http://fotios.cc/software/ua_detect.htm ...
58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
7
by: MBS | last post by:
Greetings. I'm still pretty new to PHP and I have a question. I know that variables are preceeded by a "$" (dollar sign). Typically, a variable has one value, unless it is an array. Then it is...
10
by: Kieran Simkin | last post by:
Hi, I wonder if anyone can help me, I've been headscratching for a few hours over this. Basically, I've defined a struct called cache_object: struct cache_object { char hostname; char ipaddr;...
0
by: Tom | last post by:
I am having a really annoying issue with serialization and a .NET User Control I am writing. For example, let's say I have a couple of classes in my control - first class is like: Public Class...
24
by: Rob R. Ainscough | last post by:
VS 2005 I have: ClickOnce deployment User's that hate and or don't want to use an IE Client (don't blame them) I don't see how ASPX web pages are going to survive? With .NET 2.0 and clickonce...
104
by: Leszek | last post by:
Hi. Is it possible in javascript to operate on an array without knowing how mamy elements it has? What i want to do is sending an array to a script, and this script should add all values from...
8
by: tlyczko | last post by:
I am developing an Access database that will be used by some users logging into Citrix servers. Using the "Code 1" listing, 'fGetFullNameOfLoggedUser' from Dev Ashish's site, which I found in...
22
by: Sandman | last post by:
So, I have this content management system I've developed myself. The system has a solid community part where members can register and then participate in forums, write weblogs and a ton of other...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.