Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

checking substrings in java programing

Posted on 2007-11-25
5
Medium Priority
?
1,195 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 11

Accepted Solution

by:
spoxox earned 500 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
A while back, I ran into a situation where I was trying to use the calculated columns feature in SharePoint 2013 to do some simple math using values in two lists. Between certain data types not being accessible, and also with trying to make a one to…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
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…
Suggested Courses

610 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