Verify String

Check string entered by user

Page 1 of 1

14 Replies - 1742 Views - Last Post: 31 August 2009 - 05:19 AM Rate Topic: -----

#1 almiteyG  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 22-October 08

Verify String

Posted 30 August 2009 - 05:58 AM

Hello,

The program i written below checks if the string starts and ends with 'a' and has 'b' or 'c' in the middle.

The user enters characters one by one but Now i want to enter the entire string in the start only.

I am unable to do so.

I written a small snippet where i converte the entered string to charater array but theni do not know how to access the characters in between




import java.io.*;

class Tcs2bg
{
	public static void main(String args[]) throws IOException
	{
		InputStreamReader ir = new InputStreamReader(System.in);
		BufferedReader in = new BufferedReader(ir);
		String s;
		System.out.print("UserInput : ");
		s=in.readLine();
		
/*
//string to character array
		char[] cA = s.toCharArray();
		for (char c : cA)
			System.out.print(c);
	   	System.out.println();
		   
		for (int n : cA)
			System.out.print(n);
*/

/*
// i can access the start using charAt() and end same way but i dunno how to access in between
*/

		if((s.equals("a")) || (s.equals("A")))
		{
			System.out.print("UserInput : ");
			s=in.readLine();
			while((s.equals("b")) || (s.equals("B")) || (s.equals("c")) || (s.equals("C")))
			{
				System.out.print("UserInput : ");
				s=in.readLine();			
			}
			if((s.equals("a")) || (s.equals("A")))
				System.out.println("Valid String");									
			else
			{
				System.out.println();
				System.out.println("Invalid end of String");		
			}
		}
		else
		{
			System.out.println();
			System.out.println("Invalid start of String");		
		}
	}
}




I hope someone is able to solve this simple code.

Is This A Good Question/Topic? 0
  • +

Replies To: Verify String

#2 Ice(ITB)  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 48
  • Joined: 19-August 09

Re: Verify String

Posted 30 August 2009 - 08:26 AM

You can check out all the functions you can use on a string at the Sun website.

http://java.sun.com/...ang/String.html


There's charAt(i); "i" is equal to position its at in string

and for last letter

"endsWith()"

This post has been edited by Ice(ITB): 30 August 2009 - 08:27 AM

Was This Post Helpful? 0
  • +
  • -

#3 almiteyG  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 22-October 08

Re: Verify String

Posted 30 August 2009 - 08:48 AM

If i could do with resources on the web why would i post here.

I know the functions, i somehow do not know how to use them with loops.

Like i can check if the 3rd char is a or not but how do i go about doin it in the loop.
Was This Post Helpful? 0
  • +
  • -

#4 Ice(ITB)  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 48
  • Joined: 19-August 09

Re: Verify String

Posted 30 August 2009 - 09:07 AM

ok mate my mistake most people don't search the web to hard before posting, apologies :D

So if the string will only ever be "abca" then try this.
String word="abca";
for(int i=-0;i<=3;i++){

	if word.charAt(0)='a';
	  then print "a at 1"
  
 and so on..
  

  
}




or if your only ever going to see if its the word "abca" then

String checker="abca";
if(checker.equals(inputString)){
  then print out  it matched 

}


If you have any more problems I'm fairly sure the tutorials on this site can help with this problem.
hope that helps.....

This post has been edited by Ice(ITB): 30 August 2009 - 09:15 AM

Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10462
  • View blog
  • Posts: 38,777
  • Joined: 27-December 08

Re: Verify String

Posted 30 August 2009 - 09:16 AM

To verify the String, use a method:

