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

Thread: Potentially broken switch statement or program logic.

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

    Default Potentially broken switch statement or program logic.

    The premise is a brute force search for an inputted string. Based on the length of the inputted string (1-4) the switch statement executes one of four blocks which should break when it finds the string.

    The problem is that it works for a single char just fine, but anything more than that it will execute multiple blocks and ignore my break statement.

    The code:

    import java.io.IOException;
    import java.util.*;
    public class Guesser {
    	public static void main(String[] args) throws IOException {
     
    		char[] alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890 .,?!:;'-".toCharArray();
    		Scanner sc = new Scanner(System.in);
    		System.out.println("Input 1-4 characters");
    		String characters = sc.next();
    		int len = characters.length();
     
     
    		switch (len) {
    		case 4: 
     
    	for (int i=0; ; i++) { // FOUR LETTER STRING
     
    			int w = (int) (Math.random() * alphabet.length);
    			int x = (int) (Math.random() * alphabet.length);
    			int y = (int) (Math.random() * alphabet.length);
    			int z = (int) (Math.random() * alphabet.length);
     
    			char letterOne = (alphabet[x]);
    			String firstPlace = String.valueOf(letterOne);
    			char letterTwo =(alphabet[y]);
    			String secondPlace = String.valueOf(letterTwo);
    			char letterThree =(alphabet[z]); 
    			String thirdPlace = String.valueOf(letterThree);
    			char letterFour =(alphabet[w]); 
    			String fourthPlace = String.valueOf(letterFour);
     
    			System.out.println(firstPlace + secondPlace + thirdPlace + fourthPlace);
     
    			if (characters.equals(firstPlace + secondPlace + thirdPlace + fourthPlace)) {
     
    				System.out.println("That took " + i + " tries." );
    				break;
     
    			}
     
     
    		}
     
     
     
    	case 3:
    		for (int i=0; ; i++) { // THREE LETTER STRING
    		int x = (int) (Math.random() * alphabet.length);
    		int y = (int) (Math.random() * alphabet.length);
    		int z = (int) (Math.random() * alphabet.length);
     
    		char letterOne = (alphabet[x]);
    		String firstPlace = String.valueOf(letterOne);
    		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.equals(firstPlace + secondPlace + thirdPlace)) {
     
    			System.out.println("That took " + i + " tries." );
    			break;
    		}
     
    		}
     
    		case 2:
    				for (int i=0; ; i++) { // TWO LETTER STRING
    					int x = (int) (Math.random() * alphabet.length);
    					int y = (int) (Math.random() * alphabet.length);
     
    					char letterOne = (alphabet[x]);
    					String firstPlace = String.valueOf(letterOne);
    					char letterTwo =(alphabet[y]);
    					String secondPlace = String.valueOf(letterTwo);
     
     
     
    					System.out.println(firstPlace + secondPlace);
     
    					if (characters.equals(firstPlace + secondPlace)) {
     
    						System.out.println("That took " + i + " tries." );
    						break;
     
    		}
     
    		}
    		case 1:
    			for (int i=0; ; i++) { // ONE LETTER STRING
    				int x = (int) (Math.random() * alphabet.length);
     
     
    				char letterOne = (alphabet[x]);
    				String firstPlace = String.valueOf(letterOne);
    				System.out.println(firstPlace);
     
    				if (characters.equals(firstPlace)) {
     
    					System.out.println("That took " + i + " tries." );
    					break;
     
     
    				}
     
    			}
     
    	//	default:
    	//		System.out.println("That's not 1-4 characters.");
    	//	break;
    	}
    }
    }

    It'd be a lot easier if I could break this down into separate methods and keep track of them, but when broken down the scope of the variables is out of range...

    Sample output (for inputted string '1'):

    1
    J
    c
    [...]
    O
    1
    That took 51 tries.

    Sample output (for inputted string 'ba'):

    Input 1-4 characters
    ab
    C.
    rv
    ?I
    ;H
    0L
    Uf
    Ug
    4r
    [...]
    K // here it starts outputting and searching single characters? Infinite loop.
    H
    8
    J
    6


  2. #2
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Potentially broken switch statement or program logic.

    Why do you not have a "break" at the end of your cases?
    Every case within a switch-case construct must always have a break at its end or otherwise the control flow will go through into the other blocks.

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

    BenjaminJ (August 10th, 2014)

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

    Default Re: Potentially broken switch statement or program logic.

    Yes, sorry, it was a case of curly brace confusion once again.

Similar Threads

  1. How to turn my If statement into a case/switch statement?
    By blobby404 in forum What's Wrong With My Code?
    Replies: 23
    Last Post: June 19th, 2014, 03:11 PM
  2. can i do this with switch statement?
    By m7abraham in forum Java Theory & Questions
    Replies: 1
    Last Post: March 6th, 2014, 02:58 PM
  3. [SOLVED] A Loop statement and a switch statement issue
    By sternfox in forum Loops & Control Statements
    Replies: 13
    Last Post: March 7th, 2013, 04:19 PM
  4. Replacing an If statement with a Switch statement
    By logi in forum Loops & Control Statements
    Replies: 9
    Last Post: February 4th, 2013, 12:21 AM
  5. Java Program Help Switch Statement
    By jwill22 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 11th, 2010, 12:31 AM