import javax.swing.*;
class Month
{
public static void main(String[]args)
{
String value=JOptionPane.showInputDialog(null,"Enter Month Day and Year");

if(value.length()==10)
{
char Month=value.charAt(0)+value.charAt(1);
char Day=value.charAt(3)+value.charAt(4);
char Year=value.charAt(6)+value.charAt(7)+value.charAt(8)+value.charAt(9);



JOptionPane.showMessageDialog(null," "+Month+"/"+Day+"/"+Year+"");
}

else
{
JOptionPane.showMessageDialog(null,"INVALID INPUT!!");
}

}
}

Hello i need help about combing the charAt()'s

for example i would input 05/05/2010
i want to separate them into Month, Day and Year using charAt() method so that i could display them again. but my problem is it seems that it is not allowed to add two or more charAt()'s. any advice on how will i separate them? Thanks in advance.

Recommended Answers

All 3 Replies

First, if you need more than one char (e.g. "05"), you cannot store this in a char variable because char can only store a single character (e.g. '0'). So your Month, Day and Year variables should be String instead.

Second, char in Java can be interpreted as a number, since characters are represented by their ASCII codes. So you have to be careful that Java doesn't think you are trying to literally add with the + operator, rather than concatenate. So start your expression with the empty string (""), something like this:

String Month = "" + value.charAt(0) + value.charAt(1);
commented: thaaanks +1

If you are not forced to use charAt() (assignment restrictions, etc') I recommend a different approach - using String's split(String regex) method with the regex "/", you can divide your String into a String array with the day, month and year in its cells. This will be much more convinient code-wise and will be more flexible in terms if how user formats the date (i.e. 2/5/10, 2/5/2010, 2/05/2010 will all work).

commented: thaaanks +1

thanks a lot im finished yeeess!!!

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.