aspose file tools*
The moose likes Beginning Java and the fly likes Get middle initial Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Get middle initial " Watch "Get middle initial " New topic
Author

Get middle initial

Rick Fal
Greenhorn

Joined: Feb 19, 2013
Posts: 3
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));

}

}
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18887
    
    8

error: class getMiddle is public, should be declared in a file named getMiddle.java


I don't know what "put it in Wiley" means, but this message is telling you to put that code in a file named "getMiddle.java". So you should do that.
Rick Fal
Greenhorn

Joined: Feb 19, 2013
Posts: 3
I am sorry I was unclear what I meant. Wiley Plus is where I put the program to be checked and graded. When I run the program it works. When I submit it there is an error. I am not sure why?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18887
    
    8

Sorry, I don't know anything about this Wiley Plus thing. Except that it seems to think that you put your code into a file called MiddleTest.java -- does that ring any bells?
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Get middle initial