# Question about using loops and if statements to solve

Page 1 of 1

## 8 Replies - 2375 Views - Last Post: 14 October 2012 - 07:51 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=295258&amp;s=5841ef51d30d0410b3d2052771d9f6ae&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 andreajava

Reputation: 0
• Posts: 5
• Joined: 11-October 12

# Question about using loops and if statements to solve

Posted 11 October 2012 - 06:04 PM

I need help to understand how to finish this code. It is a password checker. However it doesn't even verify that the length is correct. I could use some help with where I'm going or what I'm doing wrong. It doesn't give me any errors it simply just doesn't perform the task.
```
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String s = input.next();

private static boolean safe(String s) {

int counter = 0;

boolean digitFound = false;
boolean lowerCaseFound = false;
boolean upperCaseFound = false;
boolean spaceChar = false;

if (s.length() < 8) {
return false;
}
while(counter < s.length()) {

char c = s.charAt(counter);
if (Character.isDigit(c)) {
digitFound = true;
}
counter++;
}

counter = 0;
while(counter < s.length()) {

char c = s.charAt(counter);

if (Character.isLowerCase(c)) {
lowerCaseFound = true;
}
counter++;
}

counter = 0;
while(counter < s.length()) {

char c = s.charAt(counter);
if (Character.isUpperCase(c)) {
upperCaseFound = true;
}
counter++;
}

counter = 0;
while(counter < s.length()) {

char c = s.charAt(counter);
if (Character.isSpaceChar(c)) {
spaceChar = true;
}
counter++;
return true;
}
return false;
}
}

```

This post has been edited by baavgai: 12 October 2012 - 06:03 AM
Reason for edit:: tagged

Is This A Good Question/Topic? 0

## Replies To: Question about using loops and if statements to solve

### #2 Ytry

Reputation: 16
• Posts: 120
• Joined: 25-July 12

## Re: Question about using loops and if statements to solve

Posted 11 October 2012 - 06:05 PM

### #3 andreajava

Reputation: 0
• Posts: 5
• Joined: 11-October 12

## Re: Question about using loops and if statements to solve

Posted 11 October 2012 - 06:16 PM

I'm really not sure what that means because I don't do forums. I apologize if this is wrong.

```public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String s = input.next();
/**
* Utility method that tells if a given string is a safe password.
* @param s the string to be tested
* @return true if s is safe and false if not
*/

private static boolean safe(String s) {

int counter = 0;
/**
* This is declaring digitFound. It is a boolean data type
* and I am assigning it to false because  it has to assume its false
* for it to check it to be true.
*/
boolean digitFound = false;
boolean lowerCaseFound = false;
boolean upperCaseFound = false;
boolean spaceChar = false;

if (s.length() < 8) {
return false;
}
while(counter < s.length()) {

char c = s.charAt(counter);
if (Character.isDigitİ) {
digitFound = true;
}
counter++;
}

counter = 0;
while(counter < s.length()) {

char c = s.charAt(counter);

if (Character.isLowerCaseİ) {
lowerCaseFound = true;
}
counter++;
}

counter = 0;
while(counter < s.length()) {

char c = s.charAt(counter);
if (Character.isUpperCaseİ) {
upperCaseFound = true;
}
counter++;
}

counter = 0;
while(counter < s.length()) {

char c = s.charAt(counter);
if (Character.isSpaceCharİ) {
spaceChar = true;
}
counter++;
return true;
}
return false;
}
}
```

### #4 andreajava

Reputation: 0
• Posts: 5
• Joined: 11-October 12

## Re: Question about using loops and if statements to solve

Posted 11 October 2012 - 06:37 PM

Instead of return true I think it has to return as safe and this has to be in the main method? I got this far but now I'm just lost. I could type in "hi" and it tells me it works. I need it to tell me this password isn't acceptable. Which I thought where I had, length < 8 return false, would do this but it hadn't.

### #5 Xente

Reputation: 3
• Posts: 36
• Joined: 31-January 12

## Re: Question about using loops and if statements to solve

Posted 11 October 2012 - 07:16 PM

