This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Array without ints Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Array without ints" Watch "Array without ints" New topic
Author

Array without ints

eric elysia
Ranch Hand

Joined: Mar 06, 2005
Posts: 70
Hello,

I am working on a program and I am trying to figure out how to make sure there are no integers input into a character array.

I do understand that I need to step through each index of the array. I am just not sure how to test to make sure each index does not have an integer value.

Thank you,
Eric
Jon Egan
Ranch Hand

Joined: Mar 24, 2004
Posts: 83
Since an int and a char are both just integral primitives in Java, within the range of a character's valid values, they are indistinguishable... except for the type of the variable. If the array is defined to be a char[], anything that the compiler KNOWS to be an int will give a compile error if you try to assign it directly into one of the char's in the array (without a cast - but a cast seems counter to your stated goal).

Anything the compiler sees as possibly being a char (like a char that has some math operation like multiplication, that would take it out of char range, will just lose precision and have only a min/max value of Character.MIN_VALUE and Character.MAX_VALUE. So once they're in the array, there's no way of knowing they were once int's.

-- Jon
eric elysia
Ranch Hand

Joined: Mar 06, 2005
Posts: 70
Ah ha. You make a good point. I need to check before they are input into an array. I am using a string when I read the in the input. Then, I am converting the string into a character array with toCharArray(). So I need to check for integers before I make the conversion from the string.

Thank you,
Eric
Matt Wilcko
Ranch Hand

Joined: Sep 23, 2003
Posts: 65
When you iterate through your array, extract each element to a string. Then create a patern Matcher and supply the regular expression '[0-9]?'. This should look for occurences of any digit and return a true or false. Just search for tutorials on regular expressions and pattern matchers.
eric elysia
Ranch Hand

Joined: Mar 06, 2005
Posts: 70
What about using something like isLetter or isDigit?

Thanks,
Eric
Jon Egan
Ranch Hand

Joined: Mar 24, 2004
Posts: 83
Regular expressions are powerful and flexible, but for doing what you describe, isDigit would be fine. Not sure which would be more efficient, but I suspect any difference wouldn't matter for your needs. You'd have to write a loop to iterate through the characters in the string.

-- Jon
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
If you are using 1.4 or later, you can use regex on your source Strings.
eric elysia
Ranch Hand

Joined: Mar 06, 2005
Posts: 70
Hello David,

I just sent you an e-mail. I was going to see if you can answer a few questions for me.

Thanks,
Eric
eric elysia
Ranch Hand

Joined: Mar 06, 2005
Posts: 70
I just want to post my latest code so it makes things a little easier.

eric elysia
Ranch Hand

Joined: Mar 06, 2005
Posts: 70
My main questions are:

How can I make sure the user can only input 80 characters?

Should I change the input error checking:
if(Character.isLetter(input[i]) || Character.isSpaceChar(input[])) ?

What is the best thing to have in the echo input error checking statement:
if(choice == 'n') ?

How do I use the method:
public static boolean palindrome(char[] a, int used) ?

Thank you,
Eric
eric elysia
Ranch Hand

Joined: Mar 06, 2005
Posts: 70
OK, after much time and hard work, I have my program doing most of what I want it to do.

I still have these questions:

If the user inputs a 2 for the menu choice before a string is input, I get a NullPointerException error. How can I fix this?

The boolean palindrome(char, int) is not returning the correct result.

Thanks in advance,
Eric

eric elysia
Ranch Hand

Joined: Mar 06, 2005
Posts: 70
OK, I have gotten rid of the NullPointerException error.

I am getting this error:
ArrayIndexOutOfBoundsException: 9
at PalindromeCheck.palindrome(PalindromeCheck.java:89 )
at PalindromeCheck.checkInput(PalindromeCheck.java:67 )
at PalindromeCheck.mainMenu(PalindromeCheck.java:155)
at PalindromeCheck.main(PalindromeCheck.java:177)

How is my array getting full? I do not see what I am doing wrong.

I cannot figure out why boolean palindrome is not returning the correct results.

Thanks,
Eric

Latest code:
eric elysia
Ranch Hand

Joined: Mar 06, 2005
Posts: 70
Can anyone please help me figure out what I am doing wrong here?
eric elysia
Ranch Hand

Joined: Mar 06, 2005
Posts: 70
How is my array going out of bounds? I am trying to learn this, but it is not coming easy at times.

Thank you,
Eric
eric elysia
Ranch Hand

Joined: Mar 06, 2005
Posts: 70
Can anyone help me figure this out?

Thanks,
Eric
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
When you don't get a response here at the Ranch, you've got to look over what you've posted and see if you can provide more information. Simply posting again asking for help won't make a difference. There are a lot of posts here, and often instead of replying that we cannot help people will skip your post if it involves too much work to help. We want to help as many people as possible.

So, what have we got? You've given us a couple hundred lines of code and a stack trace. Where exactly is line 89? I see it must be in palindrome(), and most of that is commented out.Since the bolded line is the only one accessing the array, I must assume that used is set larger than a.length. Have you verified that used has the correct value you expect? Put some println()'s in there to verify your assumptions.

I recommend you try out some of these ideas as this is the kind of detective work you need to practice so you can find solutions yourself. I'll let you find the actual problem, but post more information if you cannot find it.
eric elysia
Ranch Hand

Joined: Mar 06, 2005
Posts: 70
OK, sorry about that. I am in the process or rewriting it.
When I run it more than once, it is giving me some weird output when I echo back the input to the user the second time.

eric elysia
Ranch Hand

Joined: Mar 06, 2005
Posts: 70
OK, fixed that part. I forgot to reset count.
eric elysia
Ranch Hand

Joined: Mar 06, 2005
Posts: 70
OK, now I have a few questions.

How do I:

Make sure the input is only letters and blank spaces?
Make sure there is a period at the end of the charString?
If the user inputs the wrong string, how do I allow the user to stay in the program to reinput the correct string?
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
The Character class has several static methods that will analyze an individual char. Check the JavaDocs.

The String class has a static method that will tell you if a String ends with another String. Check the JavaDocs.

For this you'll want to use a loop (e.g. while) that keeps going until the user's input passes your tests. One pattern that doesn't require you to create a boolean to store the result is this:This will loop forever until the break is hit.
eric elysia
Ranch Hand

Joined: Mar 06, 2005
Posts: 70
OK, I am still having some problems.

I am getting this error:
java.lang.StringIndexOutOfBoundsException: String index out of range: 3
at java.lang.String.charAt(Unknown Source)
at Palindrome7.checkInput(Palindrome7.java:29)
at Palindrome7.mainMenu(Palindrome7.java:127)
at Palindrome7.main(Palindrome7.java:158)
Exception in thread "main"


[ April 26, 2005: Message edited by: eric elysia ]
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
I can't tell which line is 29 (please bold the line in the future or mark it in some other way), but this is a problem:What happens when charString doesn't have a period?
eric elysia
Ranch Hand

Joined: Mar 06, 2005
Posts: 70
Sorry that I didn't mark line 29.

The user is required to put a period at the end of the input string.
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Okay, but what happens when the user doesn't put in a period? Is that line 29, by the way? If not, please edit your previous post and bold line 29.
eric elysia
Ranch Hand

Joined: Mar 06, 2005
Posts: 70
I have bolded line 29 in my previous post.

I am trying to write code that makes sure the user does enter a period.
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Originally posted by eric elysia:
I am trying to write code that makes sure the user does enter a period.
Yes, but do you see what happens if the user enters a String without a period? Let's see what happens when you enter "hi" without the quotes.Since 'h' is not a period, the loop body is executed, placing 'h' into the array a.Since 'i' is not a period, the loop body is executed, placing 'i' into the array a.Since i is now outside the bounds of legal values for the String, you get a StringIndexOutOfBoundsException.

I recommend thoroughly reading the java.lang.String JavaDocs as there are several methods in that class that would be very helpful here. Since String is such a core class, it's a very good idea to become familiar with all of its methods.

For example, you want to remove all spaces from the String and test to see if it ends in a period. trim() will remove leading and trailing whitespace (not spaces between words), and endsWith() will tell you if one String ends with another String. Or you could use charString.charAt(charString.length()-1) to check the last char of the String.

Also, if you're using JDK 1.4 or later and you're allowed to use the regular expression methods, you could remove all non-letters with a single line of code using replaceAll().

[ Grrrrrrammar is goooooood! ]
[ April 26, 2005: Message edited by: David Harkness ]
 
Consider Paul's rocket mass heater.
 
subject: Array without ints
 
Similar Threads
Sorting arrays
Printing the index of an array
Populate Array of type Double from text file
Comparing integer element data in Arrays
ArrayList objects