CodeGym
促销活动
CodeGym University
学习
课程
任务
调查和小测验
游戏
帮助
提醒时间表
社区
用户
论坛
聊天
文章
成功故事
活动
评论
订阅服务
浅色主题
问题
  • 评论
  • 关于我们
开始
开始学习
立即开始学习
  • 全部问题
AKUN
第 25 级
Chongqing
  • 23.09.2021
  • 297次阅读
  • 2个评论

I don't know why my solution can't pass the test.

有关 任务的问题 Find a substring
Java 多线程,  第 2 级,  课程 5
正在讨论
The getPartOfString method must return the substring that begins with the character after the first space and ends with the end of the word that follows the 4th space.

Example:
"CodeGym is the best place to learn Java."

Result:
"is the best place"

Example:
"Amigo and Diego are best friends!"

Result:
"and Diego are best"

If the input data is invalid, throw a StringTooShortException (create an exception).

要求:
  • The StringTooShortException class must be a child of the RuntimeException class.
  • The getPartOfString method must have a String parameter.
  • If invalid data is passed to the getPartOfString method, a StringTooShortException should be thrown.
  • The getPartOfString method must return the substring that begins with the character after the first space and ends with the word that follows the 4th space.
package com.codegym.task.task22.task2202; /* Find a substring */ import java.util.ArrayList; public class Solution { public static void main(String[] args) { System.out.println(getPartOfString("CodeGym is the best place to learn Java.")); } public static String getPartOfString(String string) { try { ArrayList<String> list = new ArrayList<>(5); String totalString = string; for (int i =0;i<4;i++) { totalString = totalString.substring(totalString.indexOf(' ')+1); list.add(totalString.substring(0,totalString.indexOf(' '))); } return list.get(0)+" "+list.get(1)+" "+list.get(2)+" "+list.get(3); } catch (Exception e) { throw new StringTooShortException(); } } public static class StringTooShortException extends RuntimeException{ } }
0
评论 (2)
  • 受欢迎
  • 新
  • 旧
你必须先登录才能发表评论
Lisa
级别 41
23 九月 2021, 15:36
Test, test, test a lot, especially test the borderline cases like:
System.out.println(getPartOfString("CodeGym is the best place"));
That's what CGs doing, too... yeah, yeah 😜🤪
+1
AKUN
级别 25 ,Chongqing,China
25 九月 2021, 00:42
Thank you very much.
0
了解
  • 注册
  • Java 课程
  • 任务帮助
  • 定价
  • 游戏项目
  • Java 语法
社区
  • 用户
  • 文章
  • 论坛
  • 聊天
  • 成功故事
  • 活动
  • Affiliate Program
公司
  • 关于我们
  • 联系方式
  • 评论
  • 新闻发布室
  • CodeGym 教育
  • 常见问题
  • 支持
CodeGym CodeGym 是一个从零开始学习 Java 语言编程的在线课程。本课程是初学者掌握 Java 语言的绝佳方式。它包含 1200 多个可即时验证的任务,以及基本范围内的 Java 基础理论。为了帮助你在教育上取得成功,我们实现了一组激励功能:小测验、编码项目以及有关高效学习和 Java 语言开发人员职业方面的内容。
关注我们
界面语言
程序员不是天生的 © 2023 CodeGym
MastercardVisa
程序员不是天生的 © 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.