Results 1 to 10 of 10
Thread: Help with error new to Java
- 11-26-2012, 05:51 AM #1
Member
- Join Date
- Nov 2012
- Posts
- 6
- Rep Power
- 0
Help with error new to Java
I have two classes made one called Item the other called CoffeeDriver, my Item class compiles fine no errors.
I am having my issues with my second class called CoffeeDriver.
The program is to provide a way to print out a listing of items:
I have to have class named Item:Java Code:Item Name Price Coffee $1.00 Water $2.00 Milk $1.50 Bagel $1.25 Donut $0.75
String instance variable to hold item name
double instance variable to hold item price
constructor that takes String and double to initialize instance variables
get and set methods for each variables.
I have to have class named CoffeeDriver:
sortName -array of items by item name displays name and price of all on screen
sortPrice - array of items by item price displays name and price of all items on screen
main - array of Item objects using data above set each item's information, array, prompt user
for what they want to see sorted by name or price. Call appropriate method.
I can basically do the NumberFormatting, fixing the printing, and all basic but my biggest issue is within the class CoffeeDriver
for some reason my sortPrice is not working right and I keep getting errors no matter what I change and its down to one error now
and I've about pulled my hair out.
the error I am receiving is:
CoffeeDriver.java:65: error: '.class' expected
Here is my code for both classes:
//header
Java Code:import java.util.*; import java.text.*; public class Item { private String itName; private double itPrice; //NumberFormat fmtnum=NumberFormat.getCurrencyInstance(); public Item(String itName, double itPrice) { this.itPrice=itPrice; this.itName=itName; } public Item() { } public String getName() { return itName; } public double getPrice() { return itPrice; } public void setName(String someName) { itName=someName; } public void setPrice(double somePrice) { itPrice=somePrice; } public String toString() { //return "Item: " + fmtnum.format(itName) + "Price: $" + fmtnum.format(itPrice); return "Item: " + itName + " Price: $" + itPrice; } static final Comparator<Item>NAME_ORDER = new Comparator<Item>() { public int compare(Item A, Item B) { return A.getName().compareTo(B.getName()); } }; static final Comparator<Item>PRICE_ORDER = new Comparator<Item>() { public int compare(Item A, Item B) { return Double.compare(A.getPrice(), B.getPrice()); } }; }
//header
Java Code:import javax.swing.*; import java.util.*; import java.text.NumberFormat;//NumberFormat public class CoffeeDriver { //main public static void main(String []args) { String customerSort; int x; Item arr[]=new Item[5]; arr[0]=new Item("Coffee",1.00); arr[1]=new Item("Water", 2.00); arr[2]=new Item("Milk", 1.50); arr[3]=new Item("Bagel",1.25); arr[4]=new Item("Donut",0.75); NumberFormat fmtnum = NumberFormat.getCurrencyInstance(); int finalSort; System.out.println("Welcome to Wings Coffee Shop. We have a great list of items on the menu."); customerSort=JOptionPane.showInputDialog(null, "Would you like to see these items sorted by name or price. To sort by name press (1). To sort by price press (2)."); finalSort = Integer.parseInt(customerSort); //do while loop if (finalSort == 2); sortItemPrice(arr); if (finalSort == 1); sortItemName(arr); } //sortName public static void sortItemName(Item arr[]) { int x; Item value; for (x=0;x<arr.length;x++) { if (arr[x].getName().compareTo(arr[x+1].getName())>0) { value = arr[x]; arr[x] =arr[x+1]; arr[x+1] =value; } JOptionPane.showMessageDialog(null,arr[x]); //System.out.println(" " + arr[x]); } } //sortPrice public static void sortItemPrice(Item arr[]) { int x=arr.length; int value=0; //Item value; for (x=0;x<arr.length;x++) { for(int j=1;j<(arr.length-x);j++) { if(int arr[j-1]>int arr[j]) { value=int arr[j-1]; int arr[j-1] = int arr[j]; int arr[j]=value; } } //if(arr[x].getPrice()>arr[x+1].getPrice()) //{ //value=arr[x]; arr[x]=arr[x+1]; arr[x+1]=value; //} System.out.println("" + arr[x]); //JOptionPane.showMessageDialog(null,arr[x]); } } //System.exit(0); }Last edited by Fubarable; 11-26-2012 at 05:56 AM. Reason: code tags added
-
Re: Help with error new to Java
I've added [code] [/code] tags around your code to help it retain its formatting, but even with the tags your code is hard to read and hard to debug because the formatting including the indentations are all over the place.
I suggest that you carefully edit your code, that you be extra careful in indenting your blocks exactly 3 spaces, and maintaining strict uniformity. Do this and many bugs become self-evident. Also, let us know which line is the offending line in your code above.
- 11-26-2012, 04:32 PM #3
Member
- Join Date
- Nov 2012
- Posts
- 6
- Rep Power
- 0
Re: Help with error new to Java
I apologize about the code tags I knew I had to use but was confused since I am new and my formatting now I see is horrifying. I will also go back and edit carefully.
This is the errors I am recieving:
----jGRASP exec: javac -g CoffeeDriver.java
CoffeeDriver.java:65: error: '.class' expected
if(int arr[j-1]>int arr[j])
^
CoffeeDriver.java:65: error: illegal start of expression
if(int arr[j-1]>int arr[j])
^
CoffeeDriver.java:65: error: ';' expected
if(int arr[j-1]>int arr[j])
^
CoffeeDriver.java:65: error: not a statement
if(int arr[j-1]>int arr[j])
^
CoffeeDriver.java:65: error: ';' expected
if(int arr[j-1]>int arr[j])
^
CoffeeDriver.java:65: error: ']' expected
if(int arr[j-1]>int arr[j])
^
CoffeeDriver.java:65: error: illegal start of expression
if(int arr[j-1]>int arr[j])
^
CoffeeDriver.java:65: error: ';' expected
if(int arr[j-1]>int arr[j])
^
CoffeeDriver.java:67: error: '.class' expected
value=int arr[j-1];
^
CoffeeDriver.java:67: error: illegal start of expression
value=int arr[j-1];
^
CoffeeDriver.java:67: error: ';' expected
value=int arr[j-1];
^
CoffeeDriver.java:68: error: ']' expected
int arr[j-1] = int arr[j];
^
CoffeeDriver.java:68: error: not a statement
int arr[j-1] = int arr[j];
^
CoffeeDriver.java:68: error: ';' expected
int arr[j-1] = int arr[j];
^
CoffeeDriver.java:68: error: ']' expected
int arr[j-1] = int arr[j];
^
CoffeeDriver.java:68: error: illegal start of expression
int arr[j-1] = int arr[j];
^
CoffeeDriver.java:69: error: ']' expected
int arr[j]=value;
^
CoffeeDriver.java:69: error: illegal start of expression
int arr[j]=value;
^
18 errors
----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.
It seems that my error starts at the "if" statement (line 63) and falls through to (line 67) of my CoffeeDriver of my //sortPrice. Somewhere I have my sort wrong for this section as how I had it before when x is 4 when I try to compare element to 5 of the array-which doesn't exist is where my sort was crashing. So I tried to fix to keep from moving past the end of array. And the above is what I ended up getting.
- 11-26-2012, 04:39 PM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,375
- Blog Entries
- 7
- Rep Power
- 17
- 11-26-2012, 04:57 PM #5
Member
- Join Date
- Nov 2012
- Posts
- 6
- Rep Power
- 0
Re: Help with error new to Java
When I first originally wrote my program was crashing, another person looked at my code and told me in order to fix the sort it should look more like this:
int n = intArray.length;
int temp = 0;
for(int i=0; i<n; i++)
{
for(int j=1; j<(n-i); j++)
{
if(intArray[j-1]>intArray[j])
{
//swap elements
temp = intArray[j-1];
intArray[j-1]=intArray[j];
intArray[j]=temp;
}
}
I filled in what I thought with scripts from my own coding, now I'm thinking I misunderstood the whole intent of the code?
I'm starting to feel that I should of left as is because without the above code added I only had the following error and the job compiled but when I ran it I got the
following error:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsExeption: 5
at CoffeeDriver.sortItemPrice(CoffeeDriver.java:61)
at CoffeeDriver.main(CoffeeDriver.java:33)
-jGrasp wedge2: eit code for process is 1.
-jGrasp: operation complete.
- 11-26-2012, 05:23 PM #6
Member
- Join Date
- Nov 2012
- Posts
- 6
- Rep Power
- 0
Re: Help with error new to Java
Now I look into it more I did completely misunderstand as the person who gave me the info was trying to tell me to do a bubble sort for my //sortPrice as is what I thought originally did, but gave me an example of a bubble sort by sorting an array of intergers. duh me!
I need to go over my //sortPrice again as a bubble sort, though I am still pulling my hair out.
- 11-26-2012, 05:42 PM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,375
- Blog Entries
- 7
- Rep Power
- 17
Re: Help with error new to Java
I'm afraid you misunderstood what that person was trying to tell you; better undo all those changes and come back here again. b.t.w. bubble sort is a complete nono; there's a much better sort algorithm available in the core classes.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 11-26-2012, 06:12 PM #8
Member
- Join Date
- Nov 2012
- Posts
- 6
- Rep Power
- 0
Re: Help with error new to Java
I completely did misunderstand, I've gone through and took out the changes I know bubble sort is a complete nono but for some reason our class assignment has to use it as I had everything working with Array.sort, but that got turned away, its when I found out we are to use the bubble sort.
After taking everything out and compiling my Class CoffeeDriver compiles and runs but I still get error messages and nothing sorts and "Water" is missing not sure how that happened. I'm not sure what the Item$1 means in the error as my other class is named Item.
here are the errors I'm getting now:
----jGRASP exec: java CoffeeDriver
Exception in thread "main" java.lang.NoClassDefFoundError: Item$1
at Item.<clinit>(Item.java:41)
at CoffeeDriver.main(CoffeeDriver.java:19)
Caused by: java.lang.ClassNotFoundException: Item$1
at java.net.URLClassLoader$1.run(URLClassLoader.java: 366)
at java.net.URLClassLoader$1.run(URLClassLoader.java: 355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.j ava:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:4 23)
at sun.misc.Launcher$AppClassLoader.loadClass(Launche r.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:3 56)
... 2 more
----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.
----jGRASP exec: java CoffeeDriver
Welcome to Wings Coffee Shop. We have a great list of items on the menu.
Item: Coffee Price: $1.0
Item: Milk Price: $1.5
Item: Bagel Price: $1.25
Item: Donut Price: $0.75
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
at CoffeeDriver.sortItemPrice(CoffeeDriver.java:62)
at CoffeeDriver.main(CoffeeDriver.java:33)
----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.
Java Code://header import java.util.*; import java.text.*; public class Item { private String itName; private double itPrice; //NumberFormat fmtnum=NumberFormat.getCurrencyInstance(); public Item(String itName, double itPrice) { this.itPrice=itPrice; this.itName=itName; } public Item() { } public String getName() { return itName; } public double getPrice() { return itPrice; } public void setName(String someName) { itName=someName; } public void setPrice(double somePrice) { itPrice=somePrice; } public String toString() { //return "Item: " + fmtnum.format(itName) + "Price: $" + fmtnum.format(itPrice); return "Item: " + itName + " Price: $" + itPrice; } static final Comparator<Item>NAME_ORDER = new Comparator<Item>() { public int compare(Item A, Item B) { return A.getName().compareTo(B.getName()); } }; static final Comparator<Item>PRICE_ORDER = new Comparator<Item>() { public int compare(Item A, Item B) { return Double.compare(A.getPrice(), B.getPrice()); } }; }Java Code://header import javax.swing.*; import java.util.*; import java.text.NumberFormat;//NumberFormat public class CoffeeDriver { //main public static void main(String []args) { String customerSort; int x; Item arr[]=new Item[5]; arr[0]=new Item("Coffee",1.00); arr[1]=new Item("Water",2.00); arr[2]=new Item("Milk",1.50); arr[3]=new Item("Bagel",1.25); arr[4]=new Item("Donut",0.75); //NumberFormat fmtnum = NumberFormat.getCurrencyInstance(); int finalSort; System.out.println("Welcome to Wings Coffee Shop. We have a great list of items on the menu."); customerSort=JOptionPane.showInputDialog(null, "Would you like to see these items sorted by name or price. To sort by name press (1). To sort by price press (2)."); finalSort = Integer.parseInt(customerSort); //do while loop if (finalSort == 2); sortItemPrice(arr); if (finalSort == 1); sortItemName(arr); } //sortName public static void sortItemName(Item arr[]) { int x; Item value; for (x=0;x<arr.length;x++) { if (arr[x].getName().compareTo(arr[x+1].getName())>0) { value = arr[x]; arr[x] =arr[x+1]; arr[x+1] =value; } JOptionPane.showMessageDialog(null,arr[x]); //System.out.println(" " + arr[x]); } } //sortPrice public static void sortItemPrice(Item arr[]) { int x=arr.length; Item value; for (x=0;x<arr.length;x++) { if(arr[x].getPrice()>arr[x+1].getPrice()) { value=arr[x]; arr[x]=arr[x+1]; arr[x+1]=value; } System.out.println("" + arr[x]); //JOptionPane.showMessageDialog(null,arr[x]); } } //System.exit(0); }
- 11-26-2012, 09:51 PM #9
Member
- Join Date
- Nov 2012
- Posts
- 6
- Rep Power
- 0
Re: Help with error new to Java
Ok so I've basically did more research, again reading on arrays etc. I've completely rewritten my code but I am down to 10 errors that I cannot
find any braces that don't match etc. which is what I think the errors mean any help or direction would be helpful:
here is my new code that I wrote with errors at the end:
Java Code://header public class Item { private String itName; private double itPrice; public Item(String itName, double itPrice) { this.itPrice=itPrice; this.itName=itName; } public String getName() { return itName; } public double getPrice() { return itPrice; } }//enderrors:Java Code://header import java.util.Scanner; public class CoffeeDriver { //main method public static void main(String[] args) { int x; Item[] arr=new Item[5]; arr[0]=new Item("Coffee",1.00); arr[1]=new Item("Water",2.00); arr[2]=new Item("Milk",1.50); arr[3]=new Item("Bagel",1.25); arr[4]=new Item("Donut",0.75); Scanner input = new Scanner(System.in); String decision; System.out.println("Welcome to Wings Coffee Shop. We have a great list of items on the menu."); System.out.println("Would you like to see these items sorted by name or price? n/p: "); decision = input.nextLine(); if(decision.equals("n")) { sortitName(arr); } if(decision.equals("p")) { sortitPrice(arr); } //sortName public static void sortitName(Item[] arr) { boolean swap=true; int j=0; int x; Item value; while (swap) { swap=false; j++; for (x=0;x<arr.length-j;x++) { if (arr[x].getName().compareTo(arr[x+1].getName())>0) { value = arr[x]; arr[x] =arr[x+1]; arr[x+1] =value; swap=true; } } for (int x=0;x<arr.length;x++) { System.out.println(arr[x].getName() + " " + arr[x].getPrice()); } }; }//end sortName //sortPrice public static void sortitPrice(Item[] arr) { int x; Item value; boolean swap=true; int j=0; while (swap) { swap=false; j++; for (x=0;x<arr.length-j;x++) { if(arr[x].getPrice()>arr[x+1].getPrice()) { value=arr[x]; arr[x]=arr[x+1]; arr[x+1]=value; swap=true; } for (int x=0;x<arr.length;x++) { System.out.println(arr[x].getPrice()+" "+arr[x].getName()); } } }; } }//end main }//end class
----jGRASP exec: javac -g CoffeeDriver.java
CoffeeDriver.java:38: error: illegal start of expression
public static void sortitName(Item[] arr)
^
CoffeeDriver.java:38: error: illegal start of expression
public static void sortitName(Item[] arr)
^
CoffeeDriver.java:38: error: ';' expected
public static void sortitName(Item[] arr)
^
CoffeeDriver.java:38: error: '.class' expected
public static void sortitName(Item[] arr)
^
CoffeeDriver.java:38: error: ';' expected
public static void sortitName(Item[] arr)
^
CoffeeDriver.java:66: error: illegal start of expression
public static void sortitPrice(Item[] arr)
^
CoffeeDriver.java:66: error: illegal start of expression
public static void sortitPrice(Item[] arr)
^
CoffeeDriver.java:66: error: ';' expected
public static void sortitPrice(Item[] arr)
^
CoffeeDriver.java:66: error: '.class' expected
public static void sortitPrice(Item[] arr)
^
CoffeeDriver.java:66: error: ';' expected
public static void sortitPrice(Item[] arr)
^
10 errors
----jGRASP wedge2: exit code for process is 1.
----jGRASP: operation complete.
Thanks,
mlaw
- 11-27-2012, 05:09 AM #10
Similar Threads
-
java.lang.NullpointerException error in tomcat 5.5 / http status 500 error
By rahil in forum Apache CommonsReplies: 3Last Post: 05-08-2012, 05:26 PM -
java out of memory error-heap space error
By elsanthosh in forum NetBeansReplies: 4Last Post: 06-15-2010, 09:31 AM -
Thread: Error 500--Internal Server Error java.lang.NullPointerException
By jackdear44 in forum New To JavaReplies: 1Last Post: 12-05-2009, 07:28 AM -
java.lang.Error: Error opening DSound for capture
By NARs in forum NetworkingReplies: 1Last Post: 10-26-2009, 04:38 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks