Help with toString and accesing user input
Hello Im new to java and Im having trouble getting my code to work. Im able to call some methods but others dont work right.
Here is what I have to do:
Create a class called myStuff that contains a constructor, getters and setters and a toString method. The private data members will be: firstName, age, shoeSize, favoriteSnack and favoriteMovie. Create a driver class that creates an instance of myStuff in main. Get the information from the user and “set” it into the object using the setter methods. Display the information to the screen using the toString method.
EX.
What is your name? Abigail
What is your age? 22
What is your favorite snack? Doritos
Output:
Abigail
Age: 22
Snack: Doritos
******Here is myStuff Class**********8
import java.util.Scanner;
public class myStuff {
private String name, newName, snack, newSnack;
private int age, newAge;
private double shoeSize;
Scanner in = new Scanner(System.in);
public myStuff()
{
this.name = name;
this.snack = snack;
this.age = age;
}
//methods
public String setName(){
System.out.println("What is your name?");
name = in.nextLine();
return name;
}
public void getName(){
name = newName;
}
public int setAge(){
System.out.println("What is your age?");
age = in.nextInt();
return age;
}
public void getAge(){
age = newAge;
}
public String setSnack(){
System.out.println("What is your favorite snack?");
snack = in.nextLine();
return snack;
}
public void getSnack(){
snack = newSnack;
}
public String toString(){
String result = Integer.toString(age);
return result;
}
}
**********DriverClass********************
import java.util.Scanner;
public class Driver {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
myStuff obj_1;
obj_1 = new myStuff();
obj_1.setAge();
obj_1.setName();
obj_1.setSnack();
System.out.println(obj_1);
}
}
************************************************** ************************
My question is how do I store all the user input and Display them using the toString?
- I called the methods on the main class, but after I call setAge()method I can't input any other info.
THANKS FOR ANY HELP
Re: Help with toString and accesing user input
A few things before we can help you, remember these on all your other posts:
1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code. This makes it easier for everyone to read your code.
2. Explain to us what exactly your code is doing that is wrong. Give full details of errors and provide us with as much information about the situation as possible.
3. Give us an example of what the output should look like when done correctly. This gives everyone an understanding of what the final product should be, compared to what you have now.
Make those changes to your post, and do those on all your future posts, and you will receive help MUCH faster. Until those are done, I really can't help you much, as I don't have a full understanding to what is going on.
Now, for once you get those settled:
I'm curious as to why you seemed to have done things the way you did. Usually, you want to include your IO stuff in your main and do calls to methods with/without parameters instead of doing the IO in your methods. Clean up your previous post, and I can go into more detail for you.
Re: Help with toString and accesing user input