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 4 of 4

Thread: Beginner Java Method

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Beginner Java Method

    Beginner warm up problem that I can't figure out why isn't working. Thanks for any assistance in advance. (Note: Trying to use substring method not charAt() method.)

    ----------------------

    Given a string, return a string made of the first 2 chars (if present), however include first char only if it is 'o' and include the second only if it is 'z', so "ozymandias" yields "oz".

    public String startOz(String str) {
     
    String r = ""; 
     
      if (str.substring(0).equals("o")) 
      r = str.substring(0);  
     
      if (str.substring(1).equals("z")) 
      r = str.substring(1); 
     
      return r;
    }

    Expected Run
    startOz("ozymandias") → "oz" "" X
    startOz("bzoo") → "z" "" X
    startOz("oxx") → "o" "" X
    startOz("oz") → "oz" "z" X
    startOz("ounce") → "o" "" X
    startOz("o") → "o" "o" OK
    startOz("abc") → "" "" OK
    startOz("") → "" "Exception:java.lang.StringIndexOutOfBoundsExcepti on: String index out of range: -1 (line number:8)" X
    startOz("zoo") → "" "" OK
    startOz("aztec") → "z" "" X
    startOz("zzzz") → "z" "" X
    startOz("oznic") → "oz" "" X

    CodingBat Java Warmup-1 startOz


  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: Beginner Java Method

    "Exception:java.lang.StringIndexOutOfBoundsExc epti on: String index out of range: -1
    Code should always test that the data is what is expected. In this case the code expected a String with at least 1 character. The String class's length method would help here.

    If you can not understand what the code is doing, add a println() statement before the if statement that prints out the String returned by the substring method so you can see what the computer sees when it executes the if statement.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2014
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Beginner Java Method

    If it possible to not use the length method? I figured I could get it working with the substring method, but I am having problems.

  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: Beginner Java Method

    If it possible to not use the length method?
    Well written code checks for error conditions and acts to prevent exceptions.
    If your instructor told you not to do that, it is not a good practice to skip tests like that but you might have to follow your instructor's wishes to get the grade.

    Did you miss the second part of my last post?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Mutator method at Beginner level
    By Huntea in forum Java Theory & Questions
    Replies: 3
    Last Post: January 30th, 2014, 07:47 AM
  2. Help with Beginner JAVA method!
    By starlest in forum Object Oriented Programming
    Replies: 1
    Last Post: May 13th, 2013, 10:14 PM
  3. [SOLVED] How to create a Java generic method, similar to a C++ template method?
    By Sharmeen in forum Object Oriented Programming
    Replies: 3
    Last Post: October 18th, 2012, 02:33 AM
  4. Help with beginner mutator method
    By Dysun in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 8th, 2012, 06:32 PM
  5. Beginner method call problem
    By Lasda in forum Java Theory & Questions
    Replies: 2
    Last Post: May 10th, 2011, 03:31 PM