Results 1 to 8 of 8
  1. #1
    nicolek808 is offline Member
    Join Date
    Sep 2009
    Posts
    9
    Rep Power
    0

    Default Recursion with int and string error message

    For this program i need to print a user inputted string based upon a user inputted int using recursion that prints in the main method.... this is one of the methods...and I am getting an error message

    printString(int,java.lang.String) cannot be applied to (int)

    thanks in advance for your help! :)

    Java Code:
    public static String printString(  int count, String oneWord)
          {
             String myString;  
            
             if( count == 1 ) 
             {
                return oneWord;
             } 
             else 
             {
                myString = printString ((count -1)) + oneWord; 
             	
                return myString;  
             }
          }

  2. #2
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    8

    Default

    myString = printString ((count -1)) + oneWord;

    input parameters not matches
    you should call Object.printString(1, '1 word');
    not Object.printString(1);

  3. #3
    nicolek808 is offline Member
    Join Date
    Sep 2009
    Posts
    9
    Rep Power
    0

    Default

    user input is a integer and a string....

    here is the full code:
    Java Code:
       public static void main(String[] commandlineArguments) 
          {
             if (commandlineArguments.length == 0) {
                System.out.print("Error: You must enter at least 1 commandline argument ");
             } 
             else 
             {
               
                Integer number = new Integer(0); //initialize number
                String word = commandlineArguments[1];
             	         
                try // check to verify if input is an integer
                {
                   number = Integer.parseInt(commandlineArguments[0]);
                } 
                    catch (NumberFormatException exception) // NumberFormatException
                   {
                      System.out.print(exception);
                      System.out.println(" is not an integer");
                      System.exit(1); // end program
                   }
                      
                String outputString; // declares String
                Integer outputInt; // declares Integer
             
                outputString = printString(number, word); 
                System.out.println( outputString );
                        
             
             }
          }
    
       
           public static String printString(int count, String oneWord)
          {
              
             String myString;  
            
             if( count == 1 ) 
             {
                return oneWord;
             } 
             else 
             {
                myString = Object.printString ((count -1)) + oneWord; 
             	
                return myString;  
             }
          }

  4. #4
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    10

    Default

    You call it wrong in your printString method.
    Also Object.printString is weird. You didn't call your class Object did you?

  5. #5
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    8

    Default

    Java Code:
    myString = printString ((count -1)) + oneWord;
    you declare method printString to intake 2 parameter, 1 for int, 1 for String
    and then you try to call a printString method with 1 parameter (1 int input)
    as you dont declare printString(int), java can not compile the code

  6. #6
    nicolek808 is offline Member
    Join Date
    Sep 2009
    Posts
    9
    Rep Power
    0

    Default

    I do understand....but i'm not sure which parameter to pass to it. Is it
    oneWord?

    Java Code:
                myString = printString (oneWord +(count -1)) + oneWord;
    I get this message:

    printString(int,java.lang.String) in KashiwabaraNicole8 cannot be applied to (java.lang.String)
    myString = printString (oneWord +(count -1)) + oneWord;
    ^
    1 error

  7. #7
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    10

    Default

    I have no idea what you are trying to do with the code so I can't say what you should pass your method. Why are you using recursion? What's the relationship between the user entered number and the printed String?

  8. #8
    nicolek808 is offline Member
    Join Date
    Sep 2009
    Posts
    9
    Rep Power
    0

    Default

    This is what the output should be if commandline argument is: 10 ten
    (prints 'ten' 10 times)

    tentententententententententen

    its part of an assigment...and recursion is required...I am able to do it without using recursion, however....its part of the requirement :( There are other methods required....

    1. Method 1: Return and print in main() X number of S (SSSS...), in other words print the String S, X times.
    2. Method 2: Return and print in main() the first character in S, X number of times (ssss...), where "s" is the first letter in String "S".
    3. Method 3: Return and print in main() each letter in S two times (s0s0s1s1s2s2s3s3...) where s0 is the first letter, s1 is the second letter, s2 is the third letter, etc.
    4. Method 4: Return and print in main() each letter in S three times backwards (sn-1sn-1sn-1sn-2sn-2sn-2sn-3sn-3sn-3sn-4sn-4sn-4...) where sn-1 is the last letter, sn-2 is the second to last letter, sn-3 is the third to last letter, etc.
    5. Method 5: Return and print in main() a String which counts down by 1 for X, and deletes one letter for each recursion for S, until X becomes 1, or until S has only 1 letter (X S, X-1 S1 letter deleted, X-2 S2 letters deleted, X-3 S3 letters deleted...)

Similar Threads

  1. Replies: 4
    Last Post: 11-27-2008, 07:09 AM
  2. strange Error message
    By little_polarbear in forum New To Java
    Replies: 4
    Last Post: 08-25-2008, 11:45 PM
  3. Getting Exception message into a String
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-04-2008, 10:31 AM
  4. java error message
    By baileyr in forum New To Java
    Replies: 2
    Last Post: 01-23-2008, 04:47 AM
  5. error message on jsp
    By sandor in forum Web Frameworks
    Replies: 1
    Last Post: 04-11-2007, 02:10 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
  •