[hotjoe.com] HotJoe Java Help Forums
  [Search] Search   [Recent Topics] Recent Topics   [Hottest Topics] Hottest Topics   [Members]  Member Listing   [Groups] Back to home page 
[Register] Register / 
[Login] Login 
Visit java.com
Please send email if you are having login problems - see the posts below for more info.
Hotmail and Yahoo! users - please see the Hotmail post or the Yahoo! post for information on lost emails.
Messages posted by: leapfrog7
Forum Index » Profile for leapfrog7 » Messages posted by leapfrog7
Author Message
Okay, so I am working on this program that I have been working on for a while, and I only need one more issue corrected before it is complete. My program displays multiple elements from an array in a GUI. Each element has several values such as name, number, and so on. I added a "Search" button and an empty editable text field so the user can use it to search.

For example, I want to make it so I can type the name, click search and then have the GUI display the element according to the name searched for.

Here is what I have for the text field:



Here is what I have for the button so far:



Finally, the array element that I want to search is the name, and I display it with:


I am completely lost and this and I would really appreciate any help or suggestions offered. I could really use a good point towards the right direction though.
Thank you so much, that helped a lot. All I came up with on my own was



Which worked for the elements that were already written in the code, but when I added new elements it would not re-total the elements, but your example worked perfectly. Thank you again!
Okay, so I am working on this program, and have been for a while now. I have the line of code:



This code works and does what it is intended to. However, I added a button to add more elements to the array, but I still need this total.

My question is how do I total elements in an array without knowing how many elements I will have? I ask this because the button allows the user to add 0 - ? additional elements. So it will be "unknown".

Can anybody help?
Nevermind, I figure out what the problem was.
I get the error below when I run my code:

Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:840)
at java.util.Scanner.next(Scanner.java:1461)
at java.util.Scanner.nextInt(Scanner.java:2091)
at java.util.Scanner.nextInt(Scanner.java:2050)
at InventoryProgram.main(InventoryProgram.java:33)

I can see where the problem is, but I cannot figure it out. Any and all help is greatly appreciated. Here is the section of code where the problem seems to be. I have more code for the program, but I would rather not put it in public view. However, if you feel you need to see it let me know. The following code is also the main class.





myInput.txt file:

Green AP
Bercana Tech
1
5
4.99
Red AP
Rodante Corp
2
10
6.83
Blue AP
Bercana Tech
3
7
0.89
Orange AP
Rodante Corp
4
3
7.27


Again, any help is greatly appreciated and I welcome all suggestions.
i was actually just coming to update this post. I tried something similar to what you typed earlier. I think I tried:



But that did not work and would not compile. But, I did finally get it to work by using:



This example worked perfectly. Thanks for your help though, I really love this forum.
I have a quick question, I am just learning Java and in my current program I need to create a GUI. My code compiles and works just fine. But the line:



Displays in the format: 2.4000000234

How to I change this to display as currency so it shows: $2.40

Value is a double, and when using the "System.out.printf" it displays just fine. Any help is greatly appreciated.
You are right, it worked great. My problem was when I transferred your example I missed the first line:



I thought the rest of the code implemented the comparable and didn't realize it was coded in the first line. Thank you so much for your help with this, I really appreciate it.
I did use the code above, and there were no problems compiling it, but I still get that error.
I really want to thank you for all your help, but I am still getting the same error when I try to run it:


Exception in thread "main" java.lang.ClassCastException: ActionProduct cannot be cast to java.lang.Comparable
at java.util.Arrays.mergeSort(Unknown Source)
at java.util.Arrays.sort(Unknown Source)
at InventoryProgram.main(InventoryProgram.java:10)


I do not understand what these errors mean or where to start looking for an answer. Do you have any ideas? Again, thank you so much for you help, it is very appreciated.
Okay, thank you for your replies by the way, I was completely lost. I do get this error when I try to run the code in Eclipse:


Exception in thread "main" java.lang.ClassCastException: ActionProduct cannot be cast to java.lang.Comparable
at java.util.Arrays.mergeSort(Unknown Source)
at java.util.Arrays.sort(Unknown Source)
at InventoryProgram.main(InventoryProgram.java:10)


I am a little more confused now though because it seems I need something called a comparable for the ActionProduct, but my course has not covered this as of yet. Do I need to set up a comparable? If I do does it need to be setup in the ActionProduct class?
This will compile:




ActionProduct Class:


Product Class:



But I cannot setup the sorting method correctly and I do not know where I am going wrong. I continued to work on it, and this is what I have in stead of the first code example, but it does not compile.

I am working on my homework assignment. I have most of it done but I am stuck. The directions are:

Modify the Inventory Program Part 1 so the application can handle multiple items.
Use an array to store the items. The output should display the information one
product at a time, including the item number, the name of the product, the number of
units in stock, the price of each unit, and the value of the inventory of that product.
In addition, the output should display the value of the entire inventory.
Create a method to calculate the value of the entire inventory
Create another method to sort the array items by name

I cannot get the sorting part of it right, can you look at my code and give me some tips? I would beg but it doesn't transfer well into writing.

class Week5Ex {
public static void main (String arg[]){

ActionProduct ap1 = new ActionProduct("Green AP", 1, 5, 4.99);
ActionProduct ap2 = new ActionProduct("Red AP", 2, 10, 6.83);

Product p[] = {ap1, ap2};

public static void sortProductArrayByNameAscending(Product p[]){
boolean swap = true;
while(swap == true){
swap = false;
for(int number=0; number < (p.length - 1); ++number){
if(p[number].name.compareToIgnoreCase(p[number+1].name) > 0){
Product temp = new Product(p[number].name, p[number].number, p[number].amount, p[number].price);
p[number] = p[number+1];
p[number+1] = temp;
swap = true;
}
}
}
}

for(int number = 0; number < p.length; ++number){
System.out.printf("Item Name: %s, Item #: %d, Units in Stock: %d, Item Price: $%.2f\n",
p[number].name, p[number].number, p[number].amount, p[number].price);
System.out.printf("Inventory Value: $%.2f\n",
p[number].InventoryValue());
System.out.println();
}

System.out.println();{
System.out.printf("Total Inventory Value: $%.2f\n", ap1.InventoryValue() + ap2.InventoryValue());
}
}

private static void Product(Product p[]) {

}
}


ActionProduct Class:


class ActionProduct extends Product {
public ActionProduct(String a, int b, int c, double d){
name = new String(a);
number = b;
amount = c;
price = d;

}

public double InventoryValue(){
return (amount*price);
}
}


Product Class:

abstract class Product {
String name;
int number;
int amount;
double price;

public abstract double InventoryValue();
}
 
Forum Index » Profile for leapfrog7 » Messages posted by leapfrog7
Go to:   
Powered by JForum 2.1.9 © JForum Team
This site run by Scott Dunbar of Xigole Systems. © 2005-2011 - Scott Dunbar
Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective owners
hotjoe.com, xigole.com, and Scott Dunbar have no affiliation with Oracle