How do write a method that accepts two arguments: reference to a String object and a

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jlock784
    New Member
    • Feb 2010
    • 5

    How do write a method that accepts two arguments: reference to a String object and a

    Write a method named showChar. The method should accept two arguments: a reference to a String object and an integer. The integer argument is a character position within the String, with the first character being at position 0. When the method executes, it should display the character at that character position.

    This is what I have so far:

    public class ShowCharMethod
    {
    public static void showChar
    char letter;
    String name = "showChar";
    letter = name.charAt();


    public static void main(String[] args)
    {
    String testString = "New York";

    showChar(testSt ring, 2);
    }

    }
    Last edited by jlock784; Mar 22 '10, 11:15 PM. Reason: left out a sentence
  • anurag275125
    New Member
    • Aug 2009
    • 79

    #2
    You should write the code something like that...

    Code:
    public class ShowCharMethod
    {
    	public static void main(String args[])
    	{
    		String testString="New York";
    		showChar(testString,2);
    	}
    	static void showChar(String string, int position)
    	{
    		char letter=string.charAt(position);
    		System.out.println("Character at position "+position+" : "+letter);
    	}
    }

    Comment

    Working...