The Artima Developer Community
Sponsored Link

Java Answers Forum
Find The Contained Number

1 reply on 1 page. Most recent reply: Feb 24, 2003 2:44 AM by Oliver

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
JZ

Posts: 1
Nickname: spendalot
Registered: Feb, 2003

Find The Contained Number Posted: Feb 23, 2003 3:53 PM
Reply to this message Reply
Advertisement
Hey Guys...I need some help,

I have a loop of numbers running and I'm trying to have the output send me a message whenever it finds the number I'm looking for. In other words, if I input the number 20 and tell it to find all the 3's, I want the ouput to be:
1,2,Found,4,5,6,7,8,9,10,11,12,Found,14,15,16,17,18,19,20.

How would I go about this?


Oliver

Posts: 12
Nickname: lazycat
Registered: Dec, 2002

Re: Find The Contained Number Posted: Feb 24, 2003 2:44 AM
Reply to this message Reply


/*
* @(#)$Id: FindNumber.java,v 1.2 2003/02/24 09:42:19 Administrator Exp Administrator $
*
*/

public class FindNumber{
String result;
int endNumber;
int numberToFind;

public FindNumber(int endNumber, int numberToFind){
this.endNumber = (endNumber > 0) ? endNumber : -endNumber;
this.numberToFind = (numberToFind >0) ? numberToFind : -numberToFind;
result = "";
}

public void findResult(){
String number;
String matchNumber = Integer.toString(numberToFind);

for(int i = 1; i <= endNumber; i++){
number = Integer.toString(i);
if(number.indexOf(matchNumber) != -1){
result += ",Found";
}
else{
if(result == "")
result += i;
else
result += "," + i;
}
}
}

public void printResult(){
System.out.println(result);
}

}

class TestFindNumber{
public static void main(String [] args){
FindNumber fn = new FindNumber(20, 3);

fn.findResult();
fn.printResult();
}
}

Flat View: This topic has 1 reply on 1 page
Topic: Help : read Unix FIFO Previous Topic   Next Topic Topic: Order for school: write little program for guessing a number

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use