Author |
Message |
wtd
|
Posted: Wed May 03, 2006 11:30 pm Post subject: (No subject) |
|
|
You need to think abstractly.
Switch works on integer type expressions ->
The char type is a sort of integer ->
Switch works on chars.
You have a String type expression.
You need to deal with one character from that string.
If you can get that character, you're set. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
rizzix
|
Posted: Thu May 04, 2006 11:54 am Post subject: (No subject) |
|
|
Hmm, I'd suggest to keep the model and view separate.You see, that would work, but it would require a redesign when you decide to change to use _words_ to represent numbers (like "One" for 1), or if you internationalise it, etc. |
|
|
|
|
|
cool dude
|
Posted: Thu May 04, 2006 4:09 pm Post subject: (No subject) |
|
|
k i thought about it and this is wat i think... take the Ascii value of the letter, thus u will have an integer that u can work with. the only problem though is i have no idea how i would take the Ascii value of the letter? also wouldn't that be more code, more troubles and if statements would be better? why is it better to use switch cases instead of if statements? |
|
|
|
|
|
wtd
|
Posted: Thu May 04, 2006 4:17 pm Post subject: (No subject) |
|
|
In the switch statement you only state the name of the variable you're matching against once, and you don't write the == operator each time. This prevents typos, and the == operator can easily be confused with the = operator, which has dramatically different results.
Remember: all other things being equal, the more code you write, the more likely you are to screw something up.
As for strings...
code: | String foo = "hello";
... bar = foo[0];
System.out.println(bar); |
Fill in the ... part and tell me what the call to System.out.println prints. |
|
|
|
|
|
cool dude
|
Posted: Thu May 04, 2006 4:36 pm Post subject: (No subject) |
|
|
i hava no clue wat you are doing here? i guess it print watever value bar is although i don't know wat bar even means? also how does this relate to wat we r talking about? |
|
|
|
|
|
wtd
|
Posted: Thu May 04, 2006 4:51 pm Post subject: (No subject) |
|
|
A string is a series of characters. You need to be able to get just one of those characters out of a string. This what I'm trying to explain. |
|
|
|
|
|
cool dude
|
Posted: Thu May 04, 2006 5:22 pm Post subject: (No subject) |
|
|
is this wat u mean?
code: |
String hello = ("Hello");
System.out.println (hello.charAt(0)); |
if i do the same thing with the operation variable i could use it now as switch so thats good but now when i say case "A", case "B", etc. it says incompatible types.
this is the part of the code
code: |
operation2 = operation.charAt(0);
switch(operation2){
case "A":
sum = inum1 + inum2;
System.out.println("the sum of the numbers is " + sum);
break;
case "S":
subtraction = inum1 - inum2;
System.out.println("The subtracted numbers is equal to " + subtraction);
break;
case "M":
product = inum1 * inum2;
System.out.println("The product of the numbers is " + product);
break;
case "D":
quotient = inum1/inum2;
System.out.println("The quotient is " + quotient);
break;
} |
|
|
|
|
|
|
wtd
|
Posted: Thu May 04, 2006 5:44 pm Post subject: (No subject) |
|
|
Is "A" of type char? |
|
|
|
|
|
Sponsor Sponsor
|
|
|
cool dude
|
Posted: Thu May 04, 2006 6:05 pm Post subject: (No subject) |
|
|
wtd wrote: Is "A" of type char? "A" is not of type char its of type string but then how do i take "A" and change it to type char? or do i have to do exactly wat i did with operation2? i.e make more variables and declare them of type char and then convert "A" into type char? (i tried doing that but it doesn't work)
***Edit*** did i convert operation into char correctly? is that how u do it? |
|
|
|
|
|
wtd
|
Posted: Thu May 04, 2006 7:08 pm Post subject: (No subject) |
|
|
How did you declare operation2? If you don't know what I mean by "declare", then you have considerable fundamental reading to do. |
|
|
|
|
|
cool dude
|
Posted: Thu May 04, 2006 7:40 pm Post subject: (No subject) |
|
|
wtd wrote: How did you declare operation2? If you don't know what I mean by "declare", then you have considerable fundamental reading to do.
obviosly i know wat u mean by declare i know turing and VB so i think i know wat declare is. i declared operation2 as char.
|
|
|
|
|
|
wtd
|
Posted: Thu May 04, 2006 7:50 pm Post subject: (No subject) |
|
|
I "knew" a lot before I knew much of anything, so I try not to assume.
When in doubt, test with small apps.
code: | public class CharTest {
public static void main(String[] args) {
String foo = "foo";
char bar = foo[0];
System.out.println(bar);
}
} |
|
|
|
|
|
|
cool dude
|
Posted: Thu May 04, 2006 8:24 pm Post subject: (No subject) |
|
|
is the code supposed to not work? and can u be a little more descriptive because i think i'm a little lost on wat we r trying to do |
|
|
|
|
|
wtd
|
Posted: Thu May 04, 2006 8:32 pm Post subject: (No subject) |
|
|
If you want to figure out how fundamental little things like accessing characters in a string work, do so in a simple test app. That way you can be sure any flaws that might be in your larger program aren't distracting you.
Remeber, any complex problem is just a collection of smaller, simpler problems. |
|
|
|
|
|
cool dude
|
Posted: Thu May 04, 2006 10:43 pm Post subject: (No subject) |
|
|
wtd wrote: If you want to figure out how fundamental little things like accessing characters in a string work, do so in a simple test app. That way you can be sure any flaws that might be in your larger program aren't distracting you.
Remeber, any complex problem is just a collection of smaller, simpler problems.
thanks for the advice i usually do that especially when learning a new language. when i tried that code u gave in a simple test app. it did not work and said array required. anyhow wat is it supposed to do? |
|
|
|
|
|
|