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.

New Topic/Question
Reply




MultiQuote










|