Indent your code...it helps a lot, especially in times like these where you're trying to find a bug in the program. The problem is with the logic, if a string is less than eight characters long it returns false, but the last return statement returns false as well. The return true statement is only in the while loop. At least that's what I can see.

Also, a better return statement would be

```return (digitFound && lowerCaseFound && upperCaseFound && isSpaceChar);
```

If all the booleans are true, it will return true.

### #6 andreajava

Reputation: 0
• Posts: 5
• Joined: 11-October 12

## Re: Question about using loops and if statements to solve

Posted 11 October 2012 - 08:01 PM

Okay here is what I have I fixed it up some. Though it's still not working. I'm truly a beginner so understanding some of the logic behind java is difficult.

```package passwordchecker;

import java.util.Scanner;

/**
*
* @author Andrea
*/

public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String s = input.next();

}
/**
* Utility method that tells if a given string is a safe password.
* @param s the string to be tested
* @return true if s is safe and false if not
*/

private static boolean safe(String s) {

int counter = 0;

/**
* This is declaring digitFound. It is a boolean data type
* and I am assigning it to false because  it has to assume its false
* for it to check it to be true.
*/
boolean digitFound = false;
boolean lowerCaseFound = false;
boolean upperCaseFound = false;
boolean spaceCharFound = false;

if (s.length() < 8) {
return false;

}
while(counter < s.length()) {

char c = s.charAt(counter);
if (Character.isDigit(c)) {
digitFound = true;
}
counter++;
}

counter = 0;
while(counter < s.length()) {

char c = s.charAt(counter);

if (Character.isLowerCase(c)) {
lowerCaseFound = true;
}
counter++;
}

counter = 0;
while(counter < s.length()) {

char c = s.charAt(counter);
if (Character.isUpperCase(c)) {
upperCaseFound = true;
}
counter++;
}

counter = 0;
while(counter < s.length()) {

char c = s.charAt(counter);
if (Character.isSpaceChar(c)) {
spaceCharFound = true;
}
counter++;
return (digitFound && lowerCaseFound
&& upperCaseFound && spaceCharFound);

}
System.out.print("Password invaild. Try again:" + input);
}
}
```

### #7 Xente

Reputation: 3
• Posts: 36
• Joined: 31-January 12

## Re: Question about using loops and if statements to solve

Posted 11 October 2012 - 10:01 PM

Did you try compiling the code? When I compile it I get errors...

### #8 pbl

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

Reputation: 8370
• Posts: 31,956
• Joined: 06-March 08

## Re: Question about using loops and if statements to solve

Posted 12 October 2012 - 05:56 AM

### #9 andreajava

Reputation: 0
• Posts: 5
• Joined: 11-October 12

## Re: Question about using loops and if statements to solve

Posted 14 October 2012 - 07:51 AM

I've been working on this and still can't get it to work. Any suggestions or ideas about something I should look to add or edit? Thank you

```package passwordchecker;

import java.util.Scanner;

/**
*
* @author Andrea Dietrich
* @version Oct 12, 2012
*/

public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String s = input.next();

}
/**
* Utility method that tells if a given string is a safe password.
* @param s the string to be tested
* @return true if s is safe and false if not
*/

private static boolean safe(String s) {
int counter = 0;

if (s.length() < 8) {
return false;
}
while (!safe(s)) {
return false;
}

boolean digitFound = false;
boolean lowerCaseFound = false;
boolean upperCaseFound = false;
boolean spaceCharFound = false;

while(counter < s.length()) {

char c = s.charAt(counter);
if (Character.isDigit(c)) {
digitFound = true;
}
counter++;
}

counter = 0;
while(counter < s.length()) {

char c = s.charAt(counter);
if (Character.isLowerCase(c)) {
lowerCaseFound = true;
}
counter++;
}

counter = 0;
while(counter < s.length()) {

char c = s.charAt(counter);
if (Character.isUpperCase(c)) {
upperCaseFound = true;
}
counter++;
}

counter = 0;
while(counter < s.length()) {

char c = s.charAt(counter);
if (Character.isSpaceChar(c)) {
spaceCharFound = true;
}
counter++;