2 Replies - 263 Views - Last Post: 04 October 2012 - 04:54 AM Rate Topic: -----

#1 faisal6621  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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;

/**
 *
 * @author Mohammad Faisal
 */
public class DetectingCycles {
    public static void detectCycle(String input){
        System.out.println("received 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  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2194
  • View blog
  • Posts: 5,222
  • 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.
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

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

Reputation: 8315
  • View blog
  • Posts: 31,836
  • 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"
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1