-5

I am new to java. I have been trying to run a program but it gives me this error. I don't understand why it doesn't work. My input is definitely a string and the method returns an int.

So I am confused as to why I get a format exception? Thanks to anyone that can help.

public class TestAA3 {

    public static void main(String[] args) { 
        int day = getDay("04/09/2034");
        System.out.print(day);   

    }

    public static String getSubstring( String s, int i, int j) {
        // declaring the String that will eventually be modified and returned by the method
        String Substring = " "; 
        // error message
        if (j<i) {
            throw new IllegalArgumentException("The second integer must be greater than the first");
        } else {
            // defining the limits of the new string
            for ( int position = i;position<=j; position++) {
                // new value of the String
                Substring += " " + s.charAt(position);

            }
            return Substring;
        }
    }

    // calling getSubstring and defining the position inside the string of the int that will be returned
    public static int getDay(String s) {

        if (s.charAt(0)==0){
            String dayString = getSubstring(s,1,1);
            return Integer.valueOf(dayString);
        } else {
            String dayString = getSubstring(s,0,1);
            return Integer.valueOf(dayString);  
        }
    }
}
  • 2
    `java.lang.NumberFormatException: For input string: " 0 4"` <- You are adding spaces to your number in the substring method. `" "` is not an empty String but a String containing a space. Use `""` instead. – OH GOD SPIDERS Nov 01 '18 at 16:11
  • 1
    You don't have to implement `getSubString`, Java already has this: `s.subString()`. You even did it wrong. Then, double-check the numbers you give to subString, they're wrong too. Use the [Javadoc](https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#substring%28int,%20int%29) for reference. – Mark Jeronimus Nov 01 '18 at 16:12
  • And instead of `s.charAt(0)==0` you probably want `Character.getNumericValue(s.charAt(0)) == 0`. – LuCio Nov 01 '18 at 16:13
  • @OHGODSPIDERS You are right!!! Thank you !!! – Patricia Manarazan Nov 01 '18 at 17:02

2 Answers2

1

Substring += " " + s.charAt(position); should be initialized to "". You initialize the space and add two spaces before the method. getSubstring(s,1,1); In fact, it is the second character. That is, the space and then you will turn to the number and you will get an error.

Toby Speight
  • 27,591
  • 48
  • 66
  • 103
  • 2
    Please [edit] your answer to include words only found in the English language. – K.Dᴀᴠɪs Nov 01 '18 at 16:18
  • You need to [edit] the answer, rather than adding the English version as comment (I've done that for you this time, but you might need to fix it up). BTW, welcome to Stack Overflow - please hang around and contribute some more (in English!) – Toby Speight Nov 01 '18 at 16:48
0

at a first look what i can see is that s.charAt(int index) returns a char, so what you actually need to check there is: s.charAt(0).equals('0'), second, there are better ways to treat dates, for example using the data type Date which allows you to call a function that returns the day/month/year. hope it helps.