Results 1 to 8 of 8

Thread: Print method

  1. #1
    phantomcraft is offline Member
    Join Date
    Feb 2013
    Location
    The Netherlands
    Posts
    17
    Rep Power
    0

    Default Print method

    Hey i'm working with methods atm and I can't figure out what is going wrong here.
    [Java] Error java - Pastebin.com

    I want to be able to just call print("Hello, world!"); anywhere i want instead of the long version
    Last edited by phantomcraft; 03-01-2013 at 02:07 PM.

  2. #2
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    947
    Rep Power
    2

    Default Re: Print method

    Original code:
    Java Code:
    public static void print (String[s] args){
       System.out.println(s);
    }
    The array initializer should be empty:
    Java Code:
    public static void print (String[] args){
       System.out.println(args[0]);
    }
    Please don't use pastebin to show code. Just use [code][/code] tags to post code.

  3. #3
    phantomcraft is offline Member
    Join Date
    Feb 2013
    Location
    The Netherlands
    Posts
    17
    Rep Power
    0

    Default Re: Print method

    Thanks but I can't print my int's with it

  4. #4
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    947
    Rep Power
    2

    Default Re: Print method

    So, here's what you do next:
    1) Copy your Java code to the clipboard
    2) Click "Reply to thread" under this message
    3) Type: [code]
    4) Paste your code from the clipboard
    5) Type: [/code]
    6) Describe what your actual output is
    7) Describe what your expected output is
    8) If you have any exceptions, please feel free to include those in your reply as well

    Remember we are programmers, not magicians with a crystal ball that read your mind...

  5. #5
    phantomcraft is offline Member
    Join Date
    Feb 2013
    Location
    The Netherlands
    Posts
    17
    Rep Power
    0

    Default Re: Print method

    Java Code:
    package test;
    
    public class Test {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            int a = 3;
            int b = 5;
            int c = 10;
            print(c);
        }
        
        public static void print (String[s] args){
            System.out.println(args[0]);
        }
    }
    Output

    Java Code:
    run:
    Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - Erroneous sym type: print
    	at test.Test.main(Test.java:12)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 0 seconds)
    What i expected:

    Java Code:
    run:
    10
    BUILD SUCCESSFUL (total time: 0 seconds)

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,036
    Rep Power
    20

    Default Re: Print method

    Well, first thing is...don't run code that doesn't compile.

    In this case it doesn't compile because your print method accepts Strings, not ints.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    947
    Rep Power
    2

    Default Re: Print method

    Again, array initializers don't take parameters (at least not in this way).

    Second, think about what information you want to pass to the method print. Is that a String array? No, it's an int. So make sure you actually accept an int:
    Java Code:
    public static void print (int myNumber){
            System.out.println( myNumber);
    }

  8. #8
    phantomcraft is offline Member
    Join Date
    Feb 2013
    Location
    The Netherlands
    Posts
    17
    Rep Power
    0

    Default Re: Print method

    Java Code:
        static void print (String s){
            System.out.println(s);
        }
    }
    Thanks anyways

Similar Threads

  1. Replies: 4
    Last Post: 04-27-2012, 09:43 AM
  2. Javax Print Attribute for Selection Print Range
    By rsawatzky in forum AWT / Swing
    Replies: 0
    Last Post: 04-26-2012, 12:14 AM
  3. using the toString method to print an array
    By Wolfie in forum New To Java
    Replies: 1
    Last Post: 03-06-2011, 10:41 PM
  4. print out error method for wrong args.
    By Implode in forum New To Java
    Replies: 9
    Last Post: 09-20-2009, 12:46 PM
  5. Print the text file and print preview them
    By Java Tip in forum java.awt
    Replies: 0
    Last Post: 06-22-2008, 11:04 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •