5 Replies - 1694 Views - Last Post: 09 July 2011 - 11:29 AM Rate Topic: -----

#1 lolitacharm  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 118
  • Joined: 25-June 11

index of coincidence

Posted 09 July 2011 - 10:52 AM

I am having some major problems with my program so far. I don't think I'm using the method correctly (i.e. how you pass a string?)

The point is to calculate the index of coincidence when two strings of equal length are entered. For example, with

hellotherehowareyou
heycanhalstaytotalk

there are 19 words and positions 1 (h) and 7 (h) contain the same letter so the index of coincidence is 10.5%


import java.util.*; 

public class Index
{
 public static void main(String[] args)
    {
        Scanner input = new Scanner(System.in);
        
        System.out.println("input a string of characters: "); 
        String firstLine = new String (input.nextLine()); 
        System.out.println();
        System.out.println("input a string of characters of equal length: ");
        String secondLine = new String (input.nextLine()); 
        String indexOfCoincidence = new String ( indexOfCoincidence(String firstLine, String secondLine));
        System.out.println("The index of Coincidence is " + String indexOfCoincidence);
    
    }
public float indexOfCoincidence(String X, String Y) 
{
     int sameChars = 0;
	  for( int i = 0; i < totalLength; ++i )
	  {
			if( X.charAt(i) == Y.charAt(i) )
				++sameChars;
          }

	  float ioc = (float)sameChars / (float)totalLength;
	  return ioc;
   }
}



Is This A Good Question/Topic? 0
  • +

Replies To: index of coincidence

#2 lolitacharm  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 118
  • Joined: 25-June 11

Re: index of coincidence

Posted 09 July 2011 - 11:13 AM

This is my revised version, but now I am getting an error in my method: cannot find symbol totalLength


import java.util.*;


public class Index

{

 public static void main(String[] args)

    {

        Scanner input = new Scanner(System.in);

         

        System.out.println("input a string of characters: ");

        String firstLine = new String (input.nextLine());

        System.out.println();

        System.out.println("input a string of characters of equal length: ");

        String secondLine = new String (input.nextLine());

        float index = indexOfCoincidence( firstLine, secondLine);

        System.out.println("The index of Coincidence is " + index);

    }

public static float indexOfCoincidence(String X, String Y)

{

     int sameChars = 0;

      for( int i = 0; i < totalLength; ++i )

      {
            if( X.charAt(i) == Y.charAt(i) )

                ++sameChars;

          }

 

      float ioc = (float)sameChars / (float)totalLength;

      return ioc;

   }

}


Was This Post Helpful? 0
  • +
  • -

#3 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7876
  • View blog
  • Posts: 13,367
  • Joined: 19-March 11

Re: index of coincidence

Posted 09 July 2011 - 11:17 AM

Your problem is here:

System.out.println("The index of Coincidence is " + String indexOfCoincidence);


Delete the word "String" from this line. You've already declared it.



EDIT: okay, you caught that.

totalLength is not declared in the method, therefore the compiler can't find the symbol.

This post has been edited by jon.kiparsky: 09 July 2011 - 11:18 AM

Was This Post Helpful? 1
  • +
  • -

#4 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1406
  • View blog
  • Posts: 3,121
  • Joined: 05-April 11

Re: index of coincidence

Posted 09 July 2011 - 11:20 AM

There is no variable named totalLength. It should be either x.lengt() or y.length()
Was This Post Helpful? 1
  • +
  • -

#5 lolitacharm  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 118
  • Joined: 25-June 11

Re: index of coincidence

Posted 09 July 2011 - 11:21 AM

View Postjon.kiparsky, on 09 July 2011 - 11:17 AM, said:

totalLength is not declared in the method, therefore the compiler can't find the symbol.



Okay, so how do I want totalLength to be declared in the method?? It needs to be the length of the two strings entered right? (which will be of equal length)
Was This Post Helpful? 0
  • +
  • -

#6 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7876
  • View blog
  • Posts: 13,367
  • Joined: 19-March 11

Re: index of coincidence

Posted 09 July 2011 - 11:29 AM

Declare an int. Figure out what value you need. Set the int to that value.

They strings should be equal length, eh? What happens if someone gives you non-equal strings? You might want to think about that. For example, if you set the length variable to be the longer of the two, you'll run into an exception when you try to get a charAt() with an index that doesn't exist.

You probably don't need to deal with this now, but it's the sort of thing you should start thinking about.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1