| | |
simple program tokenizer problem
![]() |
•
•
Join Date: Jun 2004
Posts: 6
Reputation:
Solved Threads: 0
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);
}
}
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);
}
}
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.
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.
Java Syntax (Toggle Plain Text)
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(); if(month=="01") month = "January"; else if(month=="02") month = "February"; else if(month=="03") month = "March"; else if(month== "04") month = "April"; else if(month== "05") month = "May"; else if(month== "06") month = "June"; else if(month== "07") month = "July"; else if(month== "08") month = "August"; else if(month== "09") month = "September"; else if(month== "10") month = "October"; else if(month== "11") month = "November"; else if(month== "12") month = "December"; 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); } }
I think that should work.
•
•
Join Date: Jun 2004
Posts: 6
Reputation:
Solved Threads: 0
•
•
•
•
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.
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.
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.
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.
Java Syntax (Toggle Plain Text)
if (a==5) // 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.
•
•
Join Date: May 2004
Posts: 80
Reputation:
Solved Threads: 5
•
•
•
•
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.
You're save with Java, though; the shortcut is written into the language specification.
--sg
•
•
Join Date: May 2004
Posts: 80
Reputation:
Solved Threads: 5
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
--sg
![]() |
Similar Threads
Other Threads in the Java Forum
- Previous Thread: Create stats from a text file
- Next Thread: Is is possible to use windows common controls in java
| Thread Tools | Search this Thread |
account android api applet application array arrays automation awt bidirectional binary birt bluetooth busy_handler(null) chat class classes client code columns component database designadrawingapplicationusingjavajslider draw eclipse error errors eventlistener exception expand fractal game givemetehcodez graphics gui guidancer homework html ide image inetaddress inheritance integer intellij j2me java javamicroeditionuseofmotionsensor javaprojects jlabel jme jni jpanel jtextfield julia linux list loop map method methods midlethttpconnection mobile mobiledevelopmentcreatejar monitoring myaggfun netbeans newbie nullpointerexception open-source plazmic print problem program programming project property recursion ria scanner search server set smart sms smsspam sort sourcelabs splash sql sqlite static string subclass support swing testautomation threads tree webservices windows





