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

Thread: Creating method to print array of integers

  1. #1
    Junior Member
    Join Date
    Apr 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Creating method to print array of integers

    I am trying to create a method that takes an array of integers and prints it out using System.out.print. I'm having trouble creating the right way to print it out since I cannot find a way to convert the int array to a string to print it out.

     
    public static String printArray(int[] num){
     
    	for (int i=0; i<num.length;i++){
    		String msg = num[i];
    	}
    	return System.out.print(msg + " ");
    	}


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Creating method to print array of integers

    a way to convert the int array to a string
    You can't get the contents of an array without accessing each of its elements one at a time.
    The String class and the Integer class both have methods that will convert an int value to a String.
    Take a look at the API doc to see what they are.

    The compiler will automatically convert an int to a String if the code uses the + operator between a String and an int. For example: (""+123)
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Help creating print preview of JTable
    By djastral232 in forum Object Oriented Programming
    Replies: 0
    Last Post: April 11th, 2014, 04:05 AM
  2. New Method array to String or just print as Array?
    By Necator in forum What's Wrong With My Code?
    Replies: 19
    Last Post: February 20th, 2014, 04:09 PM
  3. Need help with creating method for Array.
    By Rain_Maker in forum What's Wrong With My Code?
    Replies: 30
    Last Post: March 24th, 2013, 06:12 PM
  4. Calling a print method from another class (printing array)
    By Kaldanis in forum Object Oriented Programming
    Replies: 7
    Last Post: November 25th, 2011, 01:32 PM
  5. Replies: 2
    Last Post: April 21st, 2011, 10:29 AM