I have a feeling that the answer's right under my nose, but my n00b-ness to Java has me chasing my tail. First part, I'm suppose to ask a user to in put two strings, compare them, and state the number of characters in the first string. Second part, I'm suppose to ask the user to enter a position bearing in mind that the first character starts at zero, and then the code's suppose to state the character that is at that position.
I think my code for the first part is pretty clean, but what I need help on is the second part. I've written the method at the bottom, but now I don't know how to call it. I keep getting an error message in the compiler. I also haven't gotten a chance to actually test the method because of that error, so if you see anything wrong with that, any help would be much appreciated. Thanks in advance!
import java.util.Scanner;
public class StringCode1
{
public static void main(String[] args)
{
String string1, string2;
int pos;
Scanner stdin = new Scanner(System.in);
System.out.print("Enter first string: ");
string1 = stdin.next();
System.out.print("Enter second string: ");
string2 = stdin.next();
if (string1.compareTo(string2) > 0)
{
System.out.println(string1 + " is less than " + string2);
}
else if (string1.compareTo(string2) == 0)
{
System.out.println(string1 + " is equal to " + string2);
}
else if (string1.compareTo(string2) < 0)
{
System.out.println(string1 + " is greater than " + string2);
}
System.out.print("Number of characters in " + string1 + " is " );
System.out.println(string1.length() );
String = showChar();
}
public static char showChar(String string1, int pos)
{
Scanner stdin = new Scanner(System.in);
System.out.println("Enter position noting first character is at 0: ");
string1 = stdin.nextLine();
pos = stdin.nextInt();
System.out.print("Character at position" + pos + "in " + string1);
System.out.print("is: ");
System.out.println(string1.charAt(pos));
}
}