Hi,
I want to write a program so i can input a string followed by a single character and then test whether that string starts with that character.
Would i use the startsWith(String) method and if so how?
Thanks
Printable View
Hi,
I want to write a program so i can input a string followed by a single character and then test whether that string starts with that character.
Would i use the startsWith(String) method and if so how?
Thanks
import java.io.*;
public class TestString {
public static void main (String args[]) throws IOException {
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader in = new BufferedReader(isr);
System.out.print("Word: ");
System.out.flush();
String input= in.readLine();
System.out.print("Character: ");
System.out.flush();
char c = (in.readLine()).charAt(0);
boolean ch = startsWith(input,c);
if(ch) System.out.println("match");
else System.out.println("no match");
}
static boolean startsWith(String s, char x) {
boolean check = false;
if(s.charAt(0) == x) check = true;
return check;
}
}
Hi,
Thanks for your help.
I have done it now anyway.
Code:public class Strings4_3b
{
public static void main (String[] args)
{
String string1, string2;
System.out.print("Please Enter First String: ");
string1 = EasyIn.getString();
System.out.print("Please Enter Single Character: ");
string2 = EasyIn.getString();
while((string2.length() != 1))
{
System.out.print("Please input one character only");
string2 = EasyIn.getString();
}
if(string1.startsWith(string2))
{
System.out.println("The String starts with that character");
}
else
{
System.out.println("The String does not start with that character");
}
}
}