Results 1 to 9 of 9
  1. #1
    darkflame5019 is offline Member
    Join Date
    Jan 2011
    Posts
    3
    Rep Power
    0

    Default 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);
    
    	}
    
    }

  2. #2
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    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.

  3. #3
    user0 is offline Senior Member
    Join Date
    Dec 2010
    Posts
    100
    Rep Power
    0

    Default

    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--

  4. #4
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    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.

  5. #5
    user0 is offline Senior Member
    Join Date
    Dec 2010
    Posts
    100
    Rep Power
    0

    Default

    Quote Originally Posted by m00nchile View Post
    I was trying to teach the OP how to look up an API.
    @ 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--

  6. #6
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    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.

  7. #7
    darkflame5019 is offline Member
    Join Date
    Jan 2011
    Posts
    3
    Rep Power
    0

    Default

    yah i'm new to java, and i'm not all that good at it

  8. #8
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    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.

  9. #9
    darkflame5019 is offline Member
    Join Date
    Jan 2011
    Posts
    3
    Rep Power
    0

    Default

    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

  1. JButton dosent align center
    By karq in forum AWT / Swing
    Replies: 2
    Last Post: 10-06-2010, 09:08 AM
  2. Why dosent my test run?!?
    By karq in forum New To Java
    Replies: 5
    Last Post: 08-27-2010, 11:50 PM
  3. Why dosent return new string
    By karq in forum New To Java
    Replies: 8
    Last Post: 08-04-2010, 09:51 AM
  4. why dosent the 2d array change?
    By TheBreadCat in forum New To Java
    Replies: 11
    Last Post: 05-15-2010, 02:20 PM
  5. the code dosent work
    By pcman in forum Java 2D
    Replies: 1
    Last Post: 03-20-2008, 09:20 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •