Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: The compiler apparently doesn't like my method definition.

  1. #1
    Junior Member
    Join Date
    Sep 2021
    Location
    Midwest US
    Posts
    15
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default The compiler apparently doesn't like my method definition.

    Here I am again. In Java SE 16, I wrote
    /* Return true if testWord is an abbreviation of fullWord.  Case must match.         */
        public static boolean isAbbrev(String testWord, String fullWord, int minLength)
         {if (minLength         <= testWord.length() &&
              fullWord.length() >= testWord.length() &&
              fullWord.startsWith(testWord)
             )
            return true;
     
          return false;
         }
    but the compiler says,
    $ javac Jute.java
    Jute.java:228: error: class, interface, enum, or record expected
        public static boolean abbrev(String testWord, String fullWord, int minLength)
                      ^
    Jute.java:235: error: class, interface, enum, or record expected
          return false;
          ^
    Jute.java:236: error: class, interface, enum, or record expected
         }
         ^
    I reviewed the Oracle Tutorial pages in https://docs.oracle.com/javase/tutor...aOO/index.html but I can't see what I'm doing wrong. Do the public, static, etc. attributes have to specified in a particular order or something?

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: The compiler apparently doesn't like my method definition.

    In the proper location the code compiles without errors for me.
    The proper location is inside of a class's enclosing {}s and outside of any method's enclosing {}

    I'd guess that the method is outside of any class.

    Can you post the surrounding code?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Sep 2021
    Location
    Midwest US
    Posts
    15
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: The compiler apparently doesn't like my method definition.

    Quote Originally Posted by Norm View Post
    In the proper location the code compiles without errors for me.
    The proper location is inside of a class's enclosing {}s and outside of any method's enclosing {}

    I'd guess that the method is outside of any class.

    Can you post the surrounding code?
    I looked at that, and as far as I can see it's inside my class. If there's a brace out of place, I can't see it.
     
      import java.io.File;
      import java.util.*;
     
      public class Jute
       {public static void main(String[] args)
          throws UnrecognizedOPtion,
                 InvalidLinend
         {String                     token[];
          String                  subToken[];
          String                   arguments   = java.util.Arrays.toString(args);
          String                    language   = new String("en");
          String                     country   = new String("US");
          Locale               currentLocale   = new Locale(language, country);
          ResourceBundle            messages   = ResourceBundle.getBundle("MessagesBundle", currentLocale);
          Map<String, String> globalSettings   = new HashMap<String, String>();
     
          final static String DISPLAY_MESSAGES = "displayMessages";
          final static String DISPLAY_WINDOW   = "displayWindow";
          final static String LOCK_FILE        = "lockFile";
          final static String MAKE_DIFF        = "makeDiff";
          final static String RUN_PROFILE      = "runProfile";
          final static String PROFILE_NAME     = "profileName";
          final static String DIFF_TYPE        = "diffType";
          final static String LINEND           = "linend";
          final static String WIDTH            = "width";
     
          globalSettings.put(DISPLAY_MESSAGES, "true");
          globalSettings.put(DISPLAY_WINDOW,   "true");
          globalSettings.put(LOCK_FILE,        "false");
          globalSettings.put(MAKE_DIFF,        "false");
          globalSettings.put(RUN_PROFILE,      "true");
          globalSettings.put(PROFILE_NAME,     "profile");
          globalSettings.put(DIFF_TYPE,        "diff");
          globalSettings.put(LINEND,           "nl");       // TODO: make this dependent on the hosting OS.
          globalSettings.put(WIDTH,            "1000");
     
          while (arguments.length > 0)
            token = arguments;
     
            if (arguments.startsWith("-") )
             {token = token.split("[\s=]", 1);
     
              switch (token[0].toLowerCase() )
               {case isAbbrevIgnoreCase(token[0], "-Diff",      2):
                  globalSettings.put("makeDiff", "true");
                  break;
     
                case isAbbrevIgnoreCase(token[0], "-NODiff",    4):
                  break;
     
                case isAbbrevIgnoreCase(token[0], "-Git",       2):
                  globalSettings.put("diffType", "git");
                  break;
     
                case isAbbrevIgnoreCase(token[0], "-Svn",       1):
                  globalSettings.put("diffType", "svn");
                  break;
     
                case isAbbrevIgnoreCase(token[0], "-Help",      2):
                  /* Display usage info. and quit. */
                  break;
     
                case isAbbrevIgnoreCase(token[0], "-Linend",    2):
                 {subToken = token[1].split("\s", 2);
     
                  if (subToken[0].matches("^\\p{Digit}+$") )              /* integer */
                    globalSettings.put("linend", token[0]);
     
                  else if (subToken[0].matches("^(\\p{Punct}).*\1") )     /* delimitedSstring */
                   {subToken = token[1].split(token[1].charAt(1), 2);
                    globalSettings.put("linend", subToken[1]);
                   }
                  else if (subToken[0].compareToIgnoreCase("nl") )
                    globalSettings.put("linend", "nl");
     
                  else if (subToken[0].compareToIgnoreCase("crnl") )
                    globalSettings.put("linend", "crnl");
     
                  else if (subToken[0].compareToIgnoreCase("crlf") )
                    globalSettings.put("linend", "crlf");
     
                  else
                    throw new InvalidLinend(subToken[0]);
     
                  break;
                 }
     
                case isAbbrevIgnoreCase(token[0], "-LOck",      3):
                  globalSettings.put("lockFile", "true");
                  break;
     
                case isAbbrevIgnoreCase(token[0], "-NOLock",    4):
                  break;
     
                case isAbbrevIgnoreCase(token[0], "-Msg",       2):
                  break;
     
                case isAbbrevIgnoreCase(token[0], "-NOMsg",     4):
                  globalSettings.put("displayMessages", "false");
                  break;
     
                case isAbbrevIgnoreCase(token[0], "-Profile",   2):
                  subToken = token[1].split("\s", 2);
                  globalSettings.put("profileName", subToken[1]);
                  break;
     
                case isAbbrevIgnoreCase(token[0], "-NOProfile", 4):
                  globalSettings.put("runProfile", "false");
                  break;
     
                case isAbbrevIgnoreCase(token[0], "-WINdow",    4):
                  break;
     
                case isAbbrevIgnoreCase(token[0], "-NOWindow",  4):
                  globalSettings.put("displayWindow", "false");
                  break;
     
                case isAbbrevIgnoreCase(token[0], "-Width",     2):
                  subToken = token[1].split("\s", 2);
                  globalSettings.put("width", subToken[1]);
                  break;
     
                default:
                  throw new UnrecognizedOPtion(token[0]);
               }
             }
            arguments = token[1];
           }
     
    /*    else                                      */
    /*     {for (int A = 0; A < args.length; A++)   */
    /*        frame.addDocument(new File(args[A])); */
    /*                                              */
    /*      if (displayWindow)                      */
    /*       {MainFrame frame = new MainFrame();    */
    /*        frame.insertNewDocument();            */
    /*       }                                      */
    /*     }                                        */
    /*    frame.setVisible(true);                   */
         }
     
    /* ================================================================================= */
    /* Return true if testWord is an abbreviation of fullWord.  Case must match.         */
        public static boolean isAbbrev(String testWord, String fullWord, int minLength)
         {if (minLength         <= testWord.length() &&
              fullWord.length() >= testWord.length() &&
              fullWord.startsWith(testWord)
             )
            return true;
     
          return false;
         }
     
    /* --------------------------------------------------------------------------------- */
    /* Return true if testWord is an abbreviation of fullWord.  Case need not match.     */
        public static boolean isAbbrevIgnoreCase(String testWord, String fullWord, int minLength)
         {String tw = testWord.toLowerCase();
          String fw = fullWord.toLowerCase();
     
          if (minLength   <= tw.length() &&
              fw.length() >= tw.length() &&
              fw.startsWith(tw)
             )
            return true;
     
          return false;
         }
     
    /* ================================================================================= */
    /* Return fullWord if testWord is an abbreviation of fullWord, else null.            */
    /* Case must match.                                                                  */
        public static String isAbbrev(String testWord, String fullWord, int minLength)
         {if (minLength         <= testWord.length() &&
              fullWord.length() >= testWord.length() &&
              fullWord.startsWith(testWord)
             )
            return fullWord;
     
          return null;
         }
     
    /* --------------------------------------------------------------------------------- */
    /* Return fullWord if testWord is an abbreviation of fullWord; else null.            */
    /* Case need not match.                                                              */
        public static String isAbbrevIgnoreCase(String testWord, String fullWord, int minLength)
         {String tw = testWord;
          String fw = fullWord;
     
          if (minLength   <= tw.length() &&
              fw.length() >= tw.length() &&
              fw.startsWith(tw)
             )
            return fullWord;
     
          return null;
         }
     
    /* ================================================================================= */
    /* Return the number of space-delimited words in phrase.                             */
        public static int words(String phrase)
         {Pattern pattern  = Pattern.compile("\s");
          Matcher matcher  = pattern.matcher(phrase);
          int     numWords = 0;
     
          while (matcher.find())
            numWords += 1;
          return numWords;
         }
     
    /* --------------------------------------------------------------------------------- */
    /* Return the Wth word of phrase.                                                    */
        public static String word(String phrase, int wordNum)
         {String[] part;
     
          part = phrase.split("\s", wordNum);
          return part[wordNum - 1];
         }
     
    /* --------------------------------------------------------------------------------- */
    /* Return N words of phrase starting with the Wth word.                              */
        public static String subWord(String phrase, int wordNum, int numWords)
         {String[] parts;
     
          parts = phrase.split("\s");
          return copyOfRange(parts, wordNum - 1, numWords);
         }
     
    /* ================================================================================= */
     
        public class UnrecognizedOption extends Exception
         {System.err.println(messages.getString("Jute001E");
          System.err.println(messages.getString("Jute003I");
          exit(1);
         }
     
    /* --------------------------------------------------------------------------------- */
     
        public class InvalidLinend extends Exception
         {System.err.println(messages.getString("Jute002E");
          System.err.println(messages.getString("Jute003I");
          exit(1);
         }
     
    /* ================================================================================= */
       }
    (All of the methods below the main() get the same error. Most of the other statements in them get it too.)

    --- Update ---

    But, a { was missing at line 122 (token = arguments, so once again, my face is red.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: The compiler apparently doesn't like my method definition.

    Coding anything on the same line as a { makes it harder to see the {. Coding on a separate lines is better:
      {public static void main(String[] args)   //<<<<< HARD to see {
     
      {
          public static void main(String[] args)

    Also indent code within {}s
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Sep 2021
    Location
    Midwest US
    Posts
    15
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: The compiler apparently doesn't like my method definition.

    Yes. I have come to that conclusion, and will put my {s on their own lines in future.

    --- Update ---

    And now the "Solved:" widget has disappeared from all levels of my threads. :-(
    Hah! But at the top of the thread there's a tool bar, and at the bottom of the "Thread Tools" tab there, is "Mark this thread as solved".
    Last edited by jlturriff; September 24th, 2021 at 09:43 AM.

Similar Threads

  1. Replies: 0
    Last Post: September 26th, 2017, 05:08 PM
  2. My pseudocode is apparently wrong, and I can't figure out why...
    By Eclecstatic in forum What's Wrong With My Code?
    Replies: 0
    Last Post: September 12th, 2012, 12:17 AM
  3. compiler error regarding method call
    By dan1967 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: December 18th, 2011, 01:42 PM
  4. Simple code apparently too hard for me...
    By Avopeb in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 29th, 2011, 02:35 PM
  5. GUI doesn't work when synchronized method is run
    By worwhite in forum AWT / Java Swing
    Replies: 1
    Last Post: August 1st, 2011, 07:59 AM