9 Replies - 303 Views - Last Post: 01 August 2013 - 03:43 AM Rate Topic: -----

#1 anmarvel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 31-July 13

problem with counter in String File camparison program.

Posted 31 July 2013 - 01:15 PM

Ok so this program is soppose to take a String and compare it to a file, the only problem is is that it outputs too high a cout value and im not entirely sure why. If i search for "the" in this file:

"In 2013, major cruise lines are expected to welcome 17.6 million passengers — or more than double the number from just around a decade ago, according to the Cruise Lines International Association. On top of that, the lines also introduced new ships — 13 in 2012 alone and 167 since 2000. All that spells serious competition, with ships trying to offer adventures and amenities that set them apart from the rest. The bottom line? “By creating these flashy experiences, they can draw passengers,” says Sherri Eisenberg, editor of Bon Voyage digital magazine, a publication for cruise fans. But what kind of experiences? Read on for 10 of the latest cruise innovations."

it tells me there are 43 times it is shown , when there really only is 9.
Any help would be greatly appricated.


import java.util.*;
import java.util.regex.Pattern;
 
public class Textrepo
{
    public static void main(String[] args) throws Exception
    {
        Scanner in= new Scanner(System.in);
        java.io.File file = new java.io.File("new file");
        String s;
        System.out.print("What string are you looking for?");
        s=in.nextLine();
        int wordc=0;
        Scanner parser = new Scanner(file);
        String textFile = parser.toString();	
        String[] words = s.split("");
        String[] words2 = textFile.split("");
          for(int i = 0; i < words.length; i++)      
            {      	
                for(int j = 0; j < words2.length; j++)
                       {
                           if(words[i].equals(words2[j]))
                           {
                           	wordc++;
                           }
                           else
                           {
                           	System.out.print("i");
                           }
 
                       }
 
            }
        System.out.print("There are "+ wordc + " repeats of youre string");
        parser.close();
    }
}



Is This A Good Question/Topic? 0
  • +

Replies To: problem with counter in String File camparison program.

#2 StrongJoshua  Icon User is offline

  • D.I.C Head

Reputation: 47
  • View blog
  • Posts: 156
  • Joined: 19-July 13

Re: problem with counter in String File camparison program.

Posted 31 July 2013 - 02:07 PM

You should use while loop calling parser.hasNext() to check through the whole file. Then assign parser.next() to a temporary string value. Then cycle through your list of words to search for using a for loop and compare. At least this is how I think the program could/should work.
Hope this helps :)
Was This Post Helpful? 0
  • +
  • -

#3 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4332
  • View blog
  • Posts: 12,127
  • Joined: 18-April 07

Re: problem with counter in String File camparison program.

Posted 31 July 2013 - 02:08 PM

Oh boy... where to start.

1) Ok first of all you are splitting using "" and not " ". By putting no space in there, you are going to be splitting into character by character instead of word by word.

2) Calling "toString" on a scanner object (your "parser") is going to give you a string representation of the scanner class, not read everything from the file and make it a string. You have to read from the scanner to get at the content.

3) With your nested loop you have there, you do realize that if the user enters a sentence with two of the same word that it will end up double counting the words right? For instance, if I enter "the dog chased the fox" it will actually search the file for "the" twice and count up 9 one time and 9 the other time. I think you are wanting this to simply count the total occurrences of a given word in the input. Even if I enter "the" twice, it should still only tell me there are 9 "the"'s in the file.

If you are going to take in multiple words to count as the input, perhaps you should instead create an array of counters and as you find a tally of each word, store it in the corresponding place in the array. At the end you can print the array to show that there were 9 "the" words, 2 "dog" words, 5 "fox" words etc.

:)
Was This Post Helpful? 0
  • +
  • -

#4 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2700
  • View blog
  • Posts: 11,370
  • Joined: 20-September 08

Re: problem with counter in String File camparison program.

Posted 31 July 2013 - 02:16 PM

A StreamTokenizer is a highly efficient way of tokenizing words in a file
http://technojeeves....d-count-in-java

Easy to override that method to compare a specific word
Was This Post Helpful? 0
  • +
  • -

