#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    1
    Rep Power
    0

    Need help with calling methods from classes - Java Assignment


    I'm working through a Java assignment for school, and I've hit a road block about half way through it. The part I'm up to is asking me to add a method to a seperate .java file (MyString.java in this case) and then to call that method in my main program file. Don't get too confused with how the assignment is meant to work or anything, I only really need help with the code to call that method in my main file. I'm going to show you the relevant areas of code in both files and I'm hoping someone can help me out with the code to call that method from the class MyString.java.

    Ok, here is the section taken from the class MyString.java:

    Code:
     public static String createID(C)
       {
          String message = C.substring(0.3).toUpperCase();
          return message+= (int) (Math.random()*99 + 1200);
       }
    Basically what that's going to do is take the first three characters from the lastName field and merge them with a random number between 1200 - 1299 to create an Employee ID.

    My problem is I really have no idea how to call this method in my main file below:

    Code:
     public static void createEmpID()
        {
            JOptionPane.showMessageDialog(null,"In (2) createEmpID()"); <-- This is just filler text, I need the code that goes in here!
        }
    I have posted this on another Java help forum but the only answer so far wasn't as helpful as I'd like. Remember that I'm still a beginner when it comes to Java so please be as descriptive as possible with your answer, thank you!
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    2
    Rep Power
    0
    Ok to start with you havn't declared a return type for your method. This would be String. Your code:

    Code:
    public static String createID(C)   
    {      
      String message = C.substring(0.3).toUpperCase();       
      return message+= (int) (Math.random()*99 + 1200);    
    }
    Here you specify the variable C but it has no data type. Since you are trying to pass back a String, your data type must also be String. E.g.

    Code:
    public static String createID(String C)   
    {       
     String message = C.substring(0.3).toUpperCase();      
     return message+= (int) (Math.random()*99 + 1200);   
    }
    You can then pass your method a value and have a return by creating an object and then by accessing its methods.

    E.g.

    Code:
    MyString ObjectReference = new MyString(); // Create an object 
    System.out.println(ObjectReference.createID("TESTING")); // Print out value

IMN logo majestic logo threadwatch logo seochat tools logo