Morning to all those Daniwebbers out there!

I have what I thought was an odd task of converting a String into an INT.

I know of the parser Integer.parseInt(x)

However if the user enters 02/14/2010 the / are not an INT type.
I've thought about using substring possibilities.
Taking substring 0 to 1 converting it to an INT, places 3-4 convert, and 6-9 convert.
from there I will test to see if those dates are possible and accurate.

Is this the best way to accomplish this?

Recommended Answers

All 10 Replies

What is the int result you want from the String: "02/14/2010"?

If its a Date you need, there are classes for parsing dates. See SimpleDateFormat

my ultimate goal is to test to see if they date the user enters actually works.

02/31/2010 would not work. To do so I will be using a nested if statement.

/* Useful StringTokenizer */

import java.util.*;
import javax.swing.*;

public class Date0 {
	public static void main(String args[]) {
String text= JOptionPane.showInputDialog(null,"Type in the data in the format'02/14/2010'");
int ymd[] = new int[3];
StringTokenizer s = new StringTokenizer(text,"/");
for (int i=0; s.hasMoreTokens(); i++) 
	ymd[i] = Integer.parseInt(s.nextToken());
	text ="Your input is " + ymd[2] + " year " + ymd[0] + " month " + ymd[1] + " day";
	JOptionPane.showMessageDialog(null,text,"Welcome to DaniWeb", 
	JOptionPane.INFORMATION_MESSAGE);
		
	}
}

Don't use a StringTokenizer or any sort of String splitter for validating dates. As already mentioned, use Date utility classes like SimpleDateFormat for validating dates unless the purpose of the exercise given to you is to implement Date validation. Also, StringTokenizer has been deprecated in favour of the String#split method.

the purpose here is to use nest if statements.
It seems that my idea of string splitting is correct SOS?

Yes, it seems that you'd have to use String#split to split the date into parts and validate each component (date, day, year) separately.

How do you use split() to remove extra spaces and get only the tokens?

String data = "there  are some spaces  here";
      String[] splitData = data.split(" ");
      System.out.println("splitData=" + Arrays.toString(splitData));
      //splitData=[there, , are, some, spaces, , here]   // notice empty slots and space

String#split accepts a regular expression string. Just use the normal regular expression constructs and you should be fine:

String s = "there  are some spaces  here";
// \s here means any whitespace and + means one or more occurrences
String[] arr = s.split("\\s+");
// output: ["there", "are", "some", "spaces", "here"]

References:
http://www.regular-expressions.info/
http://www.javamex.com/tutorials/regular_expressions/

Thanks, Almost but still have empty slot

String data = " there  are some spaces  here ";
      String[] splitData = data.split("\\s+");
      System.out.println("splitData=" + Arrays.toString(splitData) + " count=" + splitData.length);
      //splitData=[, there, are, some, spaces, here] count=6       // notice empty slots

split seems a lot harder to use than StringTokenizer Regular Expressions can be tough to figure out.

Thanks, Almost but still have empty slot. split seems a lot harder to use than StringTokenizer Regular Expressions can be tough to figure out.

No, not really. That's correct output because you are effectively asking it to "split" based on a whitespace. The empty slot is because there is a blank string before the whitespace. Simply use String#trim before splitting and it should work out fine for all cases.

String data = " there  are some spaces  here ";
String[] splitData = data.trim().split("\\s+");
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.