Results 1 to 10 of 10
  1. #1
    waltersk20 is offline Member
    Join Date
    Oct 2010
    Posts
    5
    Rep Power
    0

    Default Help with toString and accesing user input

    So I'm a beginner at java and I'm trying to write a program that uses a "Driver" and "worker" class. So I know that the Driver class will contain main and that it is there that I have to use the set and get methods. The confusing part for me is how to get input from the user then taking through the "Worker" class so it can be proccess by the toString method and call on the main class.

    Thanks for any help...

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    Quote Originally Posted by waltersk20 View Post
    The confusing part for me is how to get input from the user then taking through the "Worker" class so it can be proccess by the toString method and call on the main class.
    I read that sentence several times and can only join you in being confused.

    I'm not trying to be a smart@ass, but sometimes people leap to questions about how they should do something without first being clear (and clearly expressing) what they are trying to do.

    If this is a homework assignment and you can't understand it, perhaps you could post it in the hope that someone can suggest what it might mean.

  3. #3
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    I think I get what you're trying to do:
    1) In the Driver.main method, capture some input
    2) Save this in a Worker object
    3) Get some value from the Worker.toString method
    4) Use this value later in the Driver.main method

    Is that what you're looking to do? If so, you'll want these links (labelled for each step):
    1) Read interactive command-line input with Java | Java read command line input | devdaily.com
    2a) Understanding Instance and Class Members (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
    2b) InformIT: Working with Objects in Java > Creating New Objects
    3) Java toString Method | Java Beginner
    4) Variables (The Java™ Tutorials > Learning the Java Language > Language Basics) (pretty straight-forward)

    If that ISN'T what you were looking for, then you will have to be more specific. ;)

  4. #4
    waltersk20 is offline Member
    Join Date
    Oct 2010
    Posts
    5
    Rep Power
    0

    Default

    Sorry, for the confusing question. Well, here it is what my hw says:

    ////*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 first name? Abigail
    What is your shoe size? 22

    ----Output--

    Abigail
    Age: 22


    *////

    and this is what I have so far:

    **********Worker Class*****************
    import java.util.Scanner;
    public class myStuff {

    private String name;
    private int age, newAge;

    Scanner in = new Scanner(System.in);

    public myStuff(){
    this.name = name;
    this.age = age;
    }

    //methods
    public int setAge(){
    System.out.println("What is your age?");
    age = in.nextInt();
    return age;

    }

    public void getAge(){

    age = newAge;
    }

    public String toString(){
    String result = Integer.toString(newAge);
    return result;
    }

    }

    ****************Driver Class************************


    public class Driver {


    public static void main(String[] args) {



    myStuff obj_1;

    obj_1 = new myStuff();

    obj_1.getAge();


    }

    }
    ************************************************** **

    When I call the method "getAge" nothing happens.

    So my question is, How do I call my methods on the main class and then display the input back through the "toString" method?


    THANK YOU FOR YOUR HELP.

  5. #5
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    The line private int age, newAge; declares two integers as members of the class--age, and newAge, both initialized with an integer value of null (which is zero).

    The line this.age = age; simply sets the age variable to itself, not changing its value.

    The line age = newAge; (in getAge) sets the variable age to the value of newAge, which has the same value as it--null (zero).

    So, in essence, here is what you are doing: creating two variables, and not changing their values.

    You will want to familiarize yourself with link 2a in my post above. This tells you how to access class variables. You will also want to learn how to pass parameters--for example, in your constructor, you will want to accept "name" and "age" as parameters to set this.age and this.name with.

    For creating getter/setter methods, try this link: Java Basics: OOP 7. Private data - TimeOfDay ; it describes how getting and setting should work relative to class variables (age and newAge in your case). This link: Providing Constructors for Your Classes (The Java™ Tutorials > Learning the Java Language > Classes and Objects) also has some stuff on creating a good constructor.

    That should at least get you started. Also, when posting code, please use [code] tags. ;)

    Best of luck!

  6. #6
    waltersk20 is offline Member
    Join Date
    Oct 2010
    Posts
    5
    Rep Power
    0

    Default

    thanks, but how do I get the user to set those values?

  7. #7
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    You've already done this. You just have to call your setAge() method.

  8. #8
    waltersk20 is offline Member
    Join Date
    Oct 2010
    Posts
    5
    Rep Power
    0

    Default

    So i call the setAge() method, and it works fine, but then when I call a setName() method. Im not allowed to input anything. Also i don't really understand how to retrieve all the store information throught the "toString()" method.

    .

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

    Default

    how to retrieve all the store information throught the "toString()" method.
    Are all the variables with the information available to the toString() method so that it can use them to build a String to be returned?

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,899
    Rep Power
    25

Similar Threads

  1. Need help getting input(first/last name) from user
    By nightrise420 in forum New To Java
    Replies: 11
    Last Post: 09-11-2010, 04:09 AM
  2. User Input
    By brmcdani in forum New To Java
    Replies: 2
    Last Post: 02-05-2010, 02:59 AM
  3. how to get input from User
    By Alvaro in forum New To Java
    Replies: 7
    Last Post: 01-16-2010, 12:02 AM
  4. User input- Pop Up Box
    By dedachi in forum AWT / Swing
    Replies: 3
    Last Post: 03-23-2009, 05:47 AM
  5. cant take input from user
    By new_1 in forum New To Java
    Replies: 6
    Last Post: 12-25-2007, 08:38 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •