simple program tokenizer problem

Reply

Join Date: Jun 2004
Posts: 6
Reputation: tgraves is an unknown quantity at this point 
Solved Threads: 0
tgraves tgraves is offline Offline
Newbie Poster

simple program tokenizer problem

 
0
  #1
Jul 3rd, 2004
I get errors targetting the ' that I use for each case. I tried using nothing and also quotes in their place, but no luck. Whats the problem.

If this isn't the place to get help with programs, please point me in the right direction.







import java.util.StringTokenizer;

class Birthday {
public static void main(String[] arguments) {
StringTokenizer st1;
String birthday = "09/08/1900";
st1 = new StringTokenizer(birthday, "/");
String month = st1.nextToken();
switch (month) {
case '01':
month = "January";
break;
case '02':
month = "February";
break;
case '03':
month = "March";
break;
case '04':
month = "April";
break;
case '05':
month = "May";
break;
case '06':
month = "June";
break;
case '07':
month = "July";
break;
case '08':
month = "August";
break;
case '09':
month = "September";
break;
case '10':
month = "October";
break;
case '11':
month = "November";
break;
case '12':
month = "December";
break;
}

String day = st1.nextToken();
String year = st1.nextToken();

System.out.println("The month is " + month);
System.out.println("The day is " + day);
System.out.println("The year is " + year);
}
}
Reply With Quote Quick reply to this message  
Join Date: Jun 2003
Posts: 313
Reputation: red_evolve is on a distinguished road 
Solved Threads: 0
red_evolve's Avatar
red_evolve red_evolve is offline Offline
Posting Whiz

Re: simple program tokenizer problem

 
0
  #2
Jul 3rd, 2004
Greetings.
If I am not mistaken, the target in the switch statement works for Integers only.
Java experts, please correct me if I'm wrong.
You could use If statements instead if switch doesn't work.
  1. import java.util.StringTokenizer;
  2.  
  3. class Birthday
  4. {
  5. public static void main(String[] arguments)
  6. {
  7. StringTokenizer st1;
  8. String birthday = "09/08/1900";
  9. st1 = new StringTokenizer(birthday, "/");
  10. String month = st1.nextToken();
  11.  
  12. if(month=="01")
  13. month = "January";
  14. else if(month=="02")
  15. month = "February";
  16. else if(month=="03")
  17. month = "March";
  18. else if(month== "04")
  19. month = "April";
  20.  
  21. else if(month== "05")
  22. month = "May";
  23. else if(month== "06")
  24. month = "June";
  25. else if(month== "07")
  26. month = "July";
  27. else if(month== "08")
  28. month = "August";
  29. else if(month== "09")
  30. month = "September";
  31. else if(month== "10")
  32. month = "October";
  33. else if(month== "11")
  34. month = "November";
  35. else if(month== "12")
  36. month = "December";
  37.  
  38. String day = st1.nextToken();
  39. String year = st1.nextToken();
  40.  
  41. System.out.println("The month is " + month);
  42. System.out.println("The day is " + day);
  43. System.out.println("The year is " + year);
  44. }
  45. }

I think that should work.
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 6
Reputation: tgraves is an unknown quantity at this point 
Solved Threads: 0
tgraves tgraves is offline Offline
Newbie Poster

Re: simple program tokenizer problem

 
0
  #3
Jul 4th, 2004
Originally Posted by red_evolve
Greetings.
If I am not mistaken, the target in the switch statement works for Integers only.
Java experts, please correct me if I'm wrong.
You could use If statements instead if switch doesn't work.

I think that should work.

Thanks. Well I know If statements would work, but I really want to figure out whats wrong with the switch, so I dont have the same problem with another program.



A better question I need answering is

When should I use "==" instead of "="?

The book I'm reading doesn't explain this one clearly.

I do understand that using && prevents the right side of the If statement from being run if the left side is not true.
Reply With Quote Quick reply to this message  
Join Date: Jun 2003
Posts: 313
Reputation: red_evolve is on a distinguished road 
Solved Threads: 0
red_evolve's Avatar
red_evolve red_evolve is offline Offline
Posting Whiz

Re: simple program tokenizer problem

 
0
  #4
Jul 4th, 2004
Greetings.
LOL, I see. That's good. I mean, you have the inquisitiveness to explore although you know of a way which would work.

Well, "==" differs from "=" as in:-
-> "=" is used for evaluating an expression (arithmetic).
-> "==" would always evaluate to a Boolean value, either True or False.

So, I would make it easy that, "==" is used in determining the Truth of an expression. You want to test whether something is equal to something. I.e. Is 'a' equal to 5? If yes, do something.
  1. if (a==5)
  2. // do something

I hope you get what I mean. Not really good at explaining things.
And, about the "&&" operator, yeah, it's true.
If the left hand side is false, it will never evaluate the right hand side because due to the nature of AND operator, one false, all false right? So, if there is one expression that you would deem important and must be evaluated, put it on the left hand side.
However, I kinda remember something from my Programming Language Concept class some time back that, this differs in some programming languages.
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 6
Reputation: tgraves is an unknown quantity at this point 
Solved Threads: 0
tgraves tgraves is offline Offline
Newbie Poster

Re: simple program tokenizer problem

 
0
  #5
Jul 7th, 2004
Thank you for your help. More questions to come when I find time to learn more.
Reply With Quote Quick reply to this message  
Join Date: Jun 2003
Posts: 313
Reputation: red_evolve is on a distinguished road 
Solved Threads: 0
red_evolve's Avatar
red_evolve red_evolve is offline Offline
Posting Whiz

Re: simple program tokenizer problem

 
0
  #6
Jul 11th, 2004
Greetings.
No problem!
"Study the past if you would define the future" - Confucius
Reply With Quote Quick reply to this message  
Join Date: May 2004
Posts: 72
Reputation: gusano79 is on a distinguished road 
Solved Threads: 5
gusano79 gusano79 is offline Offline
Junior Poster in Training

Re: simple program tokenizer problem

 
0
  #7
Jul 12th, 2004
Originally Posted by red_evolve
... the target in the switch statement works for Integers only. ...
An expression in a switch statement can be any of these: int, short, byte, or char. red_evolve is basically correct, though, since they're all integer types under the hood.

--sg
Reply With Quote Quick reply to this message  
Join Date: May 2004
Posts: 72
Reputation: gusano79 is on a distinguished road 
Solved Threads: 5
gusano79 gusano79 is offline Offline
Junior Poster in Training

Re: simple program tokenizer problem

 
1
  #8
Jul 12th, 2004
Originally Posted by red_evolve
...
And, about the "&&" operator, yeah, it's true.
If the left hand side is false, it will never evaluate the right hand side
...
this differs in some programming languages.
With some languages, it may depend on the compiler as well. Shortcutting an operation like that can be considered an optimization; I've heard of compilers (can't think of any off the top of my head though) that stop evaluating the expression if the LHS is false when optimizations are turned on, but will evaluate everything when they're turned off... this is especially frustrating when the debug build (optimization off) works fine, but the release build (optimization on) doesn't.

You're save with Java, though; the shortcut is written into the language specification.

--sg
Reply With Quote Quick reply to this message  
Join Date: May 2004
Posts: 72
Reputation: gusano79 is on a distinguished road 
Solved Threads: 5
gusano79 gusano79 is offline Offline
Junior Poster in Training

Re: simple program tokenizer problem

 
0
  #9
Jul 12th, 2004
Another way to approach your switch statement is to convert the string to a number first, then use the number in the switch. The Integer class can do this.

--sg
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC