Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: if statement - might have problem with char to String casting?

  1. #1
    Junior Member
    Join Date
    Jul 2014
    Posts
    29
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default if statement - might have problem with char to String casting?

    import java.io.IOException;
    import java.util.*;
    public class Guesser {
    	public static void main(String[] args) throws IOException {
     
    		char[] alphabet = "abcdefghijklmnopqrstuvwxyz1234567890 .,:;'-".toCharArray();
    		Scanner sc = new Scanner(System.in);
    		System.out.println("Input three characters"); // gather three letter word to be matched with randomly generated chars
    		String characters = sc.next();
     
     
    	for (int i=0; ; i++) {
     
    			int x = (int) (Math.random() * alphabet.length);  // randomly generate a char from the char array
    			int y = (int) (Math.random() * alphabet.length);
    			int z = (int) (Math.random() * alphabet.length);
     
    			char letterOne = (alphabet[x]);
    			String firstPlace = String.valueOf(letterOne); // convert generated char to string for concatenation
    			char letterTwo =(alphabet[y]);
    			String secondPlace = String.valueOf(letterTwo);
    			char letterThree =(alphabet[z]); 
    			String thirdPlace = String.valueOf(letterThree);
    			System.out.println(firstPlace + secondPlace + thirdPlace);
     
    			if (characters == (firstPlace + secondPlace + thirdPlace )) {
     
    				System.out.println("That took " + i + " tries." );
    				break; // stop the program when it finds the inputted string
    			}
     
     
    		}
     
    	}
    }

    I'm writing a program which will take a three letter word (for now) and then try to guess the word over and over again until it finds it, then print the word and the amount of tries it took to find it.

    The problem: at the moment the program will find the word but not break out of the for loop when it does. I think it doesn't like the char to String conversion somewhere along the line. There must be a quick fix that I'm missing.


  2. #2
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    734
    Thanks
    0
    Thanked 133 Times in 116 Posts

    Default Re: if statement - might have problem with char to String casting?

    Strings can not be compared using the "==" operator. If you want to compare Strings you have to use the equals() method.

  3. The Following User Says Thank You to Cornix For This Useful Post:

    BenjaminJ (August 5th, 2014)

  4. #3
    Junior Member
    Join Date
    Jul 2014
    Posts
    29
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: if statement - might have problem with char to String casting?

    Oh! Excellent. That totally slipped my mind. Why is not giving me an error, in that case? Is there any way of modifying this code so the user can input a string of any length they choose? I assume that would mean I would have to either a) create infinite variables or b) create user generated variables (I don't know if they exist)
    Last edited by BenjaminJ; August 3rd, 2014 at 08:57 AM.

  5. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,771
    My Mood
    Mellow
    Thanks
    234
    Thanked 715 Times in 701 Posts

    Default Re: if statement - might have problem with char to String casting?

    Is there any way of modifying this code so the user can input a string of any length they choose?
    Yes, that is possible, and No you wouldn't create infinite variables. You can create an array of the required length after the length of the String is known or you could use an ArrayList which will grow as needed. StringBuilder might also be a useful tool as you go through the "guessing" part, but I'm not sure.

  6. #5
    Junior Member
    Join Date
    Jul 2014
    Posts
    29
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: if statement - might have problem with char to String casting?

    So I'd have the array which would have a .length as the amount of items as chars inputted by the user? Then what would I do with that array? Surely there'd be an amount of possibilities which I'd need to account for without knowing what they are. Could you mock up a quick example of the array?

    Thanks

  7. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,771
    My Mood
    Mellow
    Thanks
    234
    Thanked 715 Times in 701 Posts

    Default Re: if statement - might have problem with char to String casting?

    Then what would I do with that array?
    The same as you were doing with 3 letters scaled up to the number of characters entered: test all possible permutations until the right one is found.
    Surely there'd be an amount of possibilities which I'd need to account for without knowing what they are.
    The number of permutations can be calculated for a specified number of items.
    Could you mock up a quick example of the array?
    No mocking necessary. The String class provides a method that returns an array containing the characters of the String object.

  8. The Following User Says Thank You to GregBrannon For This Useful Post:

    BenjaminJ (August 5th, 2014)

  9. #7
    Member jdv's Avatar
    Join Date
    Jul 2014
    Location
    This Land
    Posts
    74
    Thanks
    0
    Thanked 6 Times in 6 Posts

    Default Re: if statement - might have problem with char to String casting?

    Quote Originally Posted by BenjaminJ View Post
    Why is not giving me an error, in that case?
    Because it isn't an error. The "==" operator is perfectly valid in this context because it is comparing identities, not equality. (Even if this is not what you want to do at runtime.) Sometimes we don't need true equality, and sometimes we do. This is why Object.equals() can be overridden as it is for String.

    Nice ref: java - What is the difference between identity and equality in OOP? - Stack Overflow

  10. The Following User Says Thank You to jdv For This Useful Post:

    BenjaminJ (August 5th, 2014)

  11. #8
    Junior Member
    Join Date
    Jul 2014
    Posts
    29
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: if statement - might have problem with char to String casting?

    Quote Originally Posted by GregBrannon View Post
    The String class provides a method that returns an array containing the characters of the String object.
    Would you mind naming the method? I'm only just starting to learn the String class.

    And thanks jdv for the information, much appreciated.

  12. #9
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,549
    My Mood
    Grumpy
    Thanks
    0
    Thanked 169 Times in 160 Posts

    Default Re: if statement - might have problem with char to String casting?

    How about going to the Java API and looking at all the methods and see which one returns a char array.
    Improving the world one idiot at a time!

Similar Threads

  1. Sequences (convert char[] to String) [Replacing String with Character]
    By tpolwort in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 25th, 2013, 02:56 PM
  2. [SOLVED] Please help me with my String variable and switch statement problem.
    By ace1 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: August 7th, 2013, 09:15 PM
  3. Replies: 3
    Last Post: March 23rd, 2013, 07:20 PM
  4. Get int value from char, char pulled from String
    By Andrew Red in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 4th, 2013, 09:04 AM
  5. [SOLVED] Java casting/combining char
    By maple1100 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 27th, 2012, 09:16 PM

Tags for this Thread