EDIT: i mean "parsing number from a string?" sorry.

what should i do to parse a number from a string?

i tried this.

Double leap = Double.parseDouble(this.yyyy);

but it gives me error.

i want to keep the zero in front of the input.

for example:
1. i input 08 (this is a string)
2. parse the 08 to an appropriate data type, and it should not lose the leading zero.

how?


BTW, i'm creating a program that checks if the year inputd is a leap year. the needed input is only two digits.

Use "DecimalFormat".

import java.text.DecimalFormat;

private String twoDigitNum(String myNum)
{
    //specifies a 2-digit number
    DecimalFormat num = new DecimalFormat("00");

    //for a 4-digit number use the one below
    //DecimalFormat num = new DecimalFormat("0000");
        
    return (num.format(Double.parseDouble(myNum)));
}
Member Avatar for Rkeast
int number;
string blah = "2009";

try
{
number = Integer.parseInt(blah);
}
catch(e NumberFormatException)
{
System.out.println(e);
}

why dont you just change the format of the users input. Instead of making the value of there input a string make it a double

Java Code provide by cgeier for leap year is really impressive. It is very short and yet effective. I totally agree with him.

Member Avatar for Rkeast

cgeier's code can actually be made shorter by doing this instead:

private boolean isLeapYear(int userYear)
{
return (((userYear % 4 == 0) && (userYear % 100 != 0)) || (userYear % 400 == 0));
}

Good thinking....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.