线程“main”中的 Java 异常 java.lang.StringIndexOutOfBoundsException 错误

Posted

技术标签:

【中文标题】线程“main”中的 Java 异常 java.lang.StringIndexOutOfBoundsException 错误【英文标题】:Java Exception in thread "main" java.lang.StringIndexOutOfBoundsException Error 【发布时间】:2014-02-10 06:06:28 【问题描述】:

我正在编写一个程序,它会提示生日(例如 02 14),然后以文字形式打印出您输入的日期以及该日期的星座。这是我的代码。

public class Horoscope

public String getBirthday (String birth)

    String month = birth.substring(0, 2);
    String day = birth.substring(3, 5); 

    if(month.equals("01"))
    
        month = "January ";
    //end of January

    else if (month.equals("02"))
    
        month = "Feburary ";
    //end of Feb.


    else if (month.equals("03"))
    
        month = "March ";
    //end of March

    else if (month.equals("04"))
    
        month = "April ";
    //end of April

    else if (month.equals("05"))
    
        month = "May ";
    //end of may

    else if (month.equals("06"))
    
        month = "June ";
    //end of June

    else if (month.equals("07"))
    
        month = "July ";
    //end of July

    else if (month.equals("08"))
    
        month = "August ";
    //end of August

    else if (month.equals("09"))
    
        month = "September ";
    //end of Sept

    else if (month.equals("10"))
    
        month = "October ";
    //end of Oct

    else if (month.equals("11"))
    
        month = "November ";
    //end of nov

    else if (month.equals("12"))
    
        month = "December ";
    //end of Dec

    else
    
        System.out.println("Invaid month type.");
    //end of invalid



    if(day.equals("01"))
    
        day = "1st";            
    //end of 1

    else if (day.equals("02"))
    
        day = "2nd";
    //end of 2

    else if(day.equals("03"))
    
        day = "3rd";
    

    else if(day.equals("04"))
    
        day = "4th";
    //end of 4

    else if(day.equals("05"))
    
        day = "5th";
    //end of 5th

    else if(day.equals("06"))
    
        day = "6th";
    
    else if(day.equals("07"))
    
        day = "7th";
    
    else if(day.equals("08"))
    
        day = "8th";
    
    else if(day.equals("09"))
    
        day = "9th";
    
    else if(day.equals("10"))
    
        day = "10th";
    //end of 10th
    else if(day.equals("11"))
    
        day = "11th";
    
    else if(day.equals("12"))
    
        day = "12th";
    
    else if(day.equals("13"))
    
        day = "13th";
    
    else if(day.equals("14"))
    
        day = "14th";
    
    else if(day.equals("15"))
    
        day = "15th";
    //end of 15
    else if(day.equals("16"))
    
        day = "16th";
    
    else if(day.equals("17"))
    
        day = "17th";
    
    else if(day.equals("18"))
    
        day = "18th";
    
    else if(day.equals("19"))
    
        day = "19th";
    

    else if(day.equals("20"))
    
        day = "20th";
    //end of 20th

    else if(day.equals("21"))
    
        day = "21st";
    

    else if(day.equals("22"))
    
        day = "22nd";
    

    else if(day.equals("23"))
    
        day = "23rd";
    

    else if(day.equals("24"))
    
        day = "24th";
    

    else if(day.equals("25"))
    
        day = "25th";
    //end of 25th

    else if(day.equals("26"))
    
        day = "26th";
    

    else if(day.equals("27"))
    
        day = "27th";
    

    else if(day.equals("28"))
    
        day = "28th";
    

    else if(day.equals("28"))
    
        day = "28th";
    

    else if(day.equals("29"))
    
        day = "29th";
    

    else if(day.equals("30"))
    
        day = "30th";
    //end of the 30th

    else if(day.equals("31"))
    
        day = "31st";
    

    else 
    
        System.out.println("Invalid day type.");
    

    return month + day;






    

private int parseInt(String substring) 
    // TODO Auto-generated method stub
    return 0;



