Computer Science Canada

Tokenizer Help

Author:  jamonathin [ 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 Confused

Author:  wtd [ Mon Dec 05, 2005 1:30 pm ]
Post subject: 

Something this simple doesn't need any kind of special tokenizer class.

Java:
String original = "04/21/90";
String[] parts = original.split("/");

Author:  jamonathin [ Mon Dec 05, 2005 2:05 pm ]
Post 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 Smile.

Author:  jamonathin [ Mon Dec 05, 2005 2:14 pm ]
Post subject: 

Since you're so quick at responding wtd Smile . . 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.

Author:  wtd [ Mon Dec 05, 2005 4:16 pm ]
Post subject: 

Java:
||


Java:
BufferedReader keyboard = new BufferedReader(
   new InputStreamReader(System.in));

Long inputLong = Long.parseLong(keyboard.readLine());

Author:  Hikaru79 [ Tue Dec 06, 2005 8:36 am ]
Post subject: 

Although wtd is right, you don't need a StringTokenizer for something like that, here's what it would have been if you'd chosen to do it.
Java:
StringTokenizer st = new StringTokenizer(myString, "/");

The second, optional, argument is a String, of which every individual character is a delimiter.

Author:  jamonathin [ Tue Dec 06, 2005 11:39 am ]
Post subject: 

Sounds good, thanks for your help wtd and hikaru

Author:  jamonathin [ Tue Dec 06, 2005 11:59 am ]
Post subject: 

wtd wrote:
Java:
||



Another question about that wtd. I tried using random symbols in my attempts at trying to find out the 'or' sign, and i tried using
Java:
^

inbetween my conditions and the program ran it, then I switched over to the " || ", and it produced the same thing. Is that ^ an or sign or what Confused

Author:  wtd [ Tue Dec 06, 2005 1:06 pm ]
Post subject: 

jamonathin wrote:
wtd wrote:
Java:
||



Another question about that wtd. I tried using random symbols in my attempts at trying to find out the 'or' sign, and i tried using
Java:
^

inbetween my conditions and the program ran it, then I switched over to the " || ", and it produced the same thing. Is that ^ an or sign or what Confused


http://java.sun.com/docs/books/tutorial/java/nutsandbolts/bitwise.html

It's "bitwise exclusive or".

Author:  1of42 [ Tue Dec 06, 2005 6:30 pm ]
Post subject: 

Hikaru79 wrote:
Although wtd is right, you don't need a StringTokenizer for something like that, here's what it would have been if you'd chosen to do it.
Java:
StringTokenizer st = new StringTokenizer(myString, "/");

The second, optional, argument is a String, of which every individual character is a delimiter.


StringTokenizer is deprecated, don't even bother.

Author:  jamonathin [ Wed Dec 07, 2005 1:37 pm ]
Post 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:
Java:

import java.io.*;

public class game {
       
        public static void main(String[] args) {
               
                BufferedReader in = new BufferedReader (new InputStreamReader (System.in));
                       
                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);
                        }
                }

        }

}

Author:  wtd [ Wed Dec 07, 2005 2:37 pm ]
Post 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.

code:
>> 564696543201678654789456 > 18446744073709551615
=> true

Author:  jamonathin [ Wed Dec 07, 2005 2:49 pm ]
Post 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.

Sample Program Execution:

1st input: 10371
2nd input: 315
Result: 10686

1st input: 54696543201678654789456
2nd input: 82067690004564356875434
Result: 136764233206243011664890

1st input: 0
2nd input: 0
Result: 0


What would I do if long wont take them Confused . .

Author:  wtd [ Wed Dec 07, 2005 2:53 pm ]
Post subject: 

http://java.sun.com/j2se/1.4.2/docs/api/java/math/BigInteger.html

Author:  Hikaru79 [ Wed Dec 07, 2005 11:33 pm ]
Post subject: 

1of42 wrote:
Hikaru79 wrote:
Although wtd is right, you don't need a StringTokenizer for something like that, here's what it would have been if you'd chosen to do it.
Java:
StringTokenizer st = new StringTokenizer(myString, "/");

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?

Author:  wtd [ Wed Dec 07, 2005 11:34 pm ]
Post subject: 

I agree. I see no signs that it's deprecated. Though, in all honesty, the Scanner class may duplicate much of its functionality in a somewhat more convenient form.

Author:  jamonathin [ Fri Dec 09, 2005 8:34 am ]
Post subject: 

One last question. How would i do this. The user inputs a series of "things", and then the user enters some options of what could be in those things, and I have to check wether or not those things are in there.

Such as . .
code:

First Input: (Pink,Green,Orange,Blue)
Second Input: (Pink,Yellow)

Answer: NO

First Input: (Pink,Green,Orange,Blue)
Second Input: (Blue,Green)

Answer: YES


Confused pleh

Author:  wtd [ Fri Dec 09, 2005 9:48 am ]
Post subject: 

http://java.sun.com/j2se/1.4.2/docs/api/java/util/Collection.html

Check out the containsAll method

Author:  jamonathin [ Fri Dec 09, 2005 10:45 am ]
Post subject: 

Ok, easy enough, now how to I break up the second line. As in, how do i seperate the words.

I know i have to go
Java:

String[] hold = two.split("(");

and split it part by part, then i would go something like
Pseudo:

for (int i = 1 ; i <= upper(hold) ; i++)
{
     str += hold[i];
}
String[] hold = hold.split(",");

then repeat for ( ")" ); . . but how can i look for the upper bounds of [i]hold?
, or maybe there's an easier way Confused . .

Author:  jamonathin [ Fri Dec 09, 2005 10:47 am ]
Post subject: 

(where's edit Confused) Oh, and the second entry isn't in any specific order either, but I know that the final if statement will look something like

Java:

if (one.contains(two) == true)

Author:  wtd [ Fri Dec 09, 2005 11:09 am ]
Post subject: 

The input format is guaranteed?

Is fo, then just "trim" off extra whitespace, remove the two parens, trim whitespace again, then split on: "\s*,\s*".

Author:  jamonathin [ Fri Dec 09, 2005 12:05 pm ]
Post subject: 

Ok sounds good, thanx wtd Smile.

Author:  wtd [ Fri Dec 09, 2005 2:47 pm ]
Post subject: 

wtd wrote:
Is fo


Yay! I'm channeling Dan again.


: