That is a lot of my problem right there, at this point I am not even sure what I should do. This is my first attempt at GUI so I wanted to stay as basic as possible, and keep as much of my existing code as possible because I understand it as it is (for the most part.)
I have never used JList either, so between that and GUI both at once I am just getting confused as to what is changing by implimenting one, and what changes because of the other.
I am looking at the code I have attempted today, some for GUI frames, some for buttons, some for JList ... all of it new, and none of it clear.
I think I am going to delete everything I have tried up to this point, and just start over. :o(
no1zson 24 Posting Whiz in Training
no1zson 24 Posting Whiz in Training
I did have concerns about the toString, but again fall into unfamiliar territory with that function.
I do understand what you say about the list being empty, which goes back to an overall question about how the GUI pulls information, and from where.
I think I am way over my head here, I think I probably need to scrap the JLIST and just go back to the original plan and insert everything into a new gui class that uses my Compactdisk and Cdwartist classes.
Right now I am trying to build on a foundation I have not properly laid yet.
1st thing I need to do is get a QUI screen with some fields in it where I can enter information into my existing array.
Then I will worry about sorting, adding and all that other stuff.
Would you suggest I take everything from Inventory and just re-write it into my new class, so that I do not use Inventory anymore?
no1zson 24 Posting Whiz in Training
I hate that site. I was trying to read up on arrays there this morning. Just made me more confused.
I know it is helpful to people that already understand what is going on, but for me, being the first time I have ever attempted any of this, it is vague examples I do not understand.
It is good for giving me an idea of what I should try, but when I have specific questions it just makes my head swim ... like now.
no1zson 24 Posting Whiz in Training
Yeah, I am seeing that. I want to try and keep this as simple as possible. Too complicated and I am just going to frustrate myself.
I found the misspellings and the parenthesis in my last post, I think you were looking at code I posted before I found them. I did put the Button code in my JFrame extention also.
Editing the loop will be the next step, but first I have to get this list working and I am still getting the same error on the same line.
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class GuicdInventory extends JFrame
{
Inventory guicdInventory = new Inventory();
private Jlist cDJList;
//contructor adds Inventory to JFrame
public GuicdInventory()
{
super("CD Inventory");
setLayout(new FlowLayout()); // set frame layout
// adds buttons
addButton = new Button("Add");
add (addButton);
// create an object to listen to buttons
ButtonListener myButtonListener = new ButtonListener();
cDJList = new JList(Object[] guicdInventory);
cDJList.setVisibleRowCount(99);
add(new JScrollPane(cDJList));
} // end constructor
public static void main(String args[])
{
GuicdInventory guicdInentory = new GuicdInventory;
GuicdInventory.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GuicdInventory.setSize(500,800);
GuicdInventory.setVisible(true);
// tell buttons that myButtonListener should be notified
addButton.addActionListener(myButtonListener);
setVisible(true);
} // end
} // end main
I am going to go to lunch before I get too aggravated. Maybe it will look differently when I get back! :o)
no1zson 24 Posting Whiz in Training
Well, I had noticed buttons before, so I think this is pretty close to right. How I replace the loop with one is a bit of a mystery at this point, but right now, even with all the new stuff here I am still getting that same message on the same line.
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class GuicdInventory extends JFrame
{
Inventory guiInventory = new Inventory();
private Jlist cDJList;
//contructor adds Inventory to JFrame
public GuicdInventory()
{
super("CD Inventory");
setLayout(new FlowLayout()); // set frame layout
// adds buttons
addButton = new Button("Add");
add (addButton);
// create an object to listen to buttons
ButtonListener myButtonListener = new ButtonListener();
// tell buttons that myButtonListener should be notified
addButton.addActionListener(myButtonListener);
setVisible(true);
cDJList = new JList(Object[] guicdInventory);
cDJList.setVisibleRowCount(99);
add(new JScrollPane(cDJList));
} // end constructor
public static void main(String args[])
{
GuicdInventory guiceInentory = new GuicdInventory;
GuicdInventory.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GuicdInventory.setSize(500,800);
GuicdInventory.setVisible(true);
} // end
} // end main
no1zson 24 Posting Whiz in Training
OK, I wanted to learn JList weeks ago, but did not have what I considered a good opportunity. I also will need to learn buttons, so I am going with option 2 here.
I have scrapped my CdTextFrame class completely and tried to start a JList the way my book has it, but I am missing something.
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class GuicdInventory extends JFrame
{
Inventory guiInventory = new Inventory();
private Jlist cDJList;
//contructor adds Inventory to JFrame
public GuicdInventory()
{
super("CD Inventory");
setLayout(new FlowLayout()); // set frame layout
[B]cDJList = new JList(Object[] guicdInventory);[/B]
cDJList.setVisibleRowCount(99);
add(new JScrollPane(cDJList));
} // end constructor
public static void main(String args[])
{
GuicdInventory guiceInentory = new GuicdInventory;
GuicdInventory.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GuicdInventory.setSize(500,800);
GuicdInventory.setVisible(true);
} // end f
} // end main
I want to learn this a step at a time so that I do not get in over my head, but I am getting .class expected, and ; expected on the bolded line above, pointing at "guiInvnetory".
If I am understanding you, I will want to create the JList in this new class, and a button in my old Inventory class to replace my scanner, and that will enable me to simply list my array inventory?
no1zson 24 Posting Whiz in Training
What I am trying to wrap my brain around, is how the GUI is going to talk to the array, and vice versa.
I had it in my mind (before I started) that when I began defining the fields of the GUI; field2, field4 and so on, that I would somehow be able to map the data that I entered into these new fields back to that array.
I am not sure I can do it that way anylonger, but I already have an array defined, working, and ready to be used, certainly I can create a GUI that uses it as it is, cant I?
Am I making sense?
no1zson 24 Posting Whiz in Training
I have spent the weekend reading about GUI and how Java uses it, and all I got was confused. :o) I decided the best way to learn it was just to get in to it and start coding, so that is what I did. Went better than I thought it would.
I feel I have a pretty good grip on it (at least for 1 day of practice) but what I do not seem to be able to figure out is how to alter my current cd inventory application to start using the GUI instead of my old DOS prompts. I want to use the same logic, same rules, and all that, except now I want the information to be entered from, and displayed in a GUI interface.
Do I have to write a whole new class and discard the Inventory one I use currently, or is there a way to apply the tried and tested rules I currently have and just mesh in GUI methods and constructors to take the place of what is there?
Any help would be welcome.
Here is my current GUI that I have been working on. It is simple, I just wanted to get the first two fields up to play with them.
First:
import java.util.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class CdTextFrame extends JFrame
{
public JTextField cdField1; // JLabel for CdwArtist class
public JTextField cdField2;
public JTextField cdField3;
public JTextField cdField4;
//CdFrame contructor adds …
no1zson 24 Posting Whiz in Training
Having just gone through this process a month or two ago, I can offer a beginners point of view.
Netbeans did confuse me.
I found BlueJ unreliable. It would give me errors when there were none, and not give me errors when there were.
JGrasp is what I finally settled on. Simple, lite, and it does nothing more than compile and save.
It knows key words (int, public, string) and changes their color so that I know that part was right.
And when I was first starting, that was more than enough.
no1zson 24 Posting Whiz in Training
That is a neat trick, thanks for that tip.
Of course it showed me that my math was even more wrong than I though.
I had to change the formula to this:
return(price * nstock)+((price * nstock)* restock);
but with that I am going to say it is close enough, TheGather already pointed out the issue with the counter, so I have nothing more I want to do to this.
I am taking the weekend off to catch up on some reading. I was hoping to start some GUI stuff next week, and that was my whole reason for starting this endeavor. I hope it is as fun as I originally thought it was going to be.
Thanks so much for all your help this week guys. :icon_smile:
no1zson 24 Posting Whiz in Training
I am so stupid. The file I was running was not the file I had compiled. I was coding and compling in one file, running the other and wondering why nothing changed! :o)
Once I did correct that issue it does work, if I take out ".05" and just put in "5" or "05". So my method was right! First time! woo hoo, I am so happy.
Of course I need it to be ".05". When I put the decimal back in it erros and says possible loss of percision, found float, double required. If I change it to doube is says it found double, float required. Here is what it looks like with the syntax corrected.
public String artist; // artist performing on cd
public float restock; // restocking percentage
// Artist constructor
public CdwArtist()
{
artist = "";
restock = .05;
}
public CdwArtist(String in)
{
artist=in;
}
// get value
public void setArtist(String in)
{
artist=in;
}
public void setRestock(float cdRestock)
{
restock = cdRestock;
}
// return value
public String getArtist()
{
return (artist);
}
// returns indivudual inventory value for a disk
public float getValue()
{
return((price * nstock)* restock);
}
} //End Class
This is finally fun. I feel like am learning stuff and working through NEW problems for a change!
no1zson 24 Posting Whiz in Training
I want to learn more about how inheritance works, more like I NEED to learn more about it if I am to get any better at this.
In my code I have the value of a cd defined as how many cds are in stock times the price of the cd. I did this in my first class Compactdisk.
Now I have created a sub class that adds a new parameter in to my array, and that all makes sence to me now.
What if I want to alter the output of a method I have already done though? Value for instance. With my new subclass I want to make Value mean (price X stock X restock) where restock is going to be a 5% restocking fee.
I should not have to alter Compactdisk, as CdwArtist is a sub and inherits everything, so I added what you see below to CdwArtist. I think it is right, but how would I then get Inventory to grab this new Value instead of the old Value?
import java.util.*;
public class Inventory
{// begin class Inventory
public static int maxlength = 0;
public static CdwArtist[] sort(CdwArtist[] cds)
{
Arrays.sort(cds, 0, maxlength);
return cds;
}
public static String toString(CdwArtist[] cds)
{
String toSend = "\n\n";
for(int i = 0; i < maxlength; i ++)
toSend = toSend + cds[i].getName() + "\n";
return toSend;
}
public static void main(String[] args)
{//begin method main
// create cd Array
CdwArtist[] cds = new …
no1zson 24 Posting Whiz in Training
Just an FYI for my future understanding, can someone take the time and give me a quick lesson on the way CdwArtist is set up.
public CdwArtist(String in)
{
artist=in;
}
// get value
public void setArtist(String in)
{
artist=in;
}
It looks to my untrained eye as if both are doing the same thing.
no1zson 24 Posting Whiz in Training
I knew there was an issue with the counter, I had just never figured it out. My loop would always end on the last element of the array. I had played with the maxlenght stuff for awhile (I thought I even did what you suggessted here) but it did not change anything and I had bigger fish to fry so I moved on.
I knew it was something simple, but I am just so inexperienced in arrays that I would have never found that. I so much appreciate it.
I am going to close this issue out, and since it is so early I am going to open one more thing I have been trying on the side without success.
I would love to get that done and have a weekend just to read.
You guys are awesome.
no1zson 24 Posting Whiz in Training
Looks like I am talking to myself again! :o) At least I never get bored with the company. :o)
The more I look at it, I think the problem could be with the CdwArtist class. Look at this:
import java.util.*;
public class CdwArtist extends Compactdisk
{
// CdwArtist class adds one field
public String artist; // artist performing on cd
// Artist constructor
public CdwArtist()
{
artist = "";
}
public CdwArtist(String in)
{
artist=in;
}
// get value
public void setArtist(String in)
{
artist=in;
}
// return value
public String getArtist()
{
return (artist);
}
} //End Class
That does not look right to me. If I take out the CdwArtist string then the application fails when I enter the name of the cd. Confusing to me.
The set field controls artist (like it should right?) .... so I think I have some wires crossed somewhere. A set of non bloodshot eyes would sure be helpful. :o)
no1zson 24 Posting Whiz in Training
Ezzaral (another helpful poster) gave me this one.
It is more of a book than a tutorial, but I am looking for any and everything I can find.
http://www.smart2help.com/e-books/ti...ition/TIJ3.htm
no1zson 24 Posting Whiz in Training
Cerberos (a poster who has helped me on several occasions) showed me this one.
It is great.
http://sepwww.stanford.edu/sep/josman/oop/oop1.htm
I have another one I will give you when I find it.
no1zson 24 Posting Whiz in Training
Sorry about the poor attitude in that last post, I am trying not to get frustrated, I know this is a learning process, I just have to stick with it.
I have decided that the problem lies somewhere in the initialization of the array. It logically cannot be the sort, the inheritance properties of the sub class is there to serve that very purpose. Right?
This is the endless circle I have been going in for 3 hours now! Array .... find nothing .... must be Sort .... find nothing ... must be Array!! :o)
no1zson 24 Posting Whiz in Training
Hey guys. Be prepared to be shocked, but I could NOT figure out the problem with the exit sort. It is so aggravating to me. I hate feeling stupid, and I hate having to come out here day after day and feel like I am having Java spoon fed to me.
You all are great, I just do not want to feel like I am taking advantage of your skill.
This HAS to be something simple and dumb, I just do not see it. I am 99% sure it has to do with the sort, because once I enter "stop" and exit the main loop I get the error, and the only thing that happens at that point is this sort.
Everything compiles, everything runs fine, until exit, then I get all these errors in DOS.
Exception in thread "main" java.lang.NullPointException at java.util.Arrays.mergeSort(Arrays.java)
at java.util.Arrays.sort(Arrays.java)
at Inventory.sort(Inventory.java)
at Inventory.main(Inventory.java)
The more I think about it, the more I want to say it is with the array, because the sort should be inherited as is into the new array, but the array seems to be working ... so I do not know.
Here is the code in the new Inventory as it stands
import java.util.*;
public class Inventory
{// begin class Inventory
public static int maxlength = 0;
public static CdwArtist[] sort(CdwArtist[] cds)
{
Arrays.sort(cds, 0, maxlength);
return cds;
}
public static String toString(CdwArtist[] cds)
{
String toSend = "\n\n";
for(int …
no1zson 24 Posting Whiz in Training
I like the way you put those notes out to the side of the code lines, it helps me follow along.
I think I have this part, I am going to read those links again just to reinforce it. The changes have brought out an issue with the toString, program erros and never makes it to the list, so I am going to see if I can work that out. I have learned so much this week my head is spinning. After I get this block working the way I want, I think I am going to take a few days just to review what you guys have shown me, let it all sink in and make sure I understand it.
This language looks like it would be neat if I can just get rolling with it. There is an advanced java class I am looking at down the road, but I am nowhere near ready for that yet.
no1zson 24 Posting Whiz in Training
Ok. Let me play with this awhile. I only halfway understand that explaination, but with some of the stuff I have learned over the past few days, and the links from Cerberus I think I should be able to make sense of it.
Just FYI, why does it point to the "new" in that instance? Because new is trying to create a defalut that is not defined?
no1zson 24 Posting Whiz in Training
I love that link, thanks so much! I am going to read it again as soon as I am done here.
I am a little disappointed, I thought for sure I could get this Array moved over to the extended class, it sounded so simple last night. I have gone over and over my changed code, and for the last several adjustments I am getting the same error, cannot find symbol, and it is pointing to "new" in the bolded line below. I know the problem is not the word new, what does this error indicate?
I know there is a problem with the erray, but this error does not seem to point me in any direction.
import java.util.*;
public class Inventory
{// begin class Inventory
public static int maxlength = 0;
public static CdwArtist[] sort(CdwArtist[] cds)
{
Arrays.sort(cds, 0, maxlength);
return cds;
}
public static String toString(CdwArtist[] cds)
{
String toSend = "\n\n";
for(int i = 0; i < maxlength; i ++)
toSend = toSend + cds[i].getName() + "\n";
return toSend;
}
public static void main(String[] args)
{//begin method main
// create cd Array
CdwArtist[] cds = new CdwArtist[100];
float totalValue = 0;
Scanner input = new Scanner(System.in); // create scanner
// begin display method
System.out.print("Enter up to 99 CD Names or STOP to Exit: ");
String nameInput = input.nextLine(); //read cd name
maxlength ++;
for(int i = 1; i < cds.length && !nameInput.equalsIgnoreCase("STOP"); i++)
{// begin main While
[B]cds[i] = new CdwArtist();[/B]
cds[i].setName(nameInput);
System.out.print("Enter CD …
no1zson 24 Posting Whiz in Training
Cool. Cannot wait to see that work. I will give it a shot tomorrow and let you know how it goes. If I can get it to work I am going to try something else that will help reinforce some of this. Repetition is one of my keys to picking stuff up.
I appreciate your help.
no1zson 24 Posting Whiz in Training
I am at home, and my code is at work, but I have not touched the definition yet.
How can that simple name change, keep all the current Compactdisk parameters, and add the new cdwartist one?
Simple because cdwartist is a sub of the compactdisk class?
no1zson 24 Posting Whiz in Training
OK, that makes sence, because I set this one up differently than the others, so the set is included. Part of that easier quicker way I was referrring to in an earlier post.
That is the line that is going to pull that parameter in form the new class. It all starts to become clearer now.
That will not put it into my artist name into the array.
I want to use some derivitive of
cds.setArtist();
but do not know off the top of my head what to use. Seems like artist, or CdwArtist would go in the parenthsis, but I know that is not right. I am still a little shakey on the whole array process having just started this last week.
I am headed home now, I will get it.
By the way, I am reading Java, How to Program 6th Edition. (Deitel)
So far I am through chapter 10, and the most learning I have done is out here at the hands of you guys.
I wish I had know this site was here at the beginning, I would have a much better foundation than I do now.
no1zson 24 Posting Whiz in Training
I understand, and for consistency purposes I did change that variable to the same convention I have in the compactdisk class. It helps me relate one to the other.
Why is there no setArtist command needed in this class?
I am coding in the Inventory now to pull that parameter over, I have inserted
System.out.print("Enter CD Artist Name: "); // prompt for artist name
CdwArtist artist = [B]new[/B] CdwArtist();
cds[i].getArtist(input.nextLine()); // artist name input from user
Seems like I should have "setArtist" in there, not get Artist, but get is all I have defined in the CdwArtist class.
Just as a quick I tried to run it to see if I was lucky, but I get a cannot find symbol on the "new", and on the period before "get". Strange to me. Seems as if I missing a pointer somewhere that should tell Inventory where to look for this variable, but I am not sure what else I would add or where to put it at this point.
no1zson 24 Posting Whiz in Training
I see. So my sub class should look like this:
import java.util.*;
public class CdwArtist extends Compactdisk
{
public String artist; // artist performing on cd
// Artist constructor
public CdwArtist(String in)
{
artist=in;
}
// return value
public String getArtist()
{
return (artist);
}
} //End Class
Which, minus the set method is pretty close to my original class. I was at least on the right track for a change! :o)
I have another meeting to go to now, I do not fully understand the use of (string in) and artist=in just yet, and I will still have to code my Inventory class, but a little reading this evening should set me up for that.
Thanks again guys, you always help get me back in the right direction.
no1zson 24 Posting Whiz in Training
Yeah. I posted that before I read his. I think I have a little better grasp on it now. At least enough to start coding some more and see what I can come up with.
I have been meaning to ask this question for awhile, and now is the perfect opportunity.
I see you guys set up your objects different than I do, it looks shorter and quicker, but when I try to emulate that into my own coding I get the same error every time. I just tried it with the stuff TheGather put down:
import java.util.*;
public class CdwArtist extends Compactdisk
{
public String artist; // artist performing on cd
// Artist constructor
[B] Artist performer;[/B]
public CdwArtist(Artist in)
{
performer=in;
}
// return value
public Artist getArtist()
{
return performer;
}
} //End Class
but I get "cannot find symbol" on Artist on the bolded line. Seems like it should work, I see it in all the books, why do I get this error?
And exactly what does that line do?
no1zson 24 Posting Whiz in Training
Wait, no, I take that back.
If Compactdisk is automobile, CDwithArtist is a car, then CDwithArtist "isa" automobile and should inherit everything therein if I just extend it. So I should NOT have to put everything in there manually ... right?
This logic leads me to believe then that I am somewhat close with my original cdartist code, but need to make a few changes in Inventory.
Am I talking in circles now?
no1zson 24 Posting Whiz in Training
I see. So then I need to add everything that is in Compactdisk in to CdArtist along with my new Artist object, and then change my
Compactdisk cd = new Compactdisk(); to something like
CdArtist cd = new Compactdisk();
in my Inventory application and go from there.
I am not totally sure how to add all that into the new class, so I will get my face in a book here shortly. Thanks for the direction. Let me know if I am off base with this.
Thanks again.
no1zson 24 Posting Whiz in Training
So then what is my vehicle class?
If Compactdisk is car, and CdArtis is truck, then neither is really a subclass of the other? I was thinking that extending Compactdisk with CdArtist takes everthing as it is, and just adds one more detail.
Am I not doing what I think I am doing?
I want to make my CdArtist a sub class of Compactdisk, and just add that extra object into my array.
no1zson 24 Posting Whiz in Training
TheGathering,
Thanks for that. I do think I have the concept of subclasses down, and to use your analogy, Compactdisk would be my vehicle, and Cdartist would be a car or truck.
When I run my application (Inventory) it pulls information from Compactdisk, and therefore Cdartist as an extented class, right? I should not have to alter Compactdisk to add objects in CdArtist, if I understand correctly.
So I am trying to figure out what I need to do in the Inventory class to get cds to recognize this new object.
no1zson 24 Posting Whiz in Training
I must be getting better at this, because I actually understood that. :o)
Two questions.
1. Concerning the setCdArtist method, is that not being created in the extended class? I thought the whole purpose of that class was to create that method for the set and get calls in Inventory.
2. I too thought I should assign it somewhere in cds. I thought my bolded code above would do that.
It is my understanding that I do not want to alter Compactdisk, or else it takes away from the purpose of extending a sub class. So if I do not add it in to the Inventory class as I thought I was doing, where would that insert happen?
no1zson 24 Posting Whiz in Training
I appreciate that. It helps me so much to have that kind of explaination.
I can see the code in my application, run through it line by line, and see what triggers what now.
A lot of the time I am just using code because it is what I have learned, but do not know the specifics of what it does. It will help me in the long run to get this kind of detailed instruction, and my instructor in class just does not have this kind of individual time to set aside for each student.
Again, thanks.
no1zson 24 Posting Whiz in Training
I thought I understood how this worked, but maybe I do not.
I intentionally left a field out of my compactdisk class (artist) so that I could create a sub class later, impliment it, and see how that whole thing worked.
Sounded simple, I still think it is, I just think I am doing something wrong.
Here is my original class:
import java.lang.Comparable;
public class Compactdisk implements Comparable
{// begin class
//InventoryCD class has 5 fields
private String name; // Name of cd
private float price; // price of cd
private int itemno; // item number of cd
private int nstock; // how many units in stock
private int i; // cd counter for array
private float value; // value for single cd inventory
//Compact disk class constructor
public Compactdisk()
// 4 fields need to be set up
{
name = "";
price = 0;
itemno = 0;
nstock = 0;
i = 0;
value = 0;
}
// set values
public void setName(String diskName)
{
name = diskName;
}
public void setPrice(float cdPrice)
{
price = cdPrice;
}
public void setItemno(int cdItemno)
{
itemno = cdItemno;
}
public void setNstock(int cdStock)
{
nstock = cdStock;
}
public void setValue(float cdValue)
{
value = cdValue;
}
public void seti(int Count)
{
i = Count;
}
// return values
public String getName()
{
return (name);
}
public float getPrice()
{
return (price);
}
public int getItemno()
{
return (itemno);
}
public int getNstock()
{
return (nstock);
} …
no1zson 24 Posting Whiz in Training
Amazing. Do you guys just know this stuff, and sit down and logically work your way through it? or do you have manuals and texts from years of experience that you refer to in order to get the algorithms and apis and methods and things like that?
no1zson 24 Posting Whiz in Training
If you stop back in to read that, that second post is the one you want to go with as most the work is done for you.
Cheers,
-TheGathering
I kept that sort in there, it was simple and I understand it (for the most part.)
I have completed work on this issue, but wanted to pick your brain on a few blocks of code that I do not think I fully understand.
Can you give me a play by play (in laymans terms) exactly what a few of these blocks are doing?
For instance:
public int compareTo(Object in)
{
return ((Comparable)name.toLowerCase()).compareTo((Comparable)((Compactdisk)in).getName().toLowerCase());
}
I do not understand exactly what this is doing, other than comparing the names I have entered and returning them in order. I do not understand the mechanics behind it. And
public static String toString(Compactdisk[] cds)
{
String toSend = "\n\n";
for(int i = 0; i < maxlength; i ++)
toSend = toSend + cds[i].getName() + "\n";
return toSend;
}
I think this keeps track of the number of elements entered so that the loop knows when to end, but I am not sure exactly how it does it. Maxlenth is much like count, but I do not get the "toSend" part.
I know you are not my teacher or anything, but this code intrigues me and I would like to learn all I can. I have never seen these used before, and would not be able to …
no1zson 24 Posting Whiz in Training
That was way more than I expected to see. It did answer all of my questions though. I am going to try some different things with it and let you know how it comes out.
I was unsure of the maxlenght, and how it worked, and could not find a good coding example for arrays.sort for me to use. Or so I thought. The INT was throwing me off as the object I am trying to sort on is a string. But I think I have it now. The tosend is still way beyond me, I am doing some reading on that this afternoon.
thanks again guys. you make all the frustration bearable in the end.
no1zson 24 Posting Whiz in Training
I have never programmed in Java before. I have not programmed in anything since the late 80s when I used COBOL for awhile.
I appreciate your help. I will try and take what you have shown me here and apply it to my program.
Once I get everything completed I will post the final product, or more questions ... care to guess which one will come first? :o)
no1zson 24 Posting Whiz in Training
I seem to be going in a big circle with this.
Latest version ends up like this:
public static void sort(char[] cds)
{
cds[0] = name;
cds[1] = name;
cds[2] = name;
cds[3] = name;
cds[4] = name;
}
{
return (Cds.sort(Name, String.CASE_INSENSITIVE_ORDER)); trace("Array.sort():\n" + Cds);
}
} // end class Payroll
and I am getting "cannot find symbol" errors for every instance of "name" I have. I would be grateful for any help. This is my first sort and I am not even sure I am using it right, much less the coding syntax issues.
no1zson 24 Posting Whiz in Training
Well, I have kicked around several things, and after calling util.Arrays into the main class I have tried to impliment this:
}// end method main
public static void sort(char[] cds)
{
var cds = new Cds[5];
cds[0] = Name
cds[1] = Name
cds[2] = Name
cds[3] = Name
cds[4] = Name
}
{
return (Cds.sort(Name, String.CASE_INSENSITIVE_ORDER));
trace("Array.sort():\n" + Cds);
}
} // end class Payroll
I know it is not right, but for my first time I am just trying to get in the ballpark. Can someone tell me if I am even on the right track, or do I need to disregard this implimentation and try something else?
I just want to spit out a quick list of the cds in alphabetical order, by the name parameter.
no1zson 24 Posting Whiz in Training
Of course I have to make things difficult. If I just put 5 cd names in to an array it would not be that hard to set up a sort for them, and get them in alphabetical order, but I have to have the user enter the names in, so we do not know the names to be used in the sort until after the loop is entered.
Either I do not understand the sorting inside Java, or am just too inexperienced to code it correctly.
Here are my two classes:
import java.util.Scanner; //uses class Scanner
public class Inventory
{// begin class Inventory
public static void main(String[] args)
{//begin method main
// create cd Array
Compactdisk[] cds = new Compactdisk[5];
[B]Arrays.sort(cds); [/B]
cds[0] = new Compactdisk(); // adds to array
cds[0].getName();
cds[0].getPrice();
cds[0].getItemno();
cds[0].getNstock();
int cdCount = 0;
float totalValue = 0;
Scanner input = new Scanner(System.in); // create scanner
// begin display method
System.out.print("Enter up to 5 CD Names or STOP to Exit: ");
String nameInput = input.next(); //read cd name
while (!nameInput.equalsIgnoreCase("STOP"))
{// begin main While
cds[cdCount] = new Compactdisk();
cds[cdCount].setName(nameInput);
System.out.print("Enter Price of this CD: "); // prompt for price
cds[cdCount].setPrice(input.nextFloat()); // price input from user.
while (cds[cdCount].getPrice()<= 0)
{// begin while
System.out.print("Price Must Be Greater Than Zero. Enter Price: ");
cds[cdCount].setPrice(input.nextFloat()); // cd price loop from user.
} // End while
System.out.print("Enter CD Item Number: "); // prompt for cd item number
cds[cdCount].setItemno(input.nextInt()); // cds item number input from user
System.out.print("Enter Number of …
no1zson 24 Posting Whiz in Training
Problem solved. Simple missing '$' in the print command.
Should have been.
System.out.printf("Entire Inventory for all CDs is Worth %c%.2f.\n", '$', totalValue)
How ya like that? Thanks everyone for their help.
no1zson 24 Posting Whiz in Training
Thanks for your continued help. I know there are lots better things you could be using your Sunday for. Here is my code as it stands with your "try/catch" solution implimented.
import java.util.Scanner; //uses class Scanner
public class Inventory
{// begin class Inventory
public static void main(String[] args)
{//begin method main
// create cd Array
Compactdisk[] cds = new Compactdisk[5];
cds[0] = new Compactdisk(); // adds to array
cds[0].getName();
cds[0].getPrice();
cds[0].getItemno();
cds[0].getNstock();
int cdCount = 0;
float totalValue = 0;
Scanner input = new Scanner(System.in); // create scanner
// begin display method
System.out.print("Enter up to 5 CD Names or STOP to Exit: ");
String nameInput = input.next(); //read cd name
while (!nameInput.equalsIgnoreCase("STOP"))
{// begin main While
cds[cdCount] = new Compactdisk();
cds[cdCount].setName(nameInput);
System.out.print("Enter Price of this CD: "); // prompt for price
cds[cdCount].setPrice(input.nextFloat()); // price input from user.
while (cds[cdCount].getPrice()<= 0)
{// begin while
System.out.print("Price Must Be Greater Than Zero. Enter Price: ");
cds[cdCount].setPrice(input.nextFloat()); // cd price loop from user.
} // End while
System.out.print("Enter CD Item Number: "); // prompt for cd item number
cds[cdCount].setItemno(input.nextInt()); // cds item number input from user
System.out.print("Enter Number of these CDs in Stock: "); // prompt for cd stock
cds[cdCount].setNstock(input.nextInt()); // cds in stock input from user
System.out.print("CD "+cds[cdCount].getName()+", Item Number "+cds[cdCount].getItemno()+","); // display name
System.out.printf(" is worth %c%.2f.\n", '$', cds[cdCount].getPrice()); // display individual price
System.out.printf("We have %d copies in stock, making our inventory for this cd worth %c%.2f\n", cds[cdCount].getNstock(), '$', cds[cdCount].getValue()); //inventory value
try
{
totalValue = totalValue + cds[cdCount].getValue();
}
catch(NullPointerException …
no1zson 24 Posting Whiz in Training
I have never seen that code before, what is it?
I tried it just to see if I could see what it does, but nothing changes.
Everything compliles, error on execution, just when I leave the loop.
Looks like it starts to execute, but just does not like my formating of the %c%.2f./n, or the format of the variable I am trying to put there.
Exact message reads: Entire Inventory for all CDs is Worth Exceptio in thread "main" java.util.IllegalFormatConversionException: c != java.lang.Float
at java.util.Formatter$FormatSpecifier.failConversion (Formater.java:3978)
at java.util.Formatter$FormatSpecifier.printCharacter
(Formatter.java:2006)
This is way over my head.
no1zson 24 Posting Whiz in Training
Thought I figured it out, but I just made it worse. I moved the count line to run after the totalValue line. Logic being, the count was moving forward before I was ready for it to.
Thought I had it because now I can run the loop again. When I enter STOP to leave the loop, and hopefully see my total value I get a whole string of errors. Again, it complies, but errors on execution.
Errors are:
Illeagal format conversion, Format specifier and that group.
No real idea what that means. I think it might have something to do with the count being integer and the total being float? Maybe it does not like the way I am trying to print it?
no1zson 24 Posting Whiz in Training
Sorry iamthwee, here is my other class, as it now stands. Maybe my question will make more sense now. I am not very good yet, so maybe it will make less sense. ;)
import java.util.Scanner; //uses class Scanner
public class Inventory
{// begin class Inventory
public static void main(String[] args)
{//begin method main
// create cd Array
Compactdisk[] cds = new Compactdisk[5];
cds[0] = new Compactdisk(); // adds to array
cds[0].getName();
cds[0].getPrice();
cds[0].getItemno();
cds[0].getNstock();
int cdCount = 0;
float totalValue = 0;
Scanner input = new Scanner(System.in); // create scanner
// begin display method
System.out.print("Enter up to 5 CD Names or STOP to Exit: ");
String nameInput = input.next(); //read cd name
while (!nameInput.equalsIgnoreCase("STOP"))
{// begin main While
cds[cdCount] = new Compactdisk();
cds[cdCount].setName(nameInput);
System.out.print("Enter Price of this CD: "); // prompt for price
cds[cdCount].setPrice(input.nextFloat()); // price input from user.
while (cds[cdCount].getPrice()<= 0)
{// begin while
System.out.print("Price Must Be Greater Than Zero. Enter Price: ");
cds[cdCount].setPrice(input.nextFloat()); // cd price loop from user.
} // End while
System.out.print("Enter CD Item Number: "); // prompt for cd item number
cds[cdCount].setItemno(input.nextInt()); // cds item number input from user
System.out.print("Enter Number of these CDs in Stock: "); // prompt for cd stock
cds[cdCount].setNstock(input.nextInt()); // cds in stock input from user
System.out.print("CD "+cds[cdCount].getName()+", Item Number "+cds[cdCount].getItemno()+","); // display name
System.out.printf(" is worth %c%.2f.\n", '$', cds[cdCount].getPrice()); // display individual price
System.out.printf("We have %d copies in stock, making our inventory for this cd worth %c%.2f\n", cds[cdCount].getNstock(), '$', cds[cdCount].getValue()); //inventory value
cdCount++;
totalValue = totalValue + cds[cdCount].getValue();
System.out.print("Enter …
no1zson 24 Posting Whiz in Training
Massive thunderstorm here right now, I am trying to get off before I lose power. I have an array that is accepting inventory input from the user. The value of each product is figured as it is entered, simple instock number muliplied by the product value.
I simply want to sum that value at the end of the array for a total inventory value.
Here is my code:
public class Compactdisk
{// begin class
//InventoryCD class has 5 fields
String Name; // Name of cd
float price; // price of cd
int itemno; // item number of cd
int nstock; // how many units in stock
int cdCount; // cd counter for array
float value; // value for single cd inventory
float totalValue; // value for entire inventory
//Compact disk class constructor
public Compactdisk()
// 4 fields need to be set up
{
Name = "";
price = 0;
itemno = 0;
nstock = 0;
cdCount = 0;
value = 0;
totalValue = 0;
}
// set values
public void setName(String diskName)
{
Name = diskName;
}
public void setPrice(float cdPrice)
{
price = cdPrice;
}
public void setItemno(int cdItemno)
{
itemno = cdItemno;
}
public void setNstock(int cdStock)
{
nstock = cdStock;
}
public void setValue(float cdValue)
{
value = cdValue;
}
public void setcdCount(int Count)
{
cdCount = Count;
}
public void setTotalValue(float invValue)
{
totalValue = invValue;
}
// return values
public String getName()
{
return (Name);
}
public float getPrice()
{
return (price); …
no1zson 24 Posting Whiz in Training
I agree. I was going to make that one of my housecleaning chores today. I wanted to get the value summed up, print a list of the entire inventory, and then raise the array limit to 100 or so and put the check in that you just recommended.
Right now I am stuck on the Value sum, and since this thread was concerning the array I am going to close it out.
If the summing issue contiues to kick my butt I will open a different thread on it later.
Thanks for all your help guys.
no1zson 24 Posting Whiz in Training
Well, I was going to call it a day and just relax for the rest of the weekend, but Peter got me all fired up, so before I shut her down I decided to throw one more twist. Wouldn't it be clever to keep a running total of the values? I thought. I currently display the value of each cd depending on how many are in stock and its price.
How hard could it be to just sum that field. Apparently harder than I though. Here is what I came up with: Value being the name of the parameter already used, and cdCount being my counter.
public static float calculateTotal(Inventory completeValue[]);
float totalValue = 0;
for ( int cdCount = 0; cdCount < completeValue.length; cdCount++ )
{
totalValue += completeValue[cdCount].getValue();
} // end for
return totalValue;
I just inserted that at the bottom of my Compactdisk class, right after the brace that closes return(price * nstock);, but before the one that ends the class.
I get
Compactdisk.java:79: illegal start of type
for ( int cdCount = 0; cdCount < completeValue.length; cdCount++ )
^
Compactdisk.java:83: <identifier> expected
return totalValue;
I thought this would be simple, can anyone see what I am missing? It almost looks to me as if it does not like the method there at all.