简单的Java异常问题

whu*_*739 2 java exception

嘿,这只是课堂上的一个简单的练习,我决定去做一个例外.根据书的输入,这个问题应该采用以下格式:2009年4月19日我想要的是什么做我的例外是确保用户(无论谁评级)遵循这些参数,以便我的程序工作.这看起来不错吗?我可以做得更好吗?

编辑:感谢您的及时答复!我知道我有很多东西需要学习,但希望有一天我能够回答这里的问题.干杯

import jpb.*;

public class ConvertDate {
    public static void main(String[] args) {
        try{
        SimpleIO.prompt("Enter date to be converted: ");
        String bam = SimpleIO.readLine();
        bam = bam.trim().toLowerCase();
        //empty space is taken off both ends and the entirety is put in lower case
        int index1 = bam.indexOf(" ");
        int index2 = bam.lastIndexOf(" ");
        int index3 = bam.indexOf(",");
        /*critical points in the original string are located using indexing to divide and conquer
        in the next step*/
        String month = bam.substring(0,index1);
        String up = month.substring(0, 1).toUpperCase();
        String rest = month.substring(1,index1);
        String day = bam.substring(index1, index3).trim();
        String year = bam.substring(index2+1);
        //now all the pieces are labeled and in their correct cases
        System.out.println("Converted date: " +day +" " +up +rest +" " +year);
        //everything comes together so perfectly
        } catch(StringIndexOutOfBoundsException e){
            System.out.println("best type that in like the book does on page 125...");}
        }
}
Run Code Online (Sandbox Code Playgroud)

duf*_*ymo 5

这里有一些想法.这些只是我的意见,所以如果你愿意,可以带上一粒盐或完全忽略:

  1. 我不知道该import语句在做什么,但是可以在不引入对库的依赖的情况下完成所有这些操作.您应该了解依赖关系的影响.
  2. 讨厌你添加的那种评论.我知道学生和教授都喜欢他们,但作为一名专业人士,我发现他们的信息量不如代码本身,只会增加混乱.
  3. 我不会将所有这些逻辑放入主方法中.我将它封装到类或静态方法中,以便我可以重用它.这是一个无用的学生练习.
  4. 你测试了"蓝天"的快乐路径,一切都在运行.我建议查看类似JUnit的内容,并开始考虑边缘和异常情况.尝试更多输入测试以破坏您的方法.它会改善这种方式.您当前的方法可以保证您的代码能够与其他易于思考的案例保持联系(例如,传入不是一个月 - 一年的字符串).

以下是我可能会写同样的事情:

import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.text.ParseException;

public class ConvertDate 
{
    private static final DateFormat DEFAULT_FORMAT;

    static
    {
        DEFAULT_FORMAT = new SimpleDateFormat("MM-dd-yyyy");
        DEFAULT_FORMAT.setLenient(false);
    }

    public static void main(String[] args) 
    {
        for (String dateString : args)
        {
            try
            {
                Date date = DEFAULT_FORMAT.parse(dateString);
                System.out.println(date);
            }
            catch (ParseException e)
            {
                System.out.println("invalid date string: " + dateString);
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

  • _我讨厌这种评论_的确如此.@ user463372,写下评论告诉**为什么**你正在做某事,而不是**你在做什么**.我们可以阅读代码,我们知道你在做什么.但是,对你为什么这样做有一点了解可以减少光线. (2认同)

Vin*_*nie 5

通常使用异常作为程序逻辑的一部分是不受欢迎的.你的逻辑应该是:

   if(The Input is well formed){
       parse the date
   }else{
       Tell the user that the input is wrong
   }
Run Code Online (Sandbox Code Playgroud)

但你的计划是:

    try{
         parse the date

     }catch(){
         tell the user the input is wrong
     }
Run Code Online (Sandbox Code Playgroud)

要确定输入是否格式良好,您可以测试它的长度,获取月份子串,并测试长度,获取日子串并测试它是否为整数等.

程序永远不应抛出StringIndexOutOfBoundsException,因为在使用substring方法之前,您可以检查这一点.