shopping p = new shopping(pname,pprice,pbarcode,0 ,0 ,0);
You are sending it 3 variables and then 3 0's. You constructor..
shopping(String pname, String pbarcode, double pprice, int pquantity)
Calls for the 3 variables, and then 1 int quantity, you need to change those 3 0's to 1 and make it a value that isn't 0 otherwise you will be trying to purchase 0 of that item.
27 Replies - 428 Views - Last Post: 15 November 2011 - 08:17 PM
#17
Re: Help with saving to a file
Posted 15 November 2011 - 11:31 AM
okay guys obviously I am not good at programming I have so many questions. I believe I found the way to get my items to save to a text file. But I am really confused about my barcode. It's supposed to be numbers such as 111 or 222. So is my barcode supposed to be a string or an int. When I have it is a string in certain parts it says requires an int, then I change it and everything gets messed up. So is barcode an int or a string?
#18
Re: Help with saving to a file
Posted 15 November 2011 - 11:33 AM
Well, it is up to you. You can make it a String and when you need it to be an int you can just call Integer.parseInt(barcode) or you can make it an int, and when you need to use it as a String just do barcode+""; to concatnate it.
All depends on what you want, both can work just have to do the proper work around.
All depends on what you want, both can work just have to do the proper work around.
#19
Re: Help with saving to a file
Posted 15 November 2011 - 11:44 AM
okay I am getting an error here
So I'm trying to get an int it keeps saying requires sting.
pbarcode = Integer.parseInt(pbarcode);
So I'm trying to get an int it keeps saying requires sting.
#20
Re: Help with saving to a file
Posted 15 November 2011 - 11:51 AM
Break down: pbarcode = Integer.parseInt(pbarcode);
pbarcode - This is a String
Integer.parseInt(pbarcode) - This is a String you are making into an int
String != int
You need to assign it to a variable that isn't a String...
You use pbarcode in both. So you need to change it so it is assigned to a different variable...
int iBarCode = Integer.parseInt(pbardcode);
iBarCode is an int and pbarcode is a String, you make pbarCode value into an int through the parseInt method and assign that value to iBarCode.
pbarcode - This is a String
Integer.parseInt(pbarcode) - This is a String you are making into an int
String != int
You need to assign it to a variable that isn't a String...
You use pbarcode in both. So you need to change it so it is assigned to a different variable...
int iBarCode = Integer.parseInt(pbardcode);
iBarCode is an int and pbarcode is a String, you make pbarCode value into an int through the parseInt method and assign that value to iBarCode.
This post has been edited by Fuzzyness: 15 November 2011 - 11:52 AM
#21
Re: Help with saving to a file
Posted 15 November 2011 - 12:02 PM
Okay thanks that fixed the error. But now I forgot to add in the price section for the item so I just added that and now my program closes after the input barcode section.
so it doesn't even go to price it just ends the program nor does it let me enter another item. Here is the error its giving me
Exception in thread "main" java.lang.NumberFormatException: null
at java.lang.Integer.parseInt(Integer.java:454)
at java.lang.Integer.parseInt(Integer.java:527)
at program7.Program7.main(Program7.java:42)
I don't know what that means.... Java's really upsetting me
sinput = JOptionPane.showInputDialog("Enter the price of that item");
pprice = Double.parseDouble(sinput);
p.setprice(pprice);
i++;
resp = JOptionPane.showInputDialog("Enter another item? 1: yes 0: no");
cmd = Integer.parseInt(resp);
so it doesn't even go to price it just ends the program nor does it let me enter another item. Here is the error its giving me
Exception in thread "main" java.lang.NumberFormatException: null
at java.lang.Integer.parseInt(Integer.java:454)
at java.lang.Integer.parseInt(Integer.java:527)
at program7.Program7.main(Program7.java:42)
I don't know what that means.... Java's really upsetting me
#22
Re: Help with saving to a file
Posted 15 November 2011 - 12:04 PM
Repost all of your code so that we know what it looks like now. it is a learning process no worries, everything is difficult at first.
#23
Re: Help with saving to a file
Posted 15 November 2011 - 12:08 PM
Okay here is my class
and here is my program
class shopping {
private String name;
private double price;
private String barcode;
private int quantity;
public shopping(){
name = "not valid";
price = 0;
barcode = "unknown";
quantity =0;
}
shopping(String pname, String pbarcode, double pprice, int pquantity) {
name = pname;
barcode = pbarcode;
price = pprice;
quantity = pquantity;
this.add(quantity);
this.buy(quantity);
}
public void setname(String item){
name = item;
}
public String getname(){
return name;
}
public void setprice(double cost){
cost = price;
}
public double getprice(){
return price;
}
public void setbar(String bar){
bar = barcode;
}
public String getbar(String bar){
return barcode;
}
public void setquantity(double num){
num = quantity;
}
public double getquantity(){
return quantity;
}
public void add(int amt){
quantity = quantity + amt;
}
public double add(){
return quantity;
}
public void buy(int amt){
quantity = quantity - amt;
}
public double buy(){
return quantity;
}
}
and here is my program
public static void main(String[] args) {
shopping [] shop = new shopping[100];
PrintWriter ofile = null;
String pname = null;
double pprice;
String pbarcode = null;
int ibarcode = 0;
int pquantity = 0;
int cmd, size = 0;
boolean done;
shopping p = new shopping();
String resp = JOptionPane.showInputDialog( "Choose from:" +
"\n1: Add new products\n2: Shop\n3: Check out ");
cmd = Integer.parseInt(resp);
String sinput = null;
switch(cmd){
case 1: done = false;
int i = 0;
while(!done){
sinput = JOptionPane.showInputDialog("Enter the product name");
p.setname(sinput);
sinput = JOptionPane.showInputDialog("Enter the quantity of that product");
pquantity = Integer.parseInt(sinput);
p.setquantity(pquantity);
sinput = JOptionPane.showInputDialog("Enter the barcode");
ibarcode = Integer.parseInt(pbarcode);
p.setbar(pbarcode);
sinput = JOptionPane.showInputDialog("Enter the price of that item");
pprice = Double.parseDouble(sinput);
p.setprice(pprice);
i++;
resp = JOptionPane.showInputDialog("Enter another item? 1: yes 0: no");
cmd = Integer.parseInt(resp);
if(cmd == 0)
done = true;
size = i;
}
resp = JOptionPane.showInputDialog("enter name of file for this store");
try {
ofile = new PrintWriter(new File(resp));
}
catch(FileNotFoundException e){
System.out.println("Could not open file");
}
for(i = 0; i < size; i++){
ofile.println(shop[i].getname());
ofile.println(shop[i].getprice());
ofile.println(shop[i].getbar(resp));
}
ofile.close();
break;
case 2: JOptionPane.showInputDialog("Enter the barcode of the item you would like to buy");
resp = p.getbar(resp);
JOptionPane.showInputDialog("Enter how many you would like to buy");
pquantity = Integer.parseInt(sinput);
}
}
}
#24
Re: Help with saving to a file
Posted 15 November 2011 - 12:14 PM
public void setprice(double cost){
cost = price;
}
Shouldn't that be price = cost;?
String pbarcode = null;
ibarcode = Integer.parseInt(pbarcode);
I think you mean to replace pbarcode with sinput: sinput = JOptionPane.showInputDialog("Enter the barcode");
#25
Re: Help with saving to a file
Posted 15 November 2011 - 03:19 PM
okay i changed that error in my class but its still stopping after input barcode
#26
Re: Help with saving to a file
Posted 15 November 2011 - 03:41 PM
Does the line look like this: ibarcode = Integer.parseInt(sinput);
#27
Re: Help with saving to a file
Posted 15 November 2011 - 08:01 PM
Yes my code does look like that. I don't know what happened. I added the price feature and now it won't run.
#28
Re: Help with saving to a file
Posted 15 November 2011 - 08:17 PM
Okay I have another question. While shopping the program is supposed to keep track of how many items there are and how many items have been bought. So I set buy in my class to subtract the amount but I am not sure how to incorporate this into my program. Help?
|
|

New Topic/Question
Reply




MultiQuote


|