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

Thread: Need a non void method to return the first three characters of a string

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need a non void method to return the first three characters of a string

    Trying to get a non void method to return the first 3 letters of a word but get the following error corresponding to the error in the attachment

    "Type mismatch: cannot convert from int to string"
    Attached Images Attached Images


  2. #2
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: Need a non void method to return the first three characters of a string

    Can I suggest looking at this page and reading some of the methods etc.

    String (Java 2 Platform SE v1.4.2)

    Chris

  3. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need a non void method to return the first three characters of a string

    Thanks Freaky Chris.

    That link lead me to solving my problem.

    In case anyone was curious to the solution:

    //Programming Projects 9.1
    public class PP9_1 
    {
    	//Return the first three letters of the String Kris
    	public static String firstThreeInitials (String name)
    	{	
    		String c = name.substring(0,3);
    		return c;
    	}
     
    	public static void main(String[] args) 
    	{
    		String firstThreeInitials = firstThreeInitials("Kris");
    		System.out.println(firstThreeInitials);
    	}
     
    }
    Last edited by Freaky Chris; November 9th, 2011 at 07:41 AM. Reason: Added syntax

  4. #4
    Member
    Join Date
    Aug 2011
    Posts
    55
    Thanks
    5
    Thanked 3 Times in 3 Posts

    Default Re: Need a non void method to return the first three characters of a string

    You could pass them by reference instead.

  5. #5
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Need a non void method to return the first three characters of a string

    Java is pass-by-value!
    Improving the world one idiot at a time!

Similar Threads

  1. So Lost with void method
    By tripline in forum What's Wrong With My Code?
    Replies: 7
    Last Post: October 27th, 2011, 01:45 PM
  2. Replies: 2
    Last Post: August 3rd, 2011, 11:13 AM
  3. Reverse character using void method
    By bgwilf in forum Algorithms & Recursion
    Replies: 2
    Last Post: December 8th, 2010, 07:25 AM
  4. How to check whether the string contains only the specified characters ????
    By j_kathiresan in forum Java Theory & Questions
    Replies: 3
    Last Post: April 30th, 2010, 08:49 AM
  5. Calling a void method into a static void main within same class
    By sketch_flygirl in forum Object Oriented Programming
    Replies: 3
    Last Post: November 15th, 2009, 05:24 PM