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 5 of 5

Thread: How to call string in another class in java?

  1. #1
    Junior Member
    Join Date
    Apr 2009
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default How to call string in another class in java?

    Hi to all..
    i ask for a little help thanks
    here's my problem, i need to call two different strings in the other class and provide any value for it. here's what ive done so far

    Here's my first class
    public class Student
    {
    String Name,Course;
     
    public void setNme(String inputName)
    {
    Name=inputName;
    }
    public String getNme()
    {
    return Name;
    }
     
    public void setCrs(String inputCourse)
    {
    Course=inputCourse;
    }
    public String getCrs()
    {
    return Course;
    }
    }

    Here's the other one that call the Student Class

    public class Outcome
    {
    public static void main(String[]args)
    {
     
    Student a = new Student();
    a.setNme("charles");
    System.out.print(a.getNme());
     
    Student b = new Student();
    b.setCrs("\ncB.S. Computer Science");
    System.out.print(b.getCrs());
    }
    }

    I can call the first String which is Name but the second one which is Course i always get an error. any help would do but please dont use advance scripts, just the basic only thanks.

    Here's the error message i get.


    Last edited by Deep_4; November 7th, 2012 at 01:24 PM.


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: JAVA Calling Class

    Hello tazjaime and welcome to the forums

    I just compiled the code you posted above and it all works fine?

    I get the output:

    charles
    cB.S. Computer Science

    When is this error generated?
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  3. The Following User Says Thank You to JavaPF For This Useful Post:

    tazjaime (April 23rd, 2009)

  4. #3
    Junior Member
    Join Date
    Apr 2009
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: JAVA Calling Class

    i dont know why i got that error but when i copy paste it in a new notepad it works but here's my other problem...

    when using "int"

    Java Code
    public class Outcome
    {
    public static void main(String[]args)
    {
    Student a = new Student();
    a.setNme("\nCharles Jaime");
    System.out.print(a.getNme());
     
    Student b = new Student();
    b.setStudNum("\n0510180");
    System.out.print(b.getStudNum());
     
    Student c = new Student();
    c.setCrs("\nB.S. Computer Science");
    System.out.print(c.getCrs());
    }
    }
    Java Code
    public class Student
    {
    String Name,Course;
    int StudentNumber;
     
        public void setNme(String inputName)
        {
        Name=inputName;
        }
        public String getNme()
        {
        return Name;
        }
     
        public void setStudNum(int inputStudentNumber)
        {
        StudentNumber=inputStudentNumber;
        }
        public int getStudNum()
        {
        return StudentNumber;
        }
     
        public void setCrs(String inputCourse)
        {
        Course=inputCourse;
        }
        public String getCrs()
        {
        return Course;
        }
     
    }
    Here's the error when using int or double

    Last edited by tazjaime; April 23rd, 2009 at 08:55 AM.

  5. #4
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: JAVA Calling Class

    The value 0510180 is too long to be an Integer.

    I suggest handling it as a String like this:

    public class Student {
        String Name, Course;
        String StudentNumber;
     
        public void setNme(String inputName) {
            Name = inputName;
        }
     
        public String getNme() {
            return Name;
        }
     
        public void setStudNum(String inputStudentNumber) {
            StudentNumber = inputStudentNumber;
        }
     
        public String getStudNum() {
            return StudentNumber;
        }
     
        public void setCrs(String inputCourse) {
            Course = inputCourse;
        }
     
        public String getCrs() {
            return Course;
        }
     
    }
    public class Outcome {
     
        public static void main(String[] args) {
            Student a = new Student();
            a.setNme("Charles Jaime");
            System.out.println(a.getNme());
     
            Student b = new Student();
            b.setStudNum("0510180");
            System.out.println(b.getStudNum());
     
            Student c = new Student();
            c.setCrs("B.S. Computer Science");
            System.out.println(c.getCrs());
        }
    }
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  6. #5
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: JAVA Calling Class

    Also, I personally would use:

    System.out.print[B]ln[/B](b.getStudNum());

    Instead of including the new line char in the String.

    b.setStudNum("[B]\n[/B]0510180");
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

Similar Threads

  1. How to Sort an Array using the java.util.Arrays class
    By JavaPF in forum Java SE API Tutorials
    Replies: 2
    Last Post: May 17th, 2014, 01:16 AM
  2. Replies: 1
    Last Post: December 22nd, 2011, 09:55 AM
  3. How to Sendkeys to an application in Java using the Robot Class
    By JavaPF in forum Java SE API Tutorials
    Replies: 6
    Last Post: August 4th, 2011, 12:13 AM