Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Help, text reader outputs high count value!

  1. #1
    Junior Member
    Join Date
    Jul 2013
    Posts
    7
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Help, text reader outputs high count value!

    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();
        }
    }


  2. #2
    Member llowe29's Avatar
    Join Date
    Jul 2013
    Posts
    116
    My Mood
    Tired
    Thanks
    9
    Thanked 5 Times in 5 Posts

    Default Re: Help, text reader outputs high count value!

    wait why do you print i if it is not the wouldnt you print it if it was

  3. #3
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,514
    My Mood
    Grumpy
    Thanks
    0
    Thanked 165 Times in 156 Posts

    Default Re: Help, text reader outputs high count value!

    Have you printed out the contents of textFile? What you have coded does not make the Scanner object automagically read the entire contents of the file.
    Improving the world one idiot at a time!

Similar Threads

  1. Column count doesn't match value count at row 1
    By Tyluur in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 30th, 2012, 12:31 AM
  2. Why do I get 0 in the outputs?
    By jean28 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 30th, 2012, 12:00 PM
  3. Code only outputs 1's
    By LoganC in forum What's Wrong With My Code?
    Replies: 7
    Last Post: September 27th, 2012, 09:26 PM
  4. Program that selectively outputs data from a text file.
    By MJjavapf in forum File I/O & Other I/O Streams
    Replies: 5
    Last Post: September 23rd, 2012, 04:26 PM
  5. LinkedList outputs ONLY last element
    By hexwind in forum What's Wrong With My Code?
    Replies: 3
    Last Post: June 30th, 2011, 04:57 AM