public boolean validString(String x){
   return (x.charAt(0) == 'a' && x.charAt(x.length-1) == x.charAt(0) && 
			 ((x.contains("b") || x.contains("c")));
}



This checks to make sure the first & last 2 characters are 'a', and b & c fall somewhere in between. If you want to get exact about them being in the middle, you can use the charAt() methods with some formulas revolving around the length(). So the only reason you'd need to use a loop is if you have more than one String in a collection. Hope this helps some! Good luck.
Was This Post Helpful? 0
  • +
  • -

#6 almiteyG  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 22-October 08

Re: Verify String

Posted 30 August 2009 - 09:18 AM

no no your not gettin it,

the user will enter the string not me

it will start with a and end with a
in the middle he can put b or c as many times as he wants.

im tryin to find the solution.

if anyone can solve it, post it please
Was This Post Helpful? 0
  • +
  • -

#7 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10462
  • View blog
  • Posts: 38,777
  • Joined: 27-December 08

Re: Verify String

Posted 30 August 2009 - 09:23 AM

Are you having trouble getting the user input as a String or validating it? For validation, look at the method I posted earlier. If you need a String, look at the Scanner Class. Let me show you how it works:

Scanner scan = new Scanner(System.in);
System.out.println("Enter a string");
String x = scan.next();



The scanner will not return a value until the user has typed something. From there, look at the method I posted. Call it w/x as the parameter, and it will return true or false based on the conditions you described. Hope this helps some. Good luck!
Was This Post Helpful? 0
  • +
  • -

#8 smacdav  Icon User is offline

  • D.I.C Head

Reputation: 56
  • View blog
  • Posts: 177
  • Joined: 06-June 09

Re: Verify String

Posted 30 August 2009 - 09:24 AM

It's not necessary to convert the string into a character array.

For using a loop, I'd say you want to use the String class's charAt and length methods. Personally, I'd also use a boolean flag in order to determine whether the string is valid or not. Here's some pseudocode:

boolean valid = true;

if ((s.charAt(0) is not either 'A' or 'a') or (s.endsWith('a') and s.endsWith('A') are both false))
	valid = false;

for(int i = 1; i < s.length() - 1; i++)
{
	if(s.charAt(i) is not either 'b', 'B', 'c', or 'C')
	{
		valid = false;
		i = s.length(); // You can also use break here...
	}
}

if(valid)
	System.out.println("Valid String");


This post has been edited by smacdav: 30 August 2009 - 09:25 AM

Was This Post Helpful? 0
  • +
  • -

#9 Ice(ITB)  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 48
  • Joined: 19-August 09

Re: Verify String

Posted 30 August 2009 - 09:24 AM

View PostalmiteyG, on 30 Aug, 2009 - 08:18 AM, said:

if anyone can solve it, post it please


No I think its you who doesn't get it.
.... were here for to help you with your code and point in the right direction, give snippets and so on.... not give you the answer.... if I gave the answer would you learn anything?

if c appears after b then yes bc is in there.. record position of both.... find length of string
if b and c's position is in the middle of the strings length then, yes its in the middle ...

This post has been edited by Ice(ITB): 30 August 2009 - 09:28 AM

Was This Post Helpful? 0
  • +
  • -

#10 almiteyG  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 22-October 08

Re: Verify String

Posted 30 August 2009 - 09:36 AM

no i wudnt learn...

im just havin syntax problem.

and i meant your not understanding the question

for eg: valid string is abcbcbcbbba or abba or acbcbcccca

in my program the user is entering the character one at a time and if its wrong anywhere the program terminates

i want the whole string to be entered in the beginning only.

i did that too and converted it into a character array,

i only cant access the middle of the array in a loop, like substring(1,n-1)

wher 1 is character 2 and n-1 is second last character

		String str;
		System.out.print("UserInput : ");
		str=in.readLine();
		
		char[] cA = str.toCharArray();
		for (char c : cA)
			System.out.print(c);



View Postsmacdav, on 30 Aug, 2009 - 08:24 AM, said:

It's not necessary to convert the string into a character array.

For using a loop, I'd say you want to use the String class's charAt and length methods. Personally, I'd also use a boolean flag in order to determine whether the string is valid or not. Here's some pseudocode:

boolean valid = true;

if ((s.charAt(0) is not either 'A' or 'a') or (s.endsWith('a') and s.endsWith('A') are both false))
	valid = false;

for(int i = 1; i < s.length() - 1; i++)
{
	if(s.charAt(i) is not either 'b', 'B', 'c', or 'C')
	{
		valid = false;
		i = s.length(); // You can also use break here...
	}
}

if(valid)
	System.out.println("Valid String");





ok thanx, i will try this and get back to you.
Was This Post Helpful? 0
  • +
  • -

#11 Ice(ITB)  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 48
  • Joined: 19-August 09

Re: Verify String

Posted 30 August 2009 - 10:04 AM

ok for finding if middle = "bc".

START
 declare x char  to hold value
 read in  z String for  checking.
 declare int variable y = to word.length()/2
 declare boolean variable f =false.

 x=z char at y-1
if x ==b
	x=z char at y
  
	  if (x==c)
		f= true
	 end if
 end if

print out if found
END


This post has been edited by Ice(ITB): 30 August 2009 - 10:12 AM

Was This Post Helpful? 0
  • +
  • -

#12 almiteyG  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 22-October 08

Re: Verify String

Posted 30 August 2009 - 11:11 AM

View Postsmacdav, on 30 Aug, 2009 - 08:24 AM, said:

It's not necessary to convert the string into a character array.

For using a loop, I'd say you want to use the String class's charAt and length methods. Personally, I'd also use a boolean flag in order to determine whether the string is valid or not. Here's some pseudocode:

boolean valid = true;

if ((s.charAt(0) is not either 'A' or 'a') or (s.endsWith('a') and s.endsWith('A') are both false))
	valid = false;

for(int i = 1; i < s.length() - 1; i++)
{
	if(s.charAt(i) is not either 'b', 'B', 'c', or 'C')
	{
		valid = false;
		i = s.length(); // You can also use break here...
	}
}

if(valid)
	System.out.println("Valid String");





thankx a lot buddy, i got it finally

import java.io.*;

class Tcs2bg
{
	public static void main(String args[]) throws IOException
	{
		InputStreamReader ir = new InputStreamReader(System.in);
		BufferedReader in = new BufferedReader(ir);
		String str;
		System.out.print("InputString : ");
		str = in.readLine();
		boolean valid = false;	
		if((str.charAt(0)==('a')) && (str.endsWith("a")))
		{
			valid = true;
			for(int i = 1; i < str.length() - 1; i++)
			{
				if((str.charAt(i)==('b')) || (str.charAt(i)==('c')))
					valid = true;
				else
				{
					System.out.println();
					System.out.println("Invalid: String can contain only a,b,c");		
				}	
			}
		}
		else
		{
			System.out.println();
			System.out.println("Invalid: String should start and end with a");		
		}
		if(valid)
		{
			System.out.println();
			System.out.println("Valid String");			
		}
	}
}



View PostIce(ITB), on 30 Aug, 2009 - 09:04 AM, said:

ok for finding if middle = "bc".

START
 declare x char  to hold value
 read in  z String for  checking.
 declare int variable y = to word.length()/2
 declare boolean variable f =false.

 x=z char at y-1
if x ==b
	x=z char at y
  
	  if (x==c)
		f= true
	 end if
 end if

print out if found
END





you still din understand the question....thankx for all the help tho.
Was This Post Helpful? 0
  • +
  • -

#13 smacdav  Icon User is offline

  • D.I.C Head

Reputation: 56
  • View blog
  • Posts: 177
  • Joined: 06-June 09

Re: Verify String

Posted 30 August 2009 - 01:40 PM

I'm glad to help. Along that line, there are actually some issues with the code you've come up with that I'll point out:

1. Once valid is set to true, it is never set back to false for any reason. Thus, as long as the string starts and ends with 'a', the program will eventually print "Valid String", though it may print "Invalid: String can contain only a,b,c" several times first if the middle characters aren't all 'b' or 'c'.

2. I think you actually mean the error if a character in the middle is not either 'b' or 'c' to indicate that the middle characters must be 'b' or 'c'. Otherwise, the string "abaca" would produce the error message "Invalid: String can contain only a,b,c" upon hitting the middle 'a'. As a user, that error message would be confusing; I'd think, "But my string does contain only a, b, and c!"

3. Finally, since you provide nothing that breaks out of the loop, the error message discussed above may be printed several times (once for each character that is not a 'b' or a 'c'). Is that what you wanted?

This post has been edited by smacdav: 30 August 2009 - 01:41 PM

Was This Post Helpful? 1
  • +
  • -

#14 syfran  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 83
  • View blog
  • Posts: 1,103
  • Joined: 12-July 09

Re: Verify String

Posted 30 August 2009 - 02:52 PM

You can always use regex :D
import java.util.regex.*;
import java.util.Scanner;

class regex {

		public static void main(String [] pie) {
				System.out.println("Please enter String");
				System.out.println((Pattern.compile("a[bc]*a").matcher(new Scanner(System.in).nextLine()).matches() ? "valid String" : "Invalid String"));
		}
}


Yes, I am bored.
Was This Post Helpful? 1
  • +
  • -

#15 almiteyG  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 22-October 08

Re: Verify String

Posted 31 August 2009 - 05:19 AM

View Postsmacdav, on 30 Aug, 2009 - 12:40 PM, said:

I'm glad to help. Along that line, there are actually some issues with the code you've come up with that I'll point out:

1. Once valid is set to true, it is never set back to false for any reason. Thus, as long as the string starts and ends with 'a', the program will eventually print "Valid String", though it may print "Invalid: String can contain only a,b,c" several times first if the middle characters aren't all 'b' or 'c'.

2. I think you actually mean the error if a character in the middle is not either 'b' or 'c' to indicate that the middle characters must be 'b' or 'c'. Otherwise, the string "abaca" would produce the error message "Invalid: String can contain only a,b,c" upon hitting the middle 'a'. As a user, that error message would be confusing; I'd think, "But my string does contain only a, b, and c!"

3. Finally, since you provide nothing that breaks out of the loop, the error message discussed above may be printed several times (once for each character that is not a 'b' or a 'c'). Is that what you wanted?


thankx mate... i removed the kinks.

the programs works now how i want it to

i hope there are no more bugs.
thankx again

import java.io.*;

class Tcs2bg
{
	public static void main(String args[]) throws IOException
	{
		InputStreamReader ir = new InputStreamReader(System.in);
		BufferedReader in = new BufferedReader(ir);
		String str;
		System.out.print("InputString : ");
		str = in.readLine();
		boolean valid = true;	
		if((str.charAt(0)==('a')) && (str.endsWith("a")))
		{
			for(int i = 1; i < str.length() - 1; i++)
			{
				if((str.charAt(i)==('b')) || (str.charAt(i)==('c')))
				{}
				else
				{
					valid = false;
					System.out.println();
					System.out.println("Invalid: String can contain only b,c in the middle.");
					break;		
				}	
			}
		}
		else
		{
			valid=false;
			System.out.println();
			System.out.println("Invalid: String should start and end with a.");		
		}
		if(valid)
		{
			System.out.println();
			System.out.println("Valid String");			
		}
	}
}



View Postsyfran, on 30 Aug, 2009 - 01:52 PM, said:

You can always use regex :D
import java.util.regex.*;
import java.util.Scanner;

class regex {

		public static void main(String [] pie) {
				System.out.println("Please enter String");
				System.out.println((Pattern.compile("a[bc]*a").matcher(new Scanner(System.in).nextLine()).matches() ? "valid String" : "Invalid String"));
		}
}


Yes, I am bored.



wow that is sweet and that precisely was my question a(bc)*a

but i have to WAP for my practicals, so no hardcore program required.

thankx for the post, it adds to my java knowledge.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1