Solved

checking substrings in java programing

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
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.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

803 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