public String getZodiac(String zodiac)

    String zodiacMonthS = zodiac.substring(0, 2);
    String zodiacDayS = zodiac.substring(3, 5);
    int zodiacMonth = parseInt(zodiacMonthS);
    int zodiacDay = parseInt(zodiacDayS);
    String zodiacFinal;

            if      ((zodiacMonth == 12 && zodiacDay >= 22 && zodiacDay <= 31) || (zodiacMonth ==  1 && zodiacDay >= 1 && zodiacDay <= 19))
             zodiacFinal = "Capricorn";
           else if ((zodiacMonth ==  1 && zodiacDay >= 20 && zodiacDay <= 31) || (zodiacMonth ==  2 && zodiacDay >= 1 && zodiacDay <= 17))
             zodiacFinal = "Aquarius";
           else if ((zodiacMonth ==  2 && zodiacDay >= 18 && zodiacDay <= 29) || (zodiacMonth ==  3 && zodiacDay >= 1 && zodiacDay <= 19))
               zodiacFinal = "Pisces";
           else if ((zodiacMonth ==  3 && zodiacDay >= 20 && zodiacDay <= 31) || (zodiacMonth ==  4 && zodiacDay >= 1 && zodiacDay <= 19))
             zodiacFinal = "Aries";
           else if ((zodiacMonth ==  4 && zodiacDay >= 20 && zodiacDay <= 30) || (zodiacMonth ==  5 && zodiacDay >= 1 && zodiacDay <= 20))
            zodiacFinal = "Taurus";
           else if ((zodiacMonth ==  5 && zodiacDay >= 21 && zodiacDay <= 31) || (zodiacMonth ==  6 && zodiacDay >= 1 && zodiacDay <= 20))
            zodiacFinal = "Gemini";
           else if ((zodiacMonth ==  6 && zodiacDay >= 21 && zodiacDay <= 30) || (zodiacMonth ==  7 && zodiacDay >= 1 && zodiacDay <= 22))
            zodiacFinal = "Cancer";
           else if ((zodiacMonth ==  7 && zodiacDay >= 23 && zodiacDay <= 31) || (zodiacMonth ==  8 && zodiacDay >= 1 && zodiacDay <= 22))
            zodiacFinal = "Leo";
           else if ((zodiacMonth ==  8 && zodiacDay >= 23 && zodiacDay <= 31) || (zodiacMonth ==  9 && zodiacDay >= 1 && zodiacDay <= 22))
            zodiacFinal = "Virgo";
           else if ((zodiacMonth ==  9 && zodiacDay >= 23 && zodiacDay <= 30) || (zodiacMonth == 10 && zodiacDay >= 1 && zodiacDay <= 22))
            zodiacFinal = "Libra";
           else if ((zodiacMonth == 10 && zodiacDay >= 23 && zodiacDay <= 31) || (zodiacMonth == 11 && zodiacDay >= 1 && zodiacDay <= 21))
            zodiacFinal = "Scorpio";
           else if ((zodiacMonth == 11 && zodiacDay >= 22 && zodiacDay <= 30) || (zodiacMonth == 12 && zodiacDay >= 1 && zodiacDay <= 21))
            zodiacFinal = "Sagittarius";
           else
            zodiacFinal = "Illegal date";


            return zodiacFinal;




这是我的测试仪:

import java.util.Scanner; 

public class HoroscopeTester 

/**
 * @param args
 */
public static void main(String[] args) 

    System.out.println("What is your birthday? Type in month and year as two numbers.  Ex. 01 01(January 1st)");

    Scanner scanner = new Scanner(System.in);

    String birth = scanner.nextLine();
    String zodiac = scanner.nextLine();

    Horoscope horoscope = new Horoscope();

    System.out.println(horoscope.getBirthday(birth));
    System.out.print(horoscope.getZodiac(zodiac));
    scanner.close();




日期打印正常,但十二生肖部分出错。我还注意到输入日期后我需要按两次回车键。我不确定这是为什么。这是控制台为我显示的内容:

What is your birthday? Type in month and year as two numbers.  Ex. 01 01(January 1st)
02 14

February 14th
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 2
at java.lang.String.substring(Unknown Source)
at Horoscope.getZodiac(Horoscope.java:246)
at HoroscopeTester.main(HoroscopeTester.java:21)

【问题讨论】:

【参考方案1】:

问题是字符串的值超出了它的索引,即如果你已经初始化了一个大小为 10 的字符串并且你尝试存储超过 10 的长度。这个错误是在主函数中检查字符串初始化并将其初始化为更大的大小。

【讨论】:

以上是关于线程“main”中的 Java 异常 java.lang.StringIndexOutOfBoundsException 错误的主要内容,如果未能解决你的问题,请参考以下文章

如何解决线程“main”中的异常,java.lang.ArithmeticException:/为零? [关闭]

我的代码上的线程“main”java.util.NoSuchElementException 中的异常?

线程“main”中的 Java 异常 java.lang.StringIndexOutOfBoundsException 错误

如何修复运行时错误-线程“main”java.util.NoSuchElementException中的异常

HTTPClient 示例 - 线程“main”中的异常 java.lang.NoSuchFieldError: INSTANCE

线程“主”java.lang.ArrayIndexOutOfBoundsException 中的异常:Calculator.main 中的 0(Calculator.java:25)[重复]