•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 422,980 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,923 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 1138 | Replies: 5
![]() |
•
•
Join Date: Sep 2006
Posts: 8
Reputation:
Rep Power: 0
Solved Threads: 0
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.
Kevin
Kevin
Last edited by Klitzy : Sep 14th, 2006 at 4:18 pm.
•
•
Join Date: May 2006
Location: Bellevue, WA
Posts: 1,548
Reputation:
Rep Power: 8
Solved Threads: 51
First start out with a simple skeleton code:
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
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
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
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
class Main
{
public static void main(String [] args)
{
}
}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
•
•
Join Date: May 2006
Location: Bellevue, WA
Posts: 1,548
Reputation:
Rep Power: 8
Solved Threads: 51
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.
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.
•
•
Join Date: Nov 2004
Location: Netherlands
Posts: 5,752
Reputation:
Rep Power: 18
Solved Threads: 197
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.
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.
I'm not a programmer. My attitude starts with ignorance, holds steady at conversation, and ends with a trip to the hospital. Get used to it.
![]() |
•
•
•
•
•
•
•
•
DaniWeb Java Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- Time complexity of algorithm (Computer Science and Software Design)
- Quicksorting linked list - simple algorithm (C)
- Structural Equivalence Algorithm Question (C)
Other Threads in the Java Forum
- Previous Thread: Java Shape Generator Help
- Next Thread: anyone know Matlab?



Linear Mode