-1

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));

   }  
  }
Theodoros Chatzigiannakis
  • 28,773
  • 8
  • 68
  • 104
Devora
  • 7
  • 2
  • whats the error message? – LanternMike May 19 '13 at 12:01
  • 1
    Your code's indentation is "all over the place like a mad woman's knitting" ... as as my old science teacher would say (RIP AAC). If you want people to treat your code / Questions seriously, you need to pay attention to the style. – Stephen C May 19 '13 at 12:02

2 Answers2

1
String = showChar();

In the above line, you are trying to assign to a type, which is an invalid expression. Additionally, your method showChar requires two arguments and you're trying to call it with none.

One more error is that showChar has a return type of char but no return statement. Either make it a void method, or have it return something (presumably string1.charAt(pos)).

So, you'll have to find what the argument for showChar should be and then, if the value of showChar is supposed to return a char, assign it to a variable of its type, like so:

char c = showChar(string1, yourIndex);

Or, if it's going to be void:

showChar(string1, yourIndex);

...where yourIndex is the value you want to pass for the pos parameter in showCar.

Theodoros Chatzigiannakis
  • 28,773
  • 8
  • 68
  • 104
0

Modify your showChar function as below:

public static void showChar()
{
    Scanner stdin = new Scanner(System.in);

    System.out.println("Enter position noting first character is at 0: ");
    String string1 = stdin.nextLine();
    int pos = stdin.nextInt();

    System.out.print("Character at position" + pos + "in " + string1); 
    System.out.print("is: "); 
    System.out.println(string1.charAt(pos));

}  

And call it with showChar();

Benny Ng
  • 328
  • 2
  • 6