Results 1 to 3 of 3
  1. #1
    arnelcolar's Avatar
    arnelcolar is offline Member
    Join Date
    Jul 2009
    Posts
    23
    Rep Power
    0

    Default I can't get to call a method to another method..

    This program that I've been working out right now is supposed to convert a number into words. Say that a user inputs 1234, the output should be one "thousand two hundred thirty four". Heres the code of my work:

    Java Code:
     
    
    import java.io.*;
    
    class Places { //< is this right? 
    String first,second,third,last;
    
    String getFirst() {
    return first;
    }
    
    String getSecond() {
    return second;
    }
    
    String getThird() {
    return third;
    }
    
    String getLast() {
    return last;
    }
    }
    public class NumbertoString {
    
      public static void main(String args[]) throws Exception {
        String query/*,first = "",second = "",third="",last=""*/;
        int x,ones,tens,hundreds,thousands;
        BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));
    
    System.out.println("Enter a number");
    query=dataIn.readLine();
    
    char[] temp = query.toCharArray();
    ones = Integer.parseInt(Character.toString(query.charAt(3)));
    tens = Integer.parseInt(Character.toString(query.charAt(2)));
    hundreds = Integer.parseInt(Character.toString(query.charAt(1)));
    thousands = Integer.parseInt(Character.toString(query.charAt(0)));
    
    
    
    
    
    switch (ones) {
    
    case 1:
    Places.getLast ="one";//< I'm trying to call out a method but it wont work
    
    break;
    case 2: 
    System.out.print("two");
    break;
    case 3:
    System.out.print("three");
    break;
    case 4:
    System.out.println("four");
    break;
    case 5:
    System.out.println("five");
    break;
    case 6:
    System.out.println("six");
    break;
    case 7:
    System.out.print("seven");
    break;
    case 8:
    System.out.print("eight");
    break;
    case 9: 
    System.out.print("nine");
    break;
    }//end ones
    for (x=10;x<20;x++){
      System.out.print(x);
    }//end for
    
    int teen = ones + tens;
    if(teen == x) {
    switch (teen) {
    case 11:
    System.out.print("eleven");
    break;
    case 12:
    System.out.print("twelve");
    break;
    case 13:
    System.out.print("thirteen");
    break;
    case 14:
    System.out.print("fifteen");
    break;
    case 15:
    System.out.print("fourteen");
    break;
    case 16:
    System.out.print("fifteen");
    break;
    case 17:
    System.out.print("sixteen");
    break;
    case 18:
    System.out.print("seventeen");
    break;
    case 19:
    System.out.print("nineteen");
    break;
    }//end if
    }//end switch(teen)
    switch (tens)
    {
    
    
    case 1:
    System.out.print("ten");
    break;
    case 2:
    System.out.print("twenty");
    break;
    case 3:
    System.out.print("thirty");
    break;
    case 4:
    System.out.print("fourty");
    break;
    case 5:
    System.out.print("fifty");
    break;
    case 6:
    System.out.print("sixty");
    break;
    case 7:
    System.out.print("seventy");
    break;
    case 8:
    System.out.print("eighty");
    case 9:
    System.out.print("ninety");
    break;
    }//end tens
    
    switch(hundreds) 
    {
    case 1:
    System.out.print("One Hundred");
    break;
    case 2:
    System.out.print("Two Hundred");
    break;
    case 3:
    System.out.print("Three Hundred");
    break;
    case 4:
    System.out.print("four hundred");
    break;
    case 5:
    System.out.print("five hundred");
    break;
    case 6:
    System.out.print("six hundred");
    break;
    case 7:
    System.out.print("seven hundred");
    break;
    case 8:
    System.out.print("eight hundred");
    break;
    case 9:
    System.out.print("nine hundred");
    break;
    }//end switch(hundreds);
    
    switch(thousands) 
    {
    case 1:
    System.out.print("one thousand");
    break;
    case 2:
    System.out.print("two thousand");
    break;
    case 3:
    System.out.print("three thousand");
    break;
    case 4:
    System.out.print("four thousand");
    break;
    case 5:
    System.out.print("five thousand");
    break;
    case 6:
    System.out.print("six thousand");
    break;
    case 7:
    System.out.print("seven thousand");
    break;
    case 8:
    System.out.print("eight thousand");
    break;
    case 9:
    System.out.print("nine thousand");
    break;
    }//end switch(thousands)
    
    
    
    }
    }
    
    Never mind the whole code(I know it's messy), I just want to know how will I be able to call a method from another method. I'll ask for a cleaner and shorter code for this later on. I just want this to work. :)
    
    thank you! :)

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You need to read up on arrays and use them here as they will simplify your code more 5 to 10 fold. As to using methods, you simply call them. You may wish to go over the basics at the Sun tutorial here: Lesson: Language Basics (The Java™ Tutorials > Learning the Java Language)

    To call a method without an object, you need to either make the method static, and then call the method on the class, or if you have a non-static method, create an object Places object and call the method on the object. For instance:

    Java Code:
    public class Foo {
      
      // non-static method -- needs an object to call on
      public String getString1() {
        return "String 1";
      }
      
      // static method -- can be called on the class
      public static String getString2() {
        return "String 2";
      }
    }

    Java Code:
    public class Bar {
      public static void main(String[] args) {
    
        // can't call a non-static method on the class
        // System.out.println(Foo.getString1()); // this won't work
        
        // but can call the static method on the Foo class
        System.out.println(Foo.getString2());
        
        
        // create a Foo object, foo
        Foo foo = new Foo();
        // and can call the non-static method on the object
        System.out.println(foo.getString1());
      }
    }

  3. #3
    Arnold is offline Member
    Join Date
    Oct 2009
    Location
    Rotterdam
    Posts
    52
    Rep Power
    0

    Default

    It's true you need to make your methods static, but I saw you forgot to add the () after calling the method.

Similar Threads

  1. How to call the equals method
    By random0munky in forum New To Java
    Replies: 1
    Last Post: 10-12-2009, 05:33 PM
  2. how to call method?
    By leapinlizard in forum New To Java
    Replies: 9
    Last Post: 04-29-2009, 11:55 PM
  3. How to call method in servet by using JSP?
    By frankjava1 in forum Java Servlet
    Replies: 2
    Last Post: 10-24-2008, 04:20 AM
  4. Call Java Method
    By hussainzim in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 05-15-2008, 07:22 AM
  5. cannot call private method from static method
    By jon80 in forum New To Java
    Replies: 3
    Last Post: 05-07-2008, 08:37 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
  •