#5 anmarvel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 31-July 13

Re: problem with counter in String File camparison program.

Posted 31 July 2013 - 03:51 PM

View PostMartyr2, on 31 July 2013 - 02:08 PM, said:

Oh boy... where to start.

1) Ok first of all you are splitting using "" and not " ". By putting no space in there, you are going to be splitting into character by character instead of word by word.

2) Calling "toString" on a scanner object (your "parser") is going to give you a string representation of the scanner class, not read everything from the file and make it a string. You have to read from the scanner to get at the content.

3) With your nested loop you have there, you do realize that if the user enters a sentence with two of the same word that it will end up double counting the words right? For instance, if I enter "the dog chased the fox" it will actually search the file for "the" twice and count up 9 one time and 9 the other time. I think you are wanting this to simply count the total occurrences of a given word in the input. Even if I enter "the" twice, it should still only tell me there are 9 "the"'s in the file.

If you are going to take in multiple words to count as the input, perhaps you should instead create an array of counters and as you find a tally of each word, store it in the corresponding place in the array. At the end you can print the array to show that there were 9 "the" words, 2 "dog" words, 5 "fox" words etc.

:)/>



I used "" instead of " " because the if the user in puts "i h" it has to out put how meny times "i h" show up.
i.e: :then i have to go to bed" would give me 1. So i need both to be read as strigns to compare letter for letter.

orr at least i think :S

how do you recommend I fix this up?
Was This Post Helpful? 0
  • +
  • -

#6 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2700
  • View blog
  • Posts: 11,370
  • Joined: 20-September 08

Re: problem with counter in String File camparison program.

Posted 31 July 2013 - 04:47 PM

Quote

because the if the user in puts "i h" it has to out put how meny times "i h"
...

You can't have it both ways. You were complaining earlier that target string 'the' returned too many hits. If you are going to match across words (i.e. plain substrings) then of course the word 'they' etc will cause a hit.
Was This Post Helpful? 0
  • +
  • -

#7 anmarvel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 31-July 13

Re: problem with counter in String File camparison program.

Posted 31 July 2013 - 04:56 PM

View Postg00se, on 31 July 2013 - 04:47 PM, said:

Quote

because the if the user in puts "i h" it has to out put how meny times "i h"
...

You can't have it both ways. You were complaining earlier that target string 'the' returned too many hits. If you are going to match across words (i.e. plain substrings) then of course the word 'they' etc will cause a hit.


That is what I need this to do, any given input should say how many times it was used in the file. It wasnt giving too many hits- "the" showed up 9 times, it would output 43. The 9 was including a "they". but for some reason the count was incredibly high
Was This Post Helpful? 0
  • +
  • -

#8 StrongJoshua  Icon User is offline

  • D.I.C Head

Reputation: 47
  • View blog
  • Posts: 156
  • Joined: 19-July 13

Re: problem with counter in String File camparison program.

Posted 31 July 2013 - 05:13 PM

Well, then your program should only show how many times the string shows up in the text file, shouldn't it? If I understood the purpose of your program correctly you should remove the splitting and instead find a way to search the file for the string that was entered.

This post has been edited by StrongJoshua: 31 July 2013 - 05:13 PM

Was This Post Helpful? 0
  • +
  • -

#9 anmarvel  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 31-July 13

Re: problem with counter in String File camparison program.

Posted 31 July 2013 - 05:16 PM

View PostStrongJoshua, on 31 July 2013 - 05:13 PM, said:

Well, then your program should only show how many times the string shows up in the text file, shouldn't it? If I understood the purpose of your program correctly you should remove the splitting and instead find a way to search the file for the string that was entered.


Wouldn't the splitting allow both the input and the file to be seperated into two seperate arrays and then compared to one another letter by letter?
Was This Post Helpful? 0
  • +
  • -

#10 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2700
  • View blog
  • Posts: 11,370
  • Joined: 20-September 08

Re: problem with counter in String File camparison program.

Posted 01 August 2013 - 03:43 AM

If you are substringing then NO splitting is necessary - think about it
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1