By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,558 Members | 1,210 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,558 IT Pros & Developers. It's quick & easy.

Help, I am having problems with loops.

P: 3
I need help with a program I am writing for school. The program has to do the following:
/*
1. Character string must be a minimium of 15 charactors.

2. If not 15 characters long it must give error and send you back to orginal question.

3. After entering 15 characters it must count the CAPTIAL letters and tell you how many there are.

4. Need a loop to count and produce error and take you back to first screen.
*/

My code is below. It does almost ever thing I need it to. EXCEPT a loop after you NOT entering aleast 15 characters. I know it something simple and I am confussed on Nested Loops. I have been reading and reading for days. I have almost 30 hours into this, (that is including all the reading). I know this is simple, it is just the light bulb has not come on in my head yet. Can some one help me with the part I am missing. If you enter the correct amount of characters it works. The loop for the error is not working correctly.

Please help!!!!

import javax.swing.*;
import java.text.*;

public class Characters1
{
public static void main (String [] args)
{
int count = 0;
char c1 = 0;
String response = null;
boolean b1 = Character.isUpperCase(c1);

while(true)
{
response = JOptionPane.showInputDialog (null,
"Please Enter a sting of characters that is at least 15 characters long.");
{
if (response.length() < 15)
{
JOptionPane.showMessageDialog (null,"You did enter enought characters");
{
response = JOptionPane.showInputDialog (null,"Please Enter a sting of characters that is at least 15 characters long.");
}
}
int length = response.length();


{
if (length > 15)

for (int i=0; i<length; i++)
if (Character.isUpperCase(response.charAt(i)))
count++;


JOptionPane.showMessageDialog (null, "You entered "+length+" characters "+count+ " of them were captial letters");

System.exit(0); //Must use with JOptionPane

}
}
}}
}



Thanks for your help,
S Black
Nov 20 '06 #1
Share this Question
Share on Google+
2 Replies


10K+
P: 13,264
I need help with a program I am writing for school. The program has to do the following:
/*
1. Character string must be a minimium of 15 charactors.

2. If not 15 characters long it must give error and send you back to orginal question.

3. After entering 15 characters it must count the CAPTIAL letters and tell you how many there are.

4. Need a loop to count and produce error and take you back to first screen.
*/

My code is below. It does almost ever thing I need it to. EXCEPT a loop after you NOT entering aleast 15 characters. I know it something simple and I am confussed on Nested Loops. I have been reading and reading for days. I have almost 30 hours into this, (that is including all the reading). I know this is simple, it is just the light bulb has not come on in my head yet. Can some one help me with the part I am missing. If you enter the correct amount of characters it works. The loop for the error is not working correctly.

Please help!!!!

import javax.swing.*;
import java.text.*;

public class Characters1
{
public static void main (String [] args)
{
int count = 0;
char c1 = 0;
String response = null;
boolean b1 = Character.isUpperCase(c1);

while(true)
{
response = JOptionPane.showInputDialog (null,
"Please Enter a sting of characters that is at least 15 characters long.");
{
if (response.length() < 15)
{
JOptionPane.showMessageDialog (null,"You did enter enought characters");
{
response = JOptionPane.showInputDialog (null,"Please Enter a sting of characters that is at least 15 characters long.");
}
}
int length = response.length();


{
if (length > 15)

for (int i=0; i<length; i++)
if (Character.isUpperCase(response.charAt(i)))
count++;


JOptionPane.showMessageDialog (null, "You entered "+length+" characters "+count+ " of them were captial letters");

System.exit(0); //Must use with JOptionPane

}
}
}}
}



Thanks for your help,
S Black
Always a pleasure to help those that write their own code first.

Expand|Select|Wrap|Line Numbers
  1.  import javax.swing.*;
  2. import java.text.*; 
  3. public class Characters1 {
  4.  public static void main (String [] args) {
  5.   int count = 0;
  6.   char c1 = 0;
  7.   String response = null;
  8.   boolean correct = false;
  9.   while(!correct) {
  10.    response = JOptionPane.showInputDialog (null,
  11.    "Please Enter a sting of characters that is at least 15 characters long.");
  12.    if (response.length() < 15) {
  13.     JOptionPane.showMessageDialog (null,"You did not enter enough characters");
  14.    }
  15.    else {
  16.     correct = true;
  17.    }
  18.   }
  19.  
  20.   for (int i=0; i<response.length(); i++) {
  21.    if (Character.isUpperCase(response.charAt(i))) {
  22.     count++;
  23.    }
  24.   }
  25.   JOptionPane.showMessageDialog (null, "You entered "+response.length()+" characters "+count+ " of them were captial letters");
  26.   System.exit(0); //Must use with JOptionPane
  27.  }
  28. }
  29.  
  30.  
Nov 20 '06 #2

P: 3
Thanks so much for you help, I really needed it. I am going nuts with this.
Nov 21 '06 #3

Post your reply

Sign in to post your reply or Sign up for a free account.