Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Help with toString and accesing user input

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,684
    Thanks
    25
    Thanked 333 Times in 309 Posts

    Default 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.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,029
    Thanks
    52
    Thanked 2,278 Times in 2,249 Posts

    Default Re: Help with toString and accesing user input

    Cross posted at Help with toString and accesing user input - Java Forums

Similar Threads

  1. Replies: 1
    Last Post: November 2nd, 2012, 02:21 PM
  2. Reading user input from the console with the Scanner class
    By JavaPF in forum Java SE API Tutorials
    Replies: 3
    Last Post: September 7th, 2011, 03:09 AM
  3. JTable Updating String Values from User Input
    By aussiemcgr in forum AWT / Java Swing
    Replies: 5
    Last Post: August 3rd, 2010, 01:48 PM
  4. how do I keep a persistent prompt in JTextArea and allow user input
    By cazmo in forum What's Wrong With My Code?
    Replies: 2
    Last Post: February 23rd, 2010, 12:14 PM
  5. User Input Loop
    By cfmonster in forum Loops & Control Statements
    Replies: 7
    Last Post: August 24th, 2009, 01:52 PM