Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Tokenizer Help
Index -> Programming, Java -> Java Help
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
jamonathin




PostPosted: 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
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: 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




PostPosted: 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 Smile.
jamonathin




PostPosted: Mon Dec 05, 2005 2:14 pm   Post subject: (No 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.
wtd




PostPosted: Mon Dec 05, 2005 4:16 pm   Post subject: (No subject)

Java:
||


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

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




PostPosted: Tue Dec 06, 2005 8:36 am   Post subject: (No 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.
jamonathin




PostPosted: Tue Dec 06, 2005 11:39 am   Post subject: (No subject)

Sounds good, thanks for your help wtd and hikaru
jamonathin




PostPosted: Tue Dec 06, 2005 11:59 am   Post subject: (No 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
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Tue Dec 06, 2005 1:06 pm   Post subject: (No 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".
1of42




PostPosted: Tue Dec 06, 2005 6:30 pm   Post subject: (No 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.
jamonathin




PostPosted: 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:
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);
                        }
                }

        }

}
wtd




PostPosted: 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.

code:
>> 564696543201678654789456 > 18446744073709551615
=> true
jamonathin




PostPosted: 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.

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 . .
wtd




PostPosted: Wed Dec 07, 2005 2:53 pm   Post subject: (No subject)

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




PostPosted: Wed Dec 07, 2005 11:33 pm   Post subject: (No 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?
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 2  [ 23 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: