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: Return randomized String to other class in project

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Location
    Sweden
    Posts
    19
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Return randomized String to other class in project

    Hi, I want to return a randomized string from Class2 to Class1 (the classes are in same "Project". How do I do to make the returned String relevant in Class1?

    CLASS 1
    public class p12NameGen
    {
     
        public static void main(String[] args) {
     
            //I want the randomized String from class 2 to be here
     
        }
     
    }//class end

    CLASS2
    import java.util.Random;
     
     
    public class p12NamnLista
    {
     
     Random slumpNamn = new Random();
     String[] p12namn ={"Coolboy_97"};
     
     
     
        public String getp12Namn()
        {
            int namn = slumpNamn.nextInt(p12namn.length);
            return p12namn[namn];
     
        }
     
    }// class end

    Excuse the sweinglish in the code


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Return randomized String to other class in project

    See Classes and Objects for a thorough description on how to use classes and objects. This should hopefully answer your question, if not let us know if there is still confusion

  3. #3
    Junior Member
    Join Date
    Feb 2011
    Location
    Sweden
    Posts
    19
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default Re: Return randomized String to other class in project

    Ok yes thanks a lot , it helped me understand a bit about classes. Then I looked on the code of a almost similar program we wrote in school and did it like this.


    CLASS 1
    public class p12NameGen
    {
     
     
     
        public static void main(String[] args)
        {
     
           p12NamnLista randomp12Namn = new p12NamnLista();
           System.out.println(randomp12Namn.toString());
     
     
        }
     
    }//class end


    CLASS 2
    public class p12NamnLista
    {
     
      String[] p12namn ={"Coolboy_97", "Niklas_Paladin"};
     
      int namnet = (int) (p12namn.length*Math.random());
     
      private String namn = p12namn[namnet];
     
        public String getNamn() {
            return namn;
        }
     
        public void setNamn(String namn) {
            this.namn = namn;
        }
     
        @Override
        public String toString()
        {
            String slumpatNamn = this.namn;
            return slumpatNamn;
        }
     
     
     
    }// class end

    Works fine
    Last edited by Fermen; February 16th, 2011 at 06:01 PM.

Similar Threads

  1. Use of Exceptions and Methods of The String Class
    By cagataylina in forum Exceptions
    Replies: 1
    Last Post: April 26th, 2011, 01:56 AM
  2. Randomized array members.
    By Dion_sama in forum What's Wrong With My Code?
    Replies: 7
    Last Post: December 23rd, 2010, 03:58 PM
  3. Getting a string value from within a method in another class
    By sp11k3t3ht3rd in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 11th, 2010, 04:17 PM
  4. How to return sub string from String
    By humdinger in forum Collections and Generics
    Replies: 1
    Last Post: February 14th, 2010, 11:16 AM
  5. Replies: 0
    Last Post: February 2nd, 2010, 08:20 AM

Tags for this Thread