Hi guys...Im new here and just registered a few minutes ago. I am a sophmore in high school and this is my first year of java. I have a program that I have to do for homework and I don't even know where to start. We have just begun learning about arrays and the project is to create a program in which the user enters a number and then enters another number in which the computer tells you if the second number is located inside the first number. Example...56,792 4, the computer would return a "No, that number is not located in 56,792. I know this might be simple but I am just looking for a little start/algorithm. That would be very helpful. Thanks a bunch.


Recommended Answers

All 5 Replies

First start out with a simple skeleton code:

class Main
  public static void main(String [] args)


Now, let's think about what we need to do.

We probably should prompt the user to enter the numbers (even though you know it takes 2 numbers, prompts make it look nicer). You can do this with a few lines of System.out.print("some text"); (you can also use println if you want a newline at the end).

We also need to get the user's input. Assuming you're using Java 1.5, this is pretty easy. You'll probably want to use the java.util.Scanner class and it's nextInt() method.

Then comes the tough part: checking if the second number is in the first. I'd do it by making two strings out of the inputted numbers (using Integer.toString(/* int variable here */) ) and then using something like firstNumber.indexOf(secondNumber) .

I recommend reading and getting used to the Java API. You can find each of the classes and methods I mentioned in there to make sure you know how to use them. Good luck with your assignment ;)

Hmm...Thank you for the great response. My teacher said I should use a "Do...While" Loop. Still the same idea?

You could use a do-while loop. I can make two separate assumptions on how the program should work if you use that.

a) The second number should only be a single digit. Enforcing this (if you so choose) would require extra code. You could then check to see if that single digit occurs in the first number.

b) The second number is again without limitation (obviously, it should equal to or shorter than the first, though it's still valid otherwise). You could then convert the two to strings (as per my first post or another method if you prefer), and check if the second string exists as a substring of the first (by taking the beginning n characters of the first number and comparing that to the second number, where n is the length of the second number). Then move to the next index of the first number and take a new substring.

These probably aren't the only ways to do it, but they're the most obvious ways to me.

far simpler than that, you're not going to treat them as numbers at all.
String.indexOf() is your friend here.

Member Avatar for iamthwee

All you are effectively doing is trying to see if a substring is contained within the original string.

The question is, does your instructor want you to write your own, or use the java API? If it's the latter jwenting's advice is the avenue you need to explore.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.