DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 3 of 3
  1. #1
    Join Date
    Dec 2004
    Posts
    46

    startsWith(String)

    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

  2. #2
    Join Date
    Dec 2004
    Posts
    4
    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;
    }
    }

  3. #3
    Join Date
    Dec 2004
    Posts
    46
    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");
    }
    
       }
    }

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links