help with error output for do while loop

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 1542 Views - Last Post: 21 March 2013 - 08:39 PM Rate Topic: -----

#1 LawlessBaron  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 115
  • Joined: 07-February 13

help with error output for do while loop

Posted 07 March 2013 - 07:11 PM

Hi as you can see i am doing an exercise that when a user enters the wrong password it will simply prompt the user for another entry of there password but i am looking for a possible error display/output for the do while loop or completely take it out of the do while loop if necessary.

I was thinking of something with a similar syntax to the following code 1 and code 2 if the code needing a possible error output for the user prompting them to renter there password,

code 1 possible use for error output
System.out.println("Please enter invoice number: ");
		invoice = sc.nextInt();
		
		//1st do while loop start
		do {
			System.out.println("The number " + invoice + " you have entered is invalid");
			System.out.println("Please enter a valid number between 1000 - 8000 to proceed");
			invoice = sc.nextInt();
		}
		while (invoice < 1000 && 8000 > invoice);


Code 2 that needs error output
import java.util.Scanner;

public class Password {
	public static void main(String[] args) {
		int number = 0;
		int letter = 0;
		String pass1 = "";
		String pass2;
		int size = pass1.length();
		
		Scanner user = new Scanner(System.in);
		
		do {
			System.out.println("Please enter password: ");
			pass1 = user.next();
			size = pass1.length();
			
			for (int i = 0; i < size; i++) {
				char ch = pass1.charAt(i);
				
				if (Character.isDigit(ch)) {
					number++;
				}
			
				if(Character.isLetter(ch))
				letter++;
			}
		} while (!(size >= 6 && size <= 10 && number >= 1 && letter >= 1));


error output fix for code 2 but i need to make the error loop return to the line to ask for the password again

import java.util.Scanner;

public class Password {
	public static void main(String[] args) {
		int number = 0;
		int letter = 0;
		String pass1 = "";
		String pass2;
		int size = pass1.length();
		
		Scanner user = new Scanner(System.in);
		
		if (!(size >= 6 && size <= 10 && number >= 1 && letter >= 1)); {
			System.out.println("Please enter password: ");
			pass1 = user.next();
			size = pass1.length();
		}
			
		do {
			System.out.println("Error occured. Please try again");
			
			break;
		} while (!(size >= 6 || size <= 10 && number >= 1 || letter >= 1));
		
			for (int i = 0; i < size; i++) {
				char ch = pass1.charAt(i);
				
				if (Character.isDigit(ch)) {
					number++;
				}
			
				if(Character.isLetter(ch))
				letter++;
			}
		// } while (!(size >= 6 && size <= 10 && number >= 1 && letter >= 1));	
	}
}

This post has been edited by LawlessBaron: 07 March 2013 - 07:16 PM


Is This A Good Question/Topic? 0
  • +

Replies To: help with error output for do while loop

#2 flareback  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 30
  • Joined: 27-February 13

Re: help with error output for do while loop

Posted 07 March 2013 - 07:45 PM

while I do like the do-while loop. I'm not sure it's the best option here. why not do:
while(true) {
   // get password here

   if (/* check for valid password here */) {
      break;
   }

   // print error code here.
}



This will continue to loop until a valid password is entered.
Was This Post Helpful? 0
  • +
  • -

#3 LawlessBaron  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 115
  • Joined: 07-February 13

Re: help with error output for do while loop

Posted 07 March 2013 - 07:59 PM

Thanks man im just trying it now and fixing a few errors just after the if an break

Password.java:40: error: <identifier> expected
			System.out.println("Error occured. Please try again");
			                  ^
Password.java:40: error: illegal start of type
			System.out.println("Error occured. Please try again");

Was This Post Helpful? 0
  • +
  • -

#4 LawlessBaron  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 115
  • Joined: 07-February 13

Re: help with error output for do while loop

Posted 10 March 2013 - 07:34 PM

this is a result that i have come up with


import javax.swing.*;

public class Password {
 	public static void main(String[] args) {
		int number = 0;
		int letter = 0;
		String pass1 = "";
		String passRepeat;
		boolean numberIs = false;
		boolean letterIs = false;
		char charChecking = '\u0000';
		char charChecked;
		int size = pass1.length();	
		
		
		pass1 = JOptionPane.showInputDialog("Please enter your password: ");
		
			for (int i = 0; i < size; i++) {
				charChecking = pass1.charAt(i);
				
				if (Character.isDigit(charChecking)) {
				
					numberIs = true;
				}
			}
			
			if (numberIs == false) {
				JOptionPane.showInputDialog(null, "Number is missing. Please enter new password. ");
			}
			
			for (int i = 0; i < size; i++) {
				charChecking = pass1.charAt(i);

				letterIs = true;
			}
			
			if (letterIs == false) {
			
				JOptionPane.showInputDialog(null, "Letter is missing. Please enter a new password.");
			}
			
			if (size < 6 || size > 10) {
			
				passRepeat = JOptionPane.showInputDialog(null, "Please enter password again");
			}
			
			do {
			
				passRepeat = JOptionPane.showInputDialog(null, "Paswords do not match. Please re-enter password");
				
			} while(!(passRepeat.equals(pass1)));
			
			if (passRepeat.equals(pass1)) {
				JOptionPane.showInputDialog (null, "Your password has been confirmed");
 			}
	}
}


it doesn't output the errors properly

This post has been edited by LawlessBaron: 10 March 2013 - 07:37 PM

Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10341
  • View blog
  • Posts: 38,268
  • Joined: 27-December 08

Re: help with error output for do while loop

Posted 10 March 2013 - 09:26 PM

You need to be more specific about the problems or errors you are encountering.
Was This Post Helpful? 0
  • +
  • -

#6 LawlessBaron  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 115
  • Joined: 07-February 13

Re: help with error output for do while loop

Posted 14 March 2013 - 06:17 PM

It does error output but the issue is that when its correct it outputs the errors anyway so i need a line to verify that it has to be the certain perimeters so when it gets to the error looping it skips past and finishes if its correct
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

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

Reputation: 8323
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: help with error output for do while loop

Posted 14 March 2013 - 07:41 PM

Write a boolean function and exit with false when not valid

   boolean isValid(String pwd) {
       int len = pwd.length();

       // test length
       if(pwd.len < 10) {
          JOptionPane(.... not long enough
          return false;
       }

       // test at least one number
       int i;
       for(i = 0; i < len; ++i) {
         character digit = pwd.charAt(i);
         if(digit >= '0' && digit <= '9')
            break;
       }
       if(i == len) {
          JOptionPane(... no numeric digit
          return false;
       }

       // test at least one lower case
       int i;
       for(i = 0; i < len; ++i) {
         character digit = pwd.charAt(i);
         if(digit >= 'a' && digit <= 'z')
            break;
       }
       if(i == len) {
          JOptionPane(... no lowercase letter
          return false;
       }

       ....

       // all tests done
       return true;
    }


Was This Post Helpful? 1
  • +
  • -

#8 LawlessBaron  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 115
  • Joined: 07-February 13

Re: help with error output for do while loop

Posted 17 March 2013 - 05:17 PM

View Postpbl, on 15 March 2013 - 10:41 AM, said:

Write a boolean function and exit with false when not valid

   boolean isValid(String pwd) {
       int len = pwd.length();

       // test length
       if(pwd.len < 10) {
          JOptionPane(.... not long enough
          return false;
       }

       // test at least one number
       int i;
       for(i = 0; i < len; ++i) {
         character digit = pwd.charAt(i);
         if(digit >= '0' && digit <= '9')
            break;
       }
       if(i == len) {
          JOptionPane(... no numeric digit
          return false;
       }

       // test at least one lower case
       int i;
       for(i = 0; i < len; ++i) {
         character digit = pwd.charAt(i);
         if(digit >= 'a' && digit <= 'z')
            break;
       }
       if(i == len) {
          JOptionPane(... no lowercase letter
          return false;
       }

       ....

       // all tests done
       return true;
    }


Thanks man sorry for the noobyness of my Java skill its easy to pickup but putting alot of code into action is a bit difficult i am finding C# to be bit more enjoyable
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

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

Reputation: 8323
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: help with error output for do while loop

Posted 17 March 2013 - 05:23 PM

View PostLawlessBaron, on 17 March 2013 - 08:17 PM, said:

Thanks man sorry for the noobyness of my Java skill its easy to pickup but putting alot of code into action is a bit difficult i am finding C# to be bit more enjoyable

If you think so... nobody stop you from using it
Was This Post Helpful? 0
  • +
  • -

#10 LawlessBaron  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 115
  • Joined: 07-February 13

Re: help with error output for do while loop

Posted 17 March 2013 - 05:28 PM

View Postpbl, on 18 March 2013 - 08:23 AM, said:

View PostLawlessBaron, on 17 March 2013 - 08:17 PM, said:

Thanks man sorry for the noobyness of my Java skill its easy to pickup but putting alot of code into action is a bit difficult i am finding C# to be bit more enjoyable

If you think so... nobody stop you from using it

I have to use Java for this i have to do a chapter out of the Joyce Farrell 6th edition textbook
Was This Post Helpful? 0
  • +
  • -

#11 pbl  Icon User is offline

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

Reputation: 8323
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: help with error output for do while loop

Posted 17 March 2013 - 05:31 PM

Sorry, I am not responsible of your curriculum content
Was This Post Helpful? 0
  • +
  • -

#12 LawlessBaron  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 115
  • Joined: 07-February 13

Re: help with error output for do while loop

Posted 17 March 2013 - 05:47 PM

No worries man thanks for the help mate really appreciated :)
Was This Post Helpful? 0
  • +
  • -

#13 LawlessBaron  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 115
  • Joined: 07-February 13

Re: help with error output for do while loop

Posted 20 March 2013 - 12:17 AM

so my code is fine i need to put in a bit where the user must re-enter there password and it matches both the entries together
so basically equal i have it figured it needs work so once they equal it exits nothing more but i get an issue with charAt

/**
 * import javax.swing.*; to import JOptionPane with the * as a wildcard
 * it will allow any other API within javax.swing to be available to the programmer
 * if i were to just use javax.swing.JoptionPane; JOption would be the only packet avaliable
 * @see Password
 */
import javax.swing.*;

public class Password {
	public static void main(String[] args) {
		int letter = 0;			
		int digit = 0;
		String pass1 = "";
		String pass2 = "";
		int length = pass2.length();
		int count;

		do { // start of do while loop
			
			pass1 = JOptionPane.showInputDialog("Please enter your password");
			
			pass2 = JOptionPane.showInputDialog("Please re-enter password");
			length = pass2.length();
			
			if (!(length >= 6 && length <= 10)) {
				JOptionPane.showMessageDialog(null, "Password must be 6 short or 10 long", "Not long enough", JOptionPane.ERROR_MESSAGE);
			} 
			/**
			 * For loop for testing if a digit between 0-9 exists
			 * followed by an if statement to initiate testing for a digit
			 * once testing is complete a break; is initiated to break out of the loop and continue testing
			 * same applies to testing for a letter character
			 */
			int i;
			for (i = 0; i < pass2.length(); ++i) { // for loop checking for number  between 0 and 9 to make sure no error output if to short
				char ch = pass2.charAt(i);
				if (ch >= '0' && ch <= '9') { // if digit exists just continue with testing until true
					pass2 = "";
					break;
				} else {
				JOptionPane.showMessageDialog(null, "Not number");
					pass2 = "";
					break;
				}
			}		
		
			// if (i == length) {
// 				JOptionPane.showMessageDialog(null ,"No digit has been entered", "No Digit Error", JOptionPane.ERROR_MESSAGE);
// 			}
				
			for (i = 0; i < length; ++i) { // For loop checking for letters between A - Z
				char ch = pass2.charAt(i);
				if (ch >= 'a' && ch <= 'z') { // if there isn't any letters then it will return error stating no letter
					pass2 = "";
					break;
				}
			 	if (ch >= 'A' && ch <= 'Z') { // option to check for capital letters to prevent error
					count = length;
					pass2 = "";
					break;
				}	
			}
				
			if (i == length) {
				JOptionPane.showMessageDialog(null, "No letter has been entered", "No letter Error", JOptionPane.ERROR_MESSAGE);
			}
			if (!pass1.equals(pass2)) {
				JOptionPane.showMessageDialog(null, "Password is correct", "Password Correct", JOptionPane.INFORMATION_MESSAGE);
			}
		}	while (!(length >= 6 && length <= 10 || digit >= 1 && letter >= 1)); // End of do while loop
	} // Main method end
} // Class end

Was This Post Helpful? 0
  • +
  • -

#14 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2197
  • View blog
  • Posts: 5,224
  • Joined: 10-September 10

Re: help with error output for do while loop

Posted 20 March 2013 - 09:41 AM

Please use punctuation, nouns, verbs, etc. What you wrote is an uninterrupted stream of (barely) consciousness that makes little sense. I'm not sure if you still need help, and if you do need help, what help you need. Describe what you need help with, ask questions. Sample runs, actual and desired, are also helpful, if possible.
Was This Post Helpful? 0
  • +
  • -

#15 pbl  Icon User is offline

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

Reputation: 8323
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: help with error output for do while loop

Posted 20 March 2013 - 06:47 PM

there is a continue statement in Java
if a condition fails (like the length) simply continue; that will bring the next loop of the do/while

Personnaly for that type of thing i porfer to do a

while(true) {

and break out of it when all conditions have been met

    while(true) {

       input password

       if(not good length) {
         display error
         continue;
       }

       if(not a letter) {
         display error
         continue;
       }
       ....
       break;
     } // end of while


Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2