Solved

checking substrings in java programing

Posted on 2007-11-25
5
1,188 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
This article discusses how to create an extensible mechanism for linked drop downs.
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 invoke it on a web page. 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.: (CODE)

948 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

19 Experts available now in Live!

Get 1:1 Help Now