4 Replies - 402 Views - Last Post: 04 October 2012 - 08:49 PM Rate Topic: -----

#1 cyimking  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 04-October 12

Exit out of while loop.

Posted 04 October 2012 - 08:25 PM

Hello,

So im working on a project that will encrypt code based upon the number that the user submits. (WORKS). The issue is that the program have to exit if the user import 'q' ON the first option... (Where the program asks the user for an int value)

import java.util.Scanner;

public class Encryption {

	/**
	 *
	 * Description: This class prompts the user for key and message, performs the encryption, and prints the encrypted message
     * out to the screen.
     * Date: 10/4/2012
	 */
		
		   public Encryption()
		   {
			   //Empty Constructor
		   }

		
		public void run()
		{

			//Allow the user to input.
			Scanner scan = new Scanner(System.in);
			
			//Variables
			String str = "";
			int count,count1 = 0;
			char code;
			
			//While loop to run code
			while(true)
			{	
			
				
			System.out.println("\nPlease enter the encryption key you would like to use The key (should be a number between 1 and 26): ");
			count = scan.nextInt(); //Error here when importing q. How can i input q without generating an error
			
				
			//Need it so it can buffer the Int
			scan.nextLine();	
				
		    //Ask users to input a code, 
			System.out.println("\nPlease enter a code: ");
			str = scan.nextLine();
			 
			for(int x = 0; x < str.length(); x++){
			
			//Convert the string into Char, so i can encrypt the code.	
			code = str.charAt(x);
			
			//If the code is uppercase then run the forumala, so the after Z it will go to A.
			if(Character.isUpperCase(code))
			{
				count1 = (char)('A' +(code - 'A' + count) % 26);
			}
			
			//Won't shift any symbols
			else if(code != ' ' && code != ',' && code != '.' && code != ':' && code != '\'' && code != '\"' && code != '!'){
			//After the letter 'z' the code would go to 'a'	
			count1 = (char)('a' +(code - 'a' + count) % 26);	
			}
			
			else if (code == ',')
			    count1 = ',';
			
			else if (code == '.')
			    count1 = '.';
			
			else if (code == ':')
			    count1 = ':';
			
			else if (code == '\'' )
			    count1 = '\'';
			
			else if (code == '\"' )
			    count1 = '\"';
			
			else if (code == '!' )
			    count1 = '!';
			
			else
				count1 = ' ';
			
			//Print the encypted code
			System.out.print((char)count1);
			}
			
			}
		
	}

}




Is This A Good Question/Topic? 0
  • +

Replies To: Exit out of while loop.

#2 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 851
  • View blog
  • Posts: 2,599
  • Joined: 29-July 11

Re: Exit out of while loop.

Posted 04 October 2012 - 08:33 PM

while user input != 'q', execute loop.
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Exit out of while loop.

Posted 04 October 2012 - 08:35 PM

This is quite ridiculous code

			else if (code == ',')
			    count1 = ',';
			
			else if (code == '.')
			    count1 = '.';
			
			else if (code == ':')
			    count1 = ':';
			
			else if (code == '\'' )
			    count1 = '\'';
			
			else if (code == '\"' )
			    count1 = '\"';
			
			else if (code == '!' )
			    count1 = '!';
			
			else
				count1 = ' ';


I am sure you can come with a more intelligent way, even if it is a switch with 6 case with fall through over each othe :^:
Was This Post Helpful? 0
  • +
  • -

#4 cyimking  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 04-October 12

Re: Exit out of while loop.

Posted 04 October 2012 - 08:37 PM

View Postfarrell2k, on 04 October 2012 - 08:33 PM, said:

while user input != 'q', execute loop.


while counts != 'q'? Is yes, then it still generate an error.
Was This Post Helpful? 0
  • +
  • -

#5 cyimking  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 04-October 12

Re: Exit out of while loop.

Posted 04 October 2012 - 08:49 PM

View Postpbl, on 04 October 2012 - 08:35 PM, said:

This is quite ridiculous code

			else if (code == ',')
			    count1 = ',';
			
			else if (code == '.')
			    count1 = '.';
			
			else if (code == ':')
			    count1 = ':';
			
			else if (code == '\'' )
			    count1 = '\'';
			
			else if (code == '\"' )
			    count1 = '\"';
			
			else if (code == '!' )
			    count1 = '!';
			
			else
				count1 = ' ';


I am sure you can come with a more intelligent way, even if it is a switch with 6 case with fall through over each othe :^:


Well when i use a switch statement the code doesn't function. Plus the program isn't a large program, although i know using a switch statement would be good practice.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1