You are viewing our Forum Archives. To view or take place in current topics click here.
Anyone have knowledge about Java?
Posted:
Anyone have knowledge about Java?Posted:
Status: Offline
Joined: Apr 22, 201113Year Member
Posts: 1,148
Reputation Power: 44
Trying to figure out how I can create a method that will allow the user to input the details of an object using the the scanner and add it to an ArrayList.
Basically doing a project where two products are stored and there's a customer menu and a staff menu and the staff menu needs to be able to add new stock items and adjust stock levels
Basically doing a project where two products are stored and there's a customer menu and a staff menu and the staff menu needs to be able to add new stock items and adjust stock levels
#2. Posted:
Status: Offline
Joined: May 27, 201113Year Member
Posts: 2,048
Reputation Power: 100
Status: Offline
Joined: May 27, 201113Year Member
Posts: 2,048
Reputation Power: 100
There are numerous different ways it can be achieved, however, unless we know the variable types you're using not much can really be done. Posting those might help a little. Actually posting your source would be the most beneficial way that I could you.
- 0useful
- 0not useful
#3. Posted:
Status: Offline
Joined: Apr 22, 201113Year Member
Posts: 1,148
Reputation Power: 44
Super Class
Sub Class
Sub Class
Driver Class
Guess this is kinda bad with me being at the end of my first year of information systems
- 0useful
- 0not useful
#4. Posted:
Status: Offline
Joined: May 27, 201113Year Member
Posts: 2,048
Reputation Power: 100
Status: Offline
Joined: May 27, 201113Year Member
Posts: 2,048
Reputation Power: 100
From what I can gather from that, you're not really giving any information to the scanner to tell it to change the stock amount. Also, with that route, in order to update the stock, you would have to delete and re-add the stock every time you change a value. This is because in order for their not to be a type mismatch, the entire list gets turned into a String, I believe anyways. So instead of the list being read as a "String, Double, Int, String", instead it gets read as a simple String. To be honest, I myself would go with variables rather than ArrayLists just because they'd be a bit easier to use.
Now I could be wrong myself in my explanation, after all, I myself am not too experienced in Java.
Now I could be wrong myself in my explanation, after all, I myself am not too experienced in Java.
- 1useful
- 0not useful
#5. Posted:
Status: Offline
Joined: Apr 22, 201113Year Member
Posts: 1,148
Reputation Power: 44
5FDP_Jekyll wrote From what I can gather from that, you're not really giving any information to the scanner to tell it to change the stock amount. Also, with that route, in order to update the stock, you would have to delete and re-add the stock every time you change a value. This is because in order for their not to be a type mismatch, the entire list gets turned into a String, I believe anyways. So instead of the list being read as a "String, Double, Int, String", instead it gets read as a simple String. To be honest, I myself would go with variables rather than ArrayLists just because they'd be a bit easier to use.
Now I could be wrong myself in my explanation, after all, I myself am not too experienced in Java.
Thanks for the input unfortunately ArrayLists are required as part of the assignment spec
- 0useful
- 0not useful
#6. Posted:
Status: Offline
Joined: May 27, 201113Year Member
Posts: 2,048
Reputation Power: 100
Status: Offline
Joined: May 27, 201113Year Member
Posts: 2,048
Reputation Power: 100
-Raw wroteI had a feeling that was the case, if you search up ArrayList and how to use them, there's probably tons of topics on StackOverflow that you might find helpful, it's generally where I find my answers to my Java questions.5FDP_Jekyll wrote From what I can gather from that, you're not really giving any information to the scanner to tell it to change the stock amount. Also, with that route, in order to update the stock, you would have to delete and re-add the stock every time you change a value. This is because in order for their not to be a type mismatch, the entire list gets turned into a String, I believe anyways. So instead of the list being read as a "String, Double, Int, String", instead it gets read as a simple String. To be honest, I myself would go with variables rather than ArrayLists just because they'd be a bit easier to use.
Now I could be wrong myself in my explanation, after all, I myself am not too experienced in Java.
Thanks for the input unfortunately ArrayLists are required as part of the assignment spec
- 0useful
- 0not useful
#7. Posted:
Status: Offline
Joined: Aug 19, 201014Year Member
Posts: 5,243
Reputation Power: 532
Status: Offline
Joined: Aug 19, 201014Year Member
Posts: 5,243
Reputation Power: 532
Adding to an ArrayList is simple, you need to just use the .add() method.
Obviously depending on what you need to add to the ArrayList, you can get multiple inputs and capture each of them as part of your object to add to the ArrayList.
Obviously depending on what you need to add to the ArrayList, you can get multiple inputs and capture each of them as part of your object to add to the ArrayList.
Scanner scanner = new Scanner(System.in);
ArrayList myArrayList = new ArrayList<>();
public static void main(String[] args) {
String userInput = scanner.nextLine();
int userInput2 = scanner.nextInt();
MyClass myClass = new MyClass(userInput, userInput2);
myArrayList.add(myClass);
}
public class MyClass {
private String myString;
private int myInt;
public MyClass(String sIn, int iIn) {
myString = sIn;
myInt = iIn;
}
public void setString(String sIn) { myString = sIn; }
public void setInt(int iIn) { myInt = iIn; }
public String getString() { return myString; }
public int getInt() { return myInt; }
}
- 2useful
- 0not useful
#8. Posted:
Status: Offline
Joined: Apr 22, 201113Year Member
Posts: 1,148
Reputation Power: 44
public void addStockB()
{
//local variables
String brand, type;
double price;
int stock;
Scanner scan = new Scanner(System.in);
Bass aBass;
for (int i = 0; i < numberInstruments; i++)
{
//1. Input details from user
System.out.println("\n Input Details Instrument " + (i + 1));
System.out.print("Enter brand name : ");
brand = scan.nextLine();
System.out.print("Enter price : ");
price = scan.nextDouble();
System.out.print("Enter Stock level : ");
stock = scan.nextInt();
scan.nextLine();
aBass = new Bass(brand, price, stock);
list.add(aBass);
{
String fileName;
ObjectOutputStream fileOut;
try{
fileOut = new ObjectOutputStream(new FileOutputStream("shop.dat"));
for (Instruments inst : list)
{
fileOut.writeObject(inst);
}
fileOut.close();
System.out.println("Stock content saved");
}
catch (IOException e)
{
System.out.println("error");
}
}
}
}
Thanks
Yeah I spent some time on it this morning and came out with this thought it worked but can't get the items to display when I run it
- 1useful
- 0not useful
#9. Posted:
Status: Offline
Joined: Aug 19, 201014Year Member
Posts: 5,243
Reputation Power: 532
Status: Offline
Joined: Aug 19, 201014Year Member
Posts: 5,243
Reputation Power: 532
Try making your own print() method in your Bass class.
Something like this:
Something like this:
public String toString() {
String msg = brand; // String = brand name
msg += " " + String.ValueOf(this.price); // msg = "Brand Price"
msg += " " + String.ValueOf(this.stock); // msg = "Brand Price Stock"
return msg;
}
main method {
System.out.println(inst.toString());
}
- 1useful
- 0not useful
#10. Posted:
Status: Offline
Joined: Apr 22, 201113Year Member
Posts: 1,148
Reputation Power: 44
Cheers thanks for all the help
- 0useful
- 0not useful
You are viewing our Forum Archives. To view or take place in current topics click here.