Results 1 to 9 of 9
Thread: compiles but dosent work...
- 01-01-2011, 06:23 PM #1
Member
- Join Date
- Jan 2011
- Posts
- 3
- Rep Power
- 0
compiles but dosent work...
^title
The program is ment to get the user to characters one at a time, and when they're finshed they hit the period, then the program outputs what was inputed, but will take lower case charicters and make them uppercase and vise a versa, (eg "a" to "A"), but when the program outputs, it outputs the exact input, i was trying to use an Array which I havent been taught how to use yet, and i might be doing it wrong, but anyway heres the code:
(theres 4 charArrays becuase i couldn't fiqure out what was wrong, and I eventually did, and it was I was using a ( bracket and not a { bracket, and I thuaght it was trying to tell me that the line was to long or something)
Java Code:import java.io.*; class letterswitch { public static void main(String[] args)throws IOException { String user = "a"; String state = ""; String finsh = "."; int value = 0; char[] charArray1 = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n'}; char[] charArray2 = {'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'}; char[] charArray3 = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M'}; char[] charArray4 = {'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'}; InputStreamReader inStream = new InputStreamReader (System.in); BufferedReader input = new BufferedReader (inStream); System.out.println("Please enter charicters one at a time, when your finshed please enter a period."); while (value != 1){ try{ user = input.readLine(); } catch(NumberFormatException nfe){ System.out.println("Error: Incorrect Input"); System.exit(0); } if (user.equals(charArray1)){ user = user.toUpperCase(); } if (user.equals (charArray2)){ user = user.toUpperCase(); } if (user.equals (charArray3)){ user = user.toLowerCase(); } if (user.equals (charArray4)){ user = user.toLowerCase(); } state = state + user; if (user.equals(finsh)){ value = 1; } } System.out.println("You entered: " + state); } }
- 01-01-2011, 06:43 PM #2
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
Why are you trying to compare a string with an array of chars? Also, why do you even store the char arrays in the first place? Check out the API for the Character class, you'll find some usefull methods there that do exactely what you want.
Ever seen a dog chase its tail? Now that's an infinite loop.
- 01-01-2011, 06:43 PM #3
Senior Member
- Join Date
- Dec 2010
- Posts
- 100
- Rep Power
- 0
Hi - looking at the code, it seems like you are not comparing the user variable to the arrays correctly. Firstly, the IF statements you are using are incorrect because you cannot compare a character variable to an array object. You would have to compare the character to each element of the array. Secondly, I see that the user variable is a String, whereas you are trying to compare characters, i.e. your arrays are all character arrays. I suggest:
1. change user and finish variable to char
2. get rid of all the arrays
3. simply use Character.isUpperCase(user) and Character.isLowerCase(user) to determine the case of the characters.
4. Use Character.toUpperCase(user) and Character.toLowerCase(user) to change the case of each character.
Best,--user0--
- 01-01-2011, 06:44 PM #4
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
I was trying to teach the OP how to look up an API.
Ever seen a dog chase its tail? Now that's an infinite loop.
- 01-01-2011, 06:52 PM #5
Senior Member
- Join Date
- Dec 2010
- Posts
- 100
- Rep Power
- 0
@ m00nchile - the problem was simple enough that I thought I mention the easiest way to solve it. But point taken, you are absolutely right, there is no better way to learn than by reading API for yourself.
@ darkflame - here is a link to the Java API for your reference, if you don't already have it:
Java Platform SE 6
Best,--user0--
- 01-01-2011, 06:58 PM #6
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
Yea, I was just goin with the "give a man a fish, theach a man to fish" thing. Since this problem was pretty simplistic it was almost impossible to give a hint without giving the solution, so I directed the OP to the APIs.
Ever seen a dog chase its tail? Now that's an infinite loop.
- 01-01-2011, 07:06 PM #7
Member
- Join Date
- Jan 2011
- Posts
- 3
- Rep Power
- 0
yah i'm new to java, and i'm not all that good at it
- 01-01-2011, 07:17 PM #8
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
Don't worry about that, everyone started from nothing. I just tried to show you a tool that can help you learn much quicker. Whenever you find yourself doing a problem, it's not a bad idea to look it up in the API, there's almost certainly a method already made to ease the solution. For example, if you're doing something with strings, just type java string in google, and there you'll find Strings methods and descriptions of their functionality.
EDIT: I just reread my previous post and it is a bit derogatory. Sorry, that was not my intention.Ever seen a dog chase its tail? Now that's an infinite loop.
- 01-01-2011, 07:22 PM #9
Member
- Join Date
- Jan 2011
- Posts
- 3
- Rep Power
- 0
I understand =D Thanks, and it dosent help that i'm the only one in my class who's taking Comp. Sci. 120, and that my teacher admited that i'm farther then he is in java... so my frist thaught was to go ask for help from someone who knows. (and that its winter break and I can't get his help, not that he really helps me)
Similar Threads
-
JButton dosent align center
By karq in forum AWT / SwingReplies: 2Last Post: 10-06-2010, 08:08 AM -
Why dosent my test run?!?
By karq in forum New To JavaReplies: 5Last Post: 08-27-2010, 10:50 PM -
Why dosent return new string
By karq in forum New To JavaReplies: 8Last Post: 08-04-2010, 08:51 AM -
why dosent the 2d array change?
By TheBreadCat in forum New To JavaReplies: 11Last Post: 05-15-2010, 01:20 PM -
the code dosent work
By pcman in forum Java 2DReplies: 1Last Post: 03-20-2008, 08:20 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks