Posted: Mon Dec 05, 2005 1:25 pm Post subject: Tokenizer Help
Hey all, this Friday is the computer science competition (Hikaru knows what im talkin bout) and my java skills are pretty poop. Unfortionately for me my c++ is much better and I didn't find out untill last week that we could of used c++ but there's a completely different way of compiling it, and changing would juss screw up my teammates.
So, anyways, what I'm asking is this. There were some sample problems for us to try and some of them included Tokenizer.
Now I understand how to get the "tokens" when there is a space inbetween and convert them to an integer but how about when the user inputs . .
code:
04/21/90
All i've understood so far was how to grab those numbers, not how to extract them out of a string. How would i go about taking the '04', '21' and '90' out of that. And the input will always be in that same format
( dd/mm/yy ) pleh
Sponsor Sponsor
wtd
Posted: Mon Dec 05, 2005 1:30 pm Post subject: (No subject)
Something this simple doesn't need any kind of special tokenizer class.
Java:
String original = "04/21/90";
String[] parts = original.split("/");
jamonathin
Posted: Mon Dec 05, 2005 2:05 pm Post subject: (No subject)
Ok, so basically i make a new array variable that will end up being three because the string is being split. Then i can assign the arrays to whatever var i want.
Thanks wtd, looks simple enough .
jamonathin
Posted: Mon Dec 05, 2005 2:14 pm Post subject: (No subject)
Since you're so quick at responding wtd . . i have 2 quick questions for you. How do you write or in an if statement. "and" is &&, but what is or?. And how would you go about getting a "long" variable from the user.
wtd
Posted: Mon Dec 05, 2005 4:16 pm Post subject: (No subject)
The second, optional, argument is a String, of which every individual character is a delimiter.
StringTokenizer is deprecated, don't even bother.
jamonathin
Posted: Wed Dec 07, 2005 1:37 pm Post subject: (No subject)
This goes with the long question I had for wtd. I think the code you gave me isn't for that long of numbers. When i try: 564696543201678654789456 in my program, i get erros, but smaller numbers are fine.
Here's my code:
long one = 1,two = 1;
while(one !=0 && two !=0) { try { System.out.print("1st Input: ");
one = Long.parseLong(in.readLine());
System.out.print("2nd Input: ");
two = Long.parseLong(in.readLine());
long result = one + two;
if(one != 0 && two !=0) { System.out.println("Result: " + result + "\n");
} } catch(IOException inpEX) { System.err.println("Exception: " + inpEX);
} }
}
}
wtd
Posted: Wed Dec 07, 2005 2:37 pm Post subject: (No subject)
A "long" is a 64-bit integer The largest number an unsigned 64-bit integer can hold is 18446744073709551615. Your number is larger than that, so you have problems.
Posted: Wed Dec 07, 2005 2:49 pm Post subject: (No subject)
Well then ****. . . lol. Because one of the questions we had to do was
Problem 8: Extended Precision Addition wrote:
Develop a program that takes as input two "long" nonnegative integers and outpus their sum. The inputs will be no longer than 35 digits. The program should repeat until the user enters zero for both inputs.
The second, optional, argument is a String, of which every individual character is a delimiter.
StringTokenizer is deprecated, don't even bother.
Huh? How do you figure that? My JDK 1.5.0_4 doesn't seem to think it is (it will usually give you a warning if you're using deprecated objects) and the API ref at http://java.sun.com/j2se/1.5.0/docs/api/java/util/StringTokenizer.html doesn't say it is (it usually does). Where did you get the idea that such a useful thing is deprecated?