• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Get middle initial

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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));

}

}
 
Marshal
Posts: 28176
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 28176
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
I just had the craziest dream. This tiny ad was in it.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic