User Name Password Register
DaniWeb IT Discussion Community
All
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
Reply
Join Date: Sep 2006
Posts: 8
Reputation: Klitzy is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Klitzy Klitzy is offline Offline
Newbie Poster

Help Simple algorithm question/needed

  #1  
Sep 14th, 2006
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
Last edited by Klitzy : Sep 14th, 2006 at 4:18 pm.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: May 2006
Location: Bellevue, WA
Posts: 1,548
Reputation: Infarction has a spectacular aura about Infarction has a spectacular aura about Infarction has a spectacular aura about 
Rep Power: 8
Solved Threads: 51
Sponsor
Infarction's Avatar
Infarction Infarction is offline Offline
Battle Programmer

Re: Simple algorithm question/needed

  #2  
Sep 14th, 2006
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
Reply With Quote  
Join Date: Sep 2006
Posts: 8
Reputation: Klitzy is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Klitzy Klitzy is offline Offline
Newbie Poster

Re: Simple algorithm question/needed

  #3  
Sep 14th, 2006
Hmm...Thank you for the great response. My teacher said I should use a "Do...While" Loop. Still the same idea?
Reply With Quote  
Join Date: May 2006
Location: Bellevue, WA
Posts: 1,548
Reputation: Infarction has a spectacular aura about Infarction has a spectacular aura about Infarction has a spectacular aura about 
Rep Power: 8
Solved Threads: 51
Sponsor
Infarction's Avatar
Infarction Infarction is offline Offline
Battle Programmer

Re: Simple algorithm question/needed

  #4  
Sep 14th, 2006
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.
Reply With Quote  
Join Date: Nov 2004
Location: Netherlands
Posts: 5,752
Reputation: jwenting is a jewel in the rough jwenting is a jewel in the rough jwenting is a jewel in the rough jwenting is a jewel in the rough 
Rep Power: 18
Solved Threads: 197
Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

Re: Simple algorithm question/needed

  #5  
Sep 14th, 2006
far simpler than that, you're not going to treat them as numbers at all.
String.indexOf() is your friend here.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote  
Join Date: Aug 2005
Posts: 4,782
Reputation: iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light iamthwee is a glorious beacon of light 
Rep Power: 17
Solved Threads: 319
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Industrious Poster

Re: Simple algorithm question/needed

  #6  
Sep 16th, 2006
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.
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.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Java Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Java Forum

All times are GMT -4. The time now is 3:25 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC