• High School idiot's java switch problem
    24 replies, posted
Ok, well, all my experience come from a comp sci class at my High School and what I've looked up on my own, so don't expect me to know anything. But basically I'm trying to make a string inputted by the user into an array, which I can then call the indices of to do whatever with. The string is put into an array correctly, my switch method's not working correctly. Here's all of the relevant code: [lua] String question = "ABCDEF"; System.out.print("Type something: "); InputStreamReader input = new InputStreamReader(System.in); BufferedReader reader = new BufferedReader(input); try{ question = reader.readLine(); } catch(Exception e){} int a = 10; int b; int c; int d; int e; int f; int x; String y; char[] questionarray = question.toCharArray(); switch(questionarray[0]) { case 0: a = 0; break; case 1: a = 1; break; case 2: a = 2; break; case 3: a = 3; break; case 4: a = 4; break; case 5: a = 5; break; case 6: a = 6; break; case 7: a = 7; break; case 8: a = 8; break; case 9: a = 9; break; default: System.out.println("Invalid"); break; } System.out.println(a); for (char z : questionarray) System.out.println(z);[/lua] Here's two sample outputs: [code]Type something: abcdefg12345 Invalid 10 a b c d e f g 1 2 3 4 5 [/code][code]Type something: 3 Invalid 10 3 [/code] So the A value isn't changing. I'm assuming it's because it's a CharArray, and it needs int's. Or not. I'm incredibly new at this. Sorry, I realize this is probably a really simple solution, but we're not being taught anything about this in class.
I don't know Java (syntax or in general) but can't you just use an if statement to check if its within range, and then if so assign it to that value or something? Since that is really all you're doing. Sorry if I'm of no help what so ever. And if you need to you just convert it so like: int test = Integer.parseInt(questionarray[0]); if(0 <= test <= 9) { a = test; } else { System.out.println("Invalid"); }
Instead of using [code] case 1: //etc etc. [/code] try using [code] case 'a': case 'b': [/code] At least I think thats what your trying to accomplish Protip: You can use any primitive types in switch statements, not just integers.
And yeah what Hawkfight said works, too. Might be better too since you don't have to use the function to convert it or save it into memory? You could profile it.
Do you have to use switches? It seems illogical, because they're usually used for number values only, if I remember correctly.
[QUOTE=nicatronTg;27965182]Do you have to use switches? It seems illogical, because they're usually used for number values only, if I remember correctly.[/QUOTE] It's probably an AP assignment, I think I remember doing something similar.
[QUOTE=Hawkfight207;27965194]It's probably an AP assignment, I think I remember doing something similar.[/QUOTE] I'm pretty sure the AP Computer Science A test doesn't allow (or at least doesn't cover) switch blocks, chars, and a few other things. That's what my teacher's been telling us since the beginning of the year. [editline]9th February 2011[/editline] [url]http://www.collegeboard.com/student/testing/ap/compsci_a/java.html[/url] [editline]9th February 2011[/editline] then again you could always have an assignment that goes out of the scope of the AP test...
For your switch statement you are only comparing the first character of question, I hope you know that.
[QUOTE=Hawkfight207;27965143] try using [code]case 'a': case 'b':[/code] [/QUOTE] Thank you, that's exactly what I needed. Thanks to everyone else to, but this seemed to work the best.
Ok, I have another question, this time about combing two integers into one. So, if I have: int a = 3; int b = 5; int c; Could I put 'a' and 'b' together to make 'c' = 35 or 53? If so, how?
You could convert them into strings, and then just concatenate them and parse them back to an int.
[QUOTE=laharlsblade;27981474]int a = 3; int b = 5; int c; Could I put 'a' and 'b' together to make 'c' = 35 or 53? If so, how?[/QUOTE] [cpp] int c = (a * 10) + b; int d = (b * 10) + a; [/cpp]
What's with Wyzard being rated dumb?
You have two numbers and you want to make another, related number from them. Arithmetic is the simple and straightforward way to do it. Sure, you [i]could[/i] convert them to strings, concatenate, and parse back to a number as Whitewater suggested, but that's far less efficient than simple multiplication and addition.
[QUOTE=Wyzard;27983768][cpp] int c = (a * 10) + b; int d = (b * 10) + a; [/cpp][/QUOTE] ...damn. I can't believe I didn't think of that. I'm normally good at finding math work-arounds. Well, thanks. [editline]11th February 2011[/editline] [QUOTE=Wyzard;27983768][cpp] int c = (a * 10) + b; int d = (b * 10) + a; [/cpp][/QUOTE] ...damn. I can't believe I didn't think of that. I'm normally good at finding math work-arounds. Well, thanks.
[QUOTE=Wyzard;27987676]You have two numbers and you want to make another, related number from them. Arithmetic is the simple and straightforward way to do it. Sure, you [i]could[/i] convert them to strings, concatenate, and parse back to a number as Whitewater suggested, but that's far less efficient than simple multiplication and addition.[/QUOTE] Yeah, but when b >= 10 (for c, a >= 10 for d) it'll fail. Needs to be more like... [cpp]int c = (a * Math.pow( 10, Integer.toString(b).length ) ) + b;[/cpp] Alternatively, since strings do take up a bit of memory [cpp]int len = 0; int temp = b; while( temp > 0 ){temp/=10;len++} int c = ( a * Math.pow( 10, len ) ) + b;[/cpp]
Whats the point of storing two integers in a single integer if you have to keep track of the length? I far as I can tell, only 0-9 is necessary.
[QUOTE=Whitewater;27999095]Yeah, but when b >= 10 (for c, a >= 10 for d) it'll fail. Needs to be more like... [cpp]int c = (a * Math.pow( 10, Integer.toString(b).length ) ) + b;[/cpp] Alternatively, since strings do take up a bit of memory [cpp]int len = 0; int temp = b; while( temp > 0 ){temp/=10;len++} int c = ( a * Math.pow( 10, len ) ) + b;[/cpp][/QUOTE] (Math.floor(Math.log10(b)) + 1) is a more efficient way to determine the number of digits in a positive number. (Zero has to be handled as a special case, though; a logarithm of zero is undefined.)
String objects are already an array. you can get them by using the *string's name*.charAt(whatever index). Is it required of you to do it like that? Because that seems like a silly amount of unneeded work.
[QUOTE=redonkulous;28076527]String objects are already an array. you can get them by using the *string's name*.charAt(whatever index). Is it required of you to do it like that? Because that seems like a silly amount of unneeded work.[/QUOTE] String a class containing a character array, not a character array. They could expose the character array, but then it would break encapsulation.
Why did my implementation (the first post) get funnies? I don't know Java, but seems like it would work fine to me.
[QUOTE=Collin665;28113498]Why did my implementation (the first post) get funnies? I don't know Java, but seems like it would work fine to me.[/QUOTE] if (0 <= test <= 9) won't work in most, if not all programming languages... if (0 <= test && test <= 9)
[QUOTE=robmaister12;28110335]String a class containing a character array, not a character array. They could expose the character array, but then it would break encapsulation.[/QUOTE] That is what I mean. Strings already have an array that you can get the shit from with charAt().
This is Java, the equivalent of C++ std::string::at in Java is String.charAt.
Sorry, you need to Log In to post a reply to this thread.