newbie help needed with looping.

T

Trevnal

Hi,
I was hoping some one may be able to help me. I am trying to get a piece of
code to work, which I thought I had right. But as far as I can tell, it does
not do anything when I run it in jBuilder.
I want to make sure the string that is collected from Keyboard.getString is
a upper case letter and the total length of the string is 8 characters long.

int len;
boalean validName = true;

aa = Keyboard.getString();
while (!aa.equals("ZZZ"))
{
do
{
len = partNumber.length();
{
if (!len == 8)
{
validName = false
}
}
}
while (validName = false)

do
{
for (int index = 0; index <2; index ++)
{
if (aa.substring(index,index+1) <"A" \\
(aa.substring(index,index+1) >"Z")
{
validName = false;
}
}
}
while (validName = false)

do
{
for (int index = 1; index <3; index ++)
{
if (aa.substring(index,index+1) <"0" \\
(aa.substring(index,index+1) >"100")
{
validName = false;
}
}
}
while (validName = false)

partIn[index] = new Part();
partIn[index].setPartNumber(partNumber);
}

rest of code.

thanks
Hobbit
 
M

Mark Haase

Trevnal said:
Hi,
I was hoping some one may be able to help me. I am trying to get a piece of
code to work, which I thought I had right. But as far as I can tell, it does
not do anything when I run it in jBuilder.
I want to make sure the string that is collected from Keyboard.getString is
a upper case letter and the total length of the string is 8 characters long.

A better way to do this would be to use a Regex (regular expression).
String has a method

public boolean matches(String regex);

that would do the job. Use could use it as follows:

aa == Keyboard.getString();
validName = aa.matches("[A-Z]{8}") && (aa.length==8);

This would match a string that has 8 consecutive capitalized characters,
I think. The second half of the boolean expression ensures that the
string only has those 8 characters, since a regex will match any
substring as well.
 
T

Tor Iver Wilhelmsen

Trevnal said:
do
{
len = partNumber.length();
{
if (!len == 8)
{
validName = false
}
}
}
while (validName = false)

Apart from the = vs. == thingy, you never actually assign to
partNumber here, so either the loop exits at the first run, or it
enters an endless loop.
if (aa.substring(index,index+1) <"A" \\
(aa.substring(index,index+1) >"Z")

You cannot use < and > operators on strings. Use compareTo. Or use
charAt() instead of substring(), then you can compare to 'A' and 'Z'
using said:
if (aa.substring(index,index+1) <"0" \\
(aa.substring(index,index+1) >"100")

What does that mean?
 
?

.

Hi,
I was hoping some one may be able to help me. I am trying to get a piece of
code to work, which I thought I had right. But as far as I can tell, it does
not do anything when I run it in jBuilder.
I want to make sure the string that is collected from Keyboard.getString is
a upper case letter and the total length of the string is 8 characters long.

int len;
boalean validName = true;

I assume you typed this in by hand rather that copying and pasting the
code. boalean should be boolean.
aa = Keyboard.getString();

I think it is safe to assume that getString() is a static method in the
class Keyboard and it returns a String.
while (!aa.equals("ZZZ")) {

If the user enters "ZZZ" then we quit.
do {
len = partNumber.length();

I would assume that partNumber.length() returns the length of some string.
I would not assume it would change the length of partNumber. This is
important. See below.

Why the extra scope here? This seems pointless to me.
if (!len == 8) {
validName = false
}

If the length of partNumber is not 8 then validName is false. If the
length never changes (i.e. partNumber.length() does not change the length)
then this will either exit immediately or loop forever.
}
} while (validName = false)

Two problems. The = is for assignment. The == is for comparison. Second
problem, you need a semicolon at the end of the line.

Did you put some println() statements in the code to see if it got this
far? Just putting things like:

System.out.println("just before the IF");
and
System.out.println("at the bottom of the loop");

will help you see how things are running. If it prints out, over and over
then you know you have an endless loop.

Once you get this part of the code working, if you still have problems,
post the rest.
do {
for (int index = 0; index <2; index ++) {
if (aa.substring(index,index+1) <"A" \\
(aa.substring(index,index+1) >"Z") {
validName = false;
}
}
} while (validName = false)

do {
for (int index = 1; index <3; index ++) {
if (aa.substring(index,index+1) <"0" \\
(aa.substring(index,index+1) >"100") {
validName = false;
}
}
} while (validName = false)

partIn[index] = new Part();
partIn[index].setPartNumber(partNumber);
}

rest of code.

thanks
Hobbit
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top