This works for me. When I put it in Wiley plus it says the following:
fail
Compiling 1 source file to /var/folders/v7/959xv8710n98qn1vwg1jhnxr0000gn/T/asnmt1053218usr3628611-submit-1365039510034
/var/folders/v7/959xv8710n98qn1vwg1jhnxr0000gn/T/asnmt1053218usr3628611-submit-1365039510034/MiddleTest.java:3: error: class getMiddle is public, should be declared in a file named getMiddle.java
public class getMiddle
^
1 error
/var/folders/v7/959xv8710n98qn1vwg1jhnxr0000gn/T/codecomp9144166310241157728.xml:152: Compile failed; see the compiler error output for details.
import java.util.Scanner;
public class getMiddle
{
/**
Gets the middle character or character pair from this
string
when possible.
@param str a string
@return the middle character (if the string length is odd) or
the middle two characters (if it is even), or the empty string if str is
empty.*/
public static String getMiddle(String str)
{
if ((str.length() % 2) == 0)
{
//Even length
if (str.length() > 2)
{
return str.substring( str.length() / 2 - 1, str.length() / 2 +1);
}
}
//Odd length
return str.substring(str.length() / 2, str.length() / 2 + 1 );
}
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.print("Enter a string: ");
String str = in.next();
System.out.print(getMiddle(str));
}
}