Solved

checking substrings in java programing

Posted on 2007-11-25
5
1,191 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
ID: 20347894
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
ID: 20347905
0
 
LVL 17

Expert Comment

by:contactkarthi
ID: 20347920
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
ID: 20350922
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
ID: 20356628
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

730 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