special characters in strings

need to make sure an inputted string is in correct format

Page 1 of 1

2 Replies - 1478 Views - Last Post: 09 September 2009 - 09:37 PM Rate Topic: -----

#1 dragonseyeangie   User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 41
  • Joined: 20-October 08

special characters in strings

Post icon  Posted 09 September 2009 - 06:57 PM

I'm working on a homework project, i have most of the code for the superclass except for one detail,

i need to make sure a string is in the correct format like so "123-A"

i've tried several different ways and i'm getting frustrated

here's one of my test codes for this problem

public class TestCharacter
{
	public static void main(String[] args)
	{
		String str = "123-a";
		int length;
		boolean isTrue = true;
		
		length = str.length();
		
		for (int i = 0; i < 3; i++)
		{
			if (Character.isLetter(str))
			{
				isTrue = false;
			}
		}
		
		if (str.charAt(3) != "-")
		{
			isTrue = false;
		}
		
		if (!str.charAt(4).isLetter)
		{
			isTrue = false;
		}
		
		if (isTrue = true)
		{
			System.out.println("The string is valid");
		}
		
		str = "12345";
		
		for (int i = 0; i < 3; i++)
		{
			if (Character.isLetter(str))
			{
				isTrue = false;
			}
		}
		
		if (str.charAt(3) != "-")
		{
			isTrue = false;
		}
		
		if (!str.charAt(4).isLetter)
		{
			isTrue = false;
		}
		
		if (isTrue = true)
		{
			System.out.println("The string is valid");
		}
		
	}
}	




What the hey am I doing wrong?

Is This A Good Question/Topic? 0
  • +

Replies To: special characters in strings

#2 pbl   User is offline

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

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: special characters in strings

Posted 09 September 2009 - 07:03 PM

String has a nice method that translate a String into an array of char
a lot easier that way


char[] digit = str.toCharArray();

if(digit.length != 5) error
if(digit[3] != '-') error
if(!Character.isLetter(digit[4])) error
for(int i = 0; i < 3; i++)
if(!Character.isDigit[digit[i])) error
Was This Post Helpful? 0
  • +
  • -

#3 dragonseyeangie   User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 41
  • Joined: 20-October 08

Re: special characters in strings

Posted 09 September 2009 - 09:37 PM

View Postpbl, on 9 Sep, 2009 - 06:03 PM, said:

String has a nice method that translate a String into an array of char
a lot easier that way


char[] digit = str.toCharArray();

if(digit.length != 5) error
if(digit[3] != '-') error
if(!Character.isLetter(digit[4])) error
for(int i = 0; i < 3; i++)
if(!Character.isDigit[digit[i])) error



thanks i'll try that, i figured it was something like that but wasn't sure,
just couldn't figure it out, even with all the examples in the book
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1