CodeGym
Promotion
CodeGym University
Learning
Course
Tasks
Surveys & Quizzes
Games
Help
Schedule
Community
Users
Forum
Chat
Articles
Success stories
Activity
Reviews
Subscriptions
Light theme
Question
  • Reviews
  • About us
Start
Start learning
Start learning now
  • All questions
hidden #10625598
Level 23
  • 26.07.2020
  • 390views
  • 1comment

why is this wrong?

Question about the task Between tabs
Java Multithreading,  Level 2,  Lesson 5
Under discussion


The getPartOfString method must return the substring between the first and the second tab characters.
If the input data is invalid, throw a StringTooShortException.
Don't change the StringTooShortException class.

Requirements:
  • The StringTooShortException class must be a child of the Exception class.
  • The getPartOfString method must have a String parameter.
  • If the string passed to the getPartOfString method contains less than 2 tab characters, then a StringTooShortException should be thrown.
  • The getPartOfString method must return the substring between the first and the second tab characters.
package com.codegym.task.task22.task2203; /* Between tabs */ public class Solution { public static String getPartOfString(String string) throws Exception { int a, b, c; String str = null; if(string == null){ throw new StringTooShortException(); } else{ a = string.indexOf("\t"); b = string.lastIndexOf("\t"); } if(a == b) { throw new StringTooShortException(); }else{ c = string.indexOf("\t", a+1); str = string.substring(a, c); } return str.trim(); } public static class StringTooShortException extends Exception { } public static void main(String[] args) throws Exception { System.out.println(getPartOfString("\tCodeGym is the best place \tto learn Java\t.")); } }
0
Comments (1)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Dave Andrea
Level 41 , Canada
10 September 2020, 14:53
Probably because the characters immediately after 'a' and before 'c' could also be whitespace characters. Therefore the trim method would eliminate them too, not just the tabs.
c = string.indexOf("\t", a+1);
str = string.substring(a, c); // try (a+1, c) for the parameters.
0
Learn
  • Registration
  • Java Course
  • Help with Tasks
  • Pricing
  • Game Projects
  • Java Syntax
Community
  • Users
  • Articles
  • Forum
  • Chat
  • Success Stories
  • Activity
  • Affiliate Program
Company
  • About us
  • Contacts
  • Reviews
  • Press Room
  • CodeGym for EDU
  • FAQ
  • Support
CodeGym CodeGym is an online course for learning Java programming from scratch. This course is a perfect way to master Java for beginners. It contains 1200+ tasks with instant verification and an essential scope of Java fundamentals theory. To help you succeed in education, we’ve implemented a set of motivational features: quizzes, coding projects, content about efficient learning and Java developer’s career.
Follow us
Interface language
Programmers Are Made, Not Born © 2023 CodeGym
MastercardVisa
Programmers Are Made, Not Born © 2023 CodeGym
This website uses cookies to provide you with personalized service. By using this website, you agree to our use of cookies. If you require more details, please read our Terms and Policy.