Solved

checking substrings in java programing

Posted on 2007-11-25
5
1,187 Views
Last Modified: 2013-11-23
how do i write a program that prompts the user to enter two strings, and check whether the first string is a substring of the second?
0
Comment
Question by:juicyfruit82
5 Comments
 
LVL 11

Accepted Solution

by:
spoxox earned 125 total points
Comment Utility
This seems likely to be a homework assignment.

You can refer to another student's work here http://www.experts-exchange.com/Programming/Languages/Java/New_to_Java/Q_22978447.html
for an example of prompting for input and accepting a String from the Scanner object.

If you review the String object, you'll find substring handling methods.
0
 
LVL 17

Expert Comment

by:contactkarthi
Comment Utility
0
 
LVL 17

Expert Comment

by:contactkarthi
Comment Utility
you can use java.util.Scanner to get input from command line
below is some info on that
http://www.cs.utk.edu/~cs365/examples/datacheck.html

below link has the code on how to check for substring

http://ph.answers.yahoo.com/question/index?qid=20071018171147AA5sW3E
0
 
LVL 20

Expert Comment

by:gatorvip
Comment Utility
Checking for substring is a fairly simple task, as you can use the indexOf property of a String

http://java.sun.com/javase/6/docs/api/java/lang/String.html#indexOf(java.lang.String)

Returns:
    if the string argument occurs as a substring within this object, then the index of the first character of the first such substring is returned; if it does not occur as a substring, -1 is returned.
0
 
LVL 2

Expert Comment

by:amateur83
Comment Utility
import java.io.*;
import java.util.*;
public class StringSubString
{
   
     // Create a single shared BufferedReader for keyboard input
        private static BufferedReader stdin =
            new BufferedReader( new InputStreamReader( System.in ) );
       
        public static void main(String[] args) throws IOException
        {
        try
        {
            System.out.println("input 1st string: ");
            String firstString = stdin.readLine();
            System.out.println("input 2nd string: ");
            String secondString = stdin.readLine();
          System.out.println("1st string entered= " + firstString);
          System.out.println("2nd string entered= " + secondString);
            if (secondString.contains(firstString))
          {
            System.out.println("1st String is substring of 2nd String");
          }
          else
          {
            System.out.println("1st String is NOT substring of 2nd String");
          }
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
}
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now