Page 1 of 3 123 LastLast
Results 1 to 20 of 51
Like Tree1Likes

Thread: Getting all info from one class to another

  1. #1
    NitroDev is offline Member
    Join Date
    Feb 2014
    Posts
    58
    Rep Power
    0

    Question Getting all info from one class to another

    Hello! It's me again and with another problem; i wan to get info from one class to another LIKE ALL information. Trust me when i say that i have looked around the internet but no proper answer.

    Ps. Please notice that i'm a beginner at java programming so i don't want a gigantic code that i won't understand.

  2. #2
    notivago is offline Heavy Coffe Drinker
    Join Date
    Feb 2014
    Location
    São Paulo, Brazil
    Posts
    29
    Rep Power
    0

    Default Re: Getting all info from one class to another

    What do you mean by all info? You want meta data from one class? You want to copy all fields?

    So if the solution to your problem is extensive we should not bother replying, right?

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Getting all info from one class to another

    wan to get info from one class to another LIKE ALL information.
    Create a new class that holds "ALL information". Add a getter method to the class that holds that info and have it create an instance of the new class and return it.
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,685
    Rep Power
    5

    Default Re: Getting all info from one class to another

    Quote Originally Posted by NitroDev View Post
    Hello! It's me again and with another problem; i wan to get info from one class to another LIKE ALL information. Trust me when i say that i have looked around the internet but no proper answer.
    You can't copy info to a CLASS since a class is static, I'll assume you are talking about objects. So basically you want to clone() an object? Copy all the values in all the properties from one instance to another?

    clone (Java method) - Wikipedia, the free encyclopedia
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    NitroDev is offline Member
    Join Date
    Feb 2014
    Posts
    58
    Rep Power
    0

    Default Re: Getting all info from one class to another

    Well gimbal for your first reply i really want to get the info from this class:
    Java Code:
    public class World_Creation {
    
    	public static void main(String[] args) {
    		System.out.println("Now we must create a world for you!");
    		// TODO Auto-generated method stub
    
    	}
    
    }
    To this one
    Java Code:
    import java.util.Scanner;
    
    public class Character_Creation {
    	
    	public static void main(String[] args) {
    		Scanner stats = new Scanner(System.in); //creating the scanner object
    
    		System.out.println("Enter your name:"); //ask a question
    	    String name = stats.nextLine();   //get a String response and store it in a String called "name"
    	    System.out.println("So your name is " + name + "!");
    	    
    	    System.out.println("What is your age?"); //Ask another question
    	    int age = stats.nextInt();
    	    
            //now we need to check something
            if (age < 18) {                   //if statements DO NOT have semicolons at the end.
                System.out.println("Too Young!");
            } else {  //if the previous check returns false, this will be called.
                System.out.println("Welcome!");
            }
    
    	}
    }
    Or the contents

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,383
    Rep Power
    5

    Default Re: Getting all info from one class to another

    Well, you can pass information between class instances using methods. But your requirement is vague. You need to explain exactly what you want to do.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  7. #7
    NitroDev is offline Member
    Join Date
    Feb 2014
    Posts
    58
    Rep Power
    0

    Default Re: Getting all info from one class to another

    didn't my latest reply explained that?

  8. #8
    notivago is offline Heavy Coffe Drinker
    Join Date
    Feb 2014
    Location
    São Paulo, Brazil
    Posts
    29
    Rep Power
    0

    Default Re: Getting all info from one class to another

    Here is what you have to do, assuming you are using eclipse, click on World_Creation ctrl+a, ctrl+c then click on Character_Creation ctrl+a, ctrl+v

    This way you will have all info from World_Creation written into Character_Creation.

    By the way convention suggests the class names should be: WorldCreation written into CharacterCreation.

  9. #9
    NitroDev is offline Member
    Join Date
    Feb 2014
    Posts
    58
    Rep Power
    0

    Default Re: Getting all info from one class to another

    Notivago that would just replace the contents from world_creation to Character_Creation and i wan to ADD thecontents

  10. #10
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,383
    Rep Power
    5

    Default Re: Getting all info from one class to another

    I hope you don't really mean you want to do a copy/paste operation. That is unrelated to Java.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  11. #11
    notivago is offline Heavy Coffe Drinker
    Join Date
    Feb 2014
    Location
    São Paulo, Brazil
    Posts
    29
    Rep Power
    0

    Default Re: Getting all info from one class to another

    Then you just have to leave the ctrl+a out of the second part and place your cursor at the end of the file.

  12. #12
    NitroDev is offline Member
    Join Date
    Feb 2014
    Posts
    58
    Rep Power
    0

    Default Re: Getting all info from one class to another

    Quote Originally Posted by jim829 View Post
    I hope you don't really mean you want to do a copy/paste operation. That is unrelated to Java.

    Regards,
    Jim
    Nope i wan to learn java too so none of that copy paste stuff

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Getting all info from one class to another

    Sounds like a smart text editor that will copy most of the contents of one file into another file.
    If you don't understand my response, don't ignore it, ask a question.

  14. #14
    NitroDev is offline Member
    Join Date
    Feb 2014
    Posts
    58
    Rep Power
    0

    Default Re: Getting all info from one class to another

    Quote Originally Posted by Norm View Post
    Sounds like a smart text editor that will copy most of the contents of one file into another file.
    And what do you exactly mean by that Norm?

  15. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Getting all info from one class to another

    click on World_Creation ctrl+a, ctrl+c then click on Character_Creation ctrl+a, ctrl+v
    Ctrl+X sounds like a text editor command.

    Can you post a small example of the classes before and after the info is copied from one to the other?
    Show the contents of the two classes BEFORE and again AFTER the copy is done.
    If you don't understand my response, don't ignore it, ask a question.

  16. #16
    notivago is offline Heavy Coffe Drinker
    Join Date
    Feb 2014
    Location
    São Paulo, Brazil
    Posts
    29
    Rep Power
    0

    Default Re: Getting all info from one class to another

    Quote Originally Posted by NitroDev View Post
    And what do you exactly mean by that Norm?
    He means that if you really want to go pro you could:
    Java Code:
    $ cat World_Creation.java >> Character_Creation.java
    Or you could:
    Java Code:
    $ vim  Character_Creation.java
    G
    :r World_Creation.java
    That should do the trick.

    P.S.: There is so many ways of copying "like all the info" from one class into another.
    Last edited by notivago; 02-11-2014 at 10:25 PM.

  17. #17
    NitroDev is offline Member
    Join Date
    Feb 2014
    Posts
    58
    Rep Power
    0

    Default Re: Getting all info from one class to another

    Okay how about this; Is there a way to get from class Character_Creation to World_Creation one?

  18. #18
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Getting all info from one class to another

    get from class Character_Creation to World_Creation
    Depends. What does "get from" mean?
    One class can call the methods of another class.
    If you don't understand my response, don't ignore it, ask a question.

  19. #19
    notivago is offline Heavy Coffe Drinker
    Join Date
    Feb 2014
    Location
    São Paulo, Brazil
    Posts
    29
    Rep Power
    0

    Default Re: Getting all info from one class to another

    Quote Originally Posted by NitroDev View Post
    Okay how about this; Is there a way to get from class Character_Creation to World_Creation one?
    Get exactly what? Your question is vague at best. And your classes do not have anything in them besides methods. Was your intention to get the input from the user and store it? Your code is more akin to C than to Java. As far as I can see, your classes have nothing to be passed. They have two main methods which usually would be 2 different entry points for aplication and should only bootstrap the application, one of whish is empty and absolutly does nothing and other that reads values into local variable and print then and then throws away any collected information.

    My serious(aside the horsing around with copy/paste) advice for you is start by following the tutorial and read a book(Head first java is my one time favorite), follow all the exercises so that you grasp the root concepts of Java and OO. Then try to write your simple game.

    If you jump-in development and try it without the most rudimentary understanding of what you are doing it will be a never ending frustration for you and for us.

  20. #20
    NitroDev is offline Member
    Join Date
    Feb 2014
    Posts
    58
    Rep Power
    0

    Default Re: Getting all info from one class to another

    Quote Originally Posted by notivago View Post
    Get exactly what? Your question is vague at best. And your classes do not have anything in them besides methods. Was your intention to get the input from the user and store it? Your code is more akin to C than to Java. As far as I can see, your classes have nothing to be passed. They have two main methods which usually would be 2 different entry points for aplication and should only bootstrap the application, one of whish is empty and absolutly does nothing and other that reads values into local variable and print then and then throws away any collected information.

    My serious(aside the horsing around with copy/paste) advice for you is start by following the tutorial and read a book(Head first java is my one time favorite), follow all the exercises so that you grasp the root concepts of Java and OO. Then try to write your simple game.

    If you jump-in development and try it without the most rudimentary understanding of what you are doing it will be a never ending frustration for you and for us.
    Okay here:
    Java Code:
    import java.util.Scanner;
     
    public class Character_Creation {
         
        public static void main(String[] args) {
            Scanner stats = new Scanner(System.in); //creating the scanner object
     
            System.out.println("Enter your name:"); //ask a question
            String name = stats.nextLine();   //get a String response and store it in a String called "name"
            System.out.println("So your name is " + name + "!");
             
            System.out.println("What is your age?"); //Ask another question
            int age = stats.nextInt();
             
            //now we need to check something
            if (age < 18) {                   //if statements DO NOT have semicolons at the end.
                System.out.println("Too Young!");
            } else {  //if the previous check returns false, this will be called.
                System.out.println("Welcome!");
               //i want to "jump" from this classes end
            }
     
        }
    }
    Java Code:
    public class World_Creation {
     
        public static void main(String[] args) {
           //to here
            System.out.println("Now we must create a world for you!");
            // TODO Auto-generated method stub
     
        }
     
    }

Page 1 of 3 123 LastLast

Similar Threads

  1. Replies: 10
    Last Post: 04-20-2011, 02:03 AM
  2. Print info from a different class?
    By liquidtoon in forum New To Java
    Replies: 3
    Last Post: 11-01-2010, 01:19 PM
  3. Replies: 10
    Last Post: 06-17-2010, 04:22 AM
  4. How it is possible view info about class?
    By artemff in forum New To Java
    Replies: 3
    Last Post: 12-31-2009, 12:17 AM
  5. Getting Class info
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-06-2007, 02:46 PM

Posting Permissions

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