# Detecting Cycle in String on Integer basis

Page 1 of 1

## 2 Replies - 765 Views - Last Post: 04 October 2012 - 04:54 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=294198&amp;s=f99689dd60cbf30e9d8d4a1c39318cba&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 faisal6621

Reputation: 0
• Posts: 4
• Joined: 31-August 10

# Detecting Cycle in String on Integer basis

Posted 03 October 2012 - 10:36 PM

The problem statement is attached, and here what I'd tried.

```import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.StringTokenizer;

/**
*
*/
public class DetectingCycles {
public static void detectCycle(String input){
StringTokenizer token = new StringTokenizer(input, " ");
int[] nums = new int[token.countTokens()];
for(int i=0; i<nums.length; i++){
nums[i] = Integer.parseInt(token.nextToken());
}

System.out.println("printing the int array");
for(int i=0; i<nums.length; i++){
System.out.print(nums[i]+" ");
}
System.out.println();

for(int i=0; i<nums.length-1; i++){
String a="";
String b="";
boolean found = false;
int limit = (nums.length-i)/2;
int j=i;
for(int l=1; l<=limit; l++){
a+=nums[j]+" ";
int k=j+1;
//here the logic fails for numbers with more than 1 digit.
for(int m=0; m<a.length()/2; m++){
b+=nums[k]+" ";
k++;
}
System.out.println(a+"\t"+b);
if(a.equals(b)){
System.out.println(a.trim()+" found");
found=true;
break;
}else{
b="";
j++;
}
}
if(found){
break;
}
}
}

public static void main(String[] args) throws FileNotFoundException {
Scanner input = new Scanner(new FileInputStream(args[0]));
while(input.hasNextLine()){
detectCycle(input.nextLine());
}
}
}

```

The logic is working well for the numbers with only 1 digit but fails for the numbers with more than 1 digit.
Is there any better solution for this problem?

Is This A Good Question/Topic? 0

## Replies To: Detecting Cycle in String on Integer basis

### #2 GregBrannon

• D.I.C Lover

Reputation: 2250
• Posts: 5,340
• Joined: 10-September 10

## Re: Detecting Cycle in String on Integer basis

Posted 04 October 2012 - 04:03 AM

I don't think you attached anything, or it wasn't allowed, but it's probably not necessary. Just describe in the post what you're trying to do. Include the necessary part of whatever you tried to attach, copied and pasted directly into a reply to the thread. You might have been able to accomplish the same thing with comments in your code, but those are also missing.

### #3 pbl

• There is nothing you can't do with a JTable

Reputation: 8370
• Posts: 31,956
• Joined: 06-March 08

## Re: Detecting Cycle in String on Integer basis

Posted 04 October 2012 - 04:54 AM

The Tokenizer class is a legacy. Use the STring split() method

String[] token = input.split(" ");

will return an array of String named token. Each element of the array containg what was between the " " so

String[] token = "Hello cruel world".split(" ");
will produce:
token[0] = "Hello"
token[1] = "cruel";
token[2] = "world"