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 5 of 5

Thread: How do I fix my program?

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How do I fix my program?

    /*This program will allow the user to search for a particular 
      file he/she desires and count the number of times a particular
      substring occurs in the file.
    */
     
    import java.util.Scanner;
    import java.io.*;
     
    public class Assignment4
    {
    	public static void main(String[] args) throws IOException
    	{
    		int count = 0;
     
    		Scanner keyboard = new Scanner(System.in);
     
    		System.out.println("Enter the name of the file you would like" +
    			" to search.");
    		String fileName = keyboard.nextLine();
     
    		File file = new File(fileName);
     
    		if(!file.exists())
    		{
    			System.out.println("The file " + fileName + "is not found.");
    			System.exit(0);
    		}
     
    		Scanner inputFile = new Scanner(file);
     
    		System.out.print("Enter the substring you would like to find" + 
    			" the occurance of:  ");
    		String userSearch = keyboard.nextLine();
    		int stringSize = userSearch.length();
    		int stringStart = userSearch.charAt(stringSize);
     
    		while(stringStart != -1 && stringSize != -1)
    		{
    			count++;
    		}
     
     
    		System.out.print(count + "occurences of the substring " + "\"" +
    			userSearch + "\" were found in " + fileName + ".");
     
    		inputFile.close();
    	}
    }

    so there is my code, the object of this program is to count the number of occurrences of a particular sub string in a file.
    My teacher said it can be done with the charAt and length methods of a String.
    My while loop is messed up, which is the main problem with my program.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: How do I fix my program?

    And what is your question? Does it compile? Does it misbehave? Are there exceptions? Please see the link in my signature entitled getting help

  3. #3
    Member
    Join Date
    Oct 2011
    Posts
    40
    My Mood
    Stressed
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: How do I fix my program?

    So it appears that you've given us the code your teacher gave to you, and you need to fill in the while loop? You're right, that's where the problem is. At this point, your while loop really doesn't do anything useful at all, does it?
    Do you have an idea of what is should accomplish?

    You want to find some substrings, so you're going to be moving along the file. You want your while loop to exit when you run out of string to traverse, so you must be deleting stuff from the string after you look at it. You know how long your string is going to be (stringLength).

    So, start moving through the string, evaluating things using charAt and comparing them with your string, then deleting stuff you've already looked at.

  4. #4
    Junior Member
    Join Date
    Nov 2011
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How do I fix my program?

    thanks for the replies.
    it does compile however it does not do much if anything like you said.
    So, start moving through the string, evaluating things using charAt and comparing them with your string, then deleting stuff you've already looked at.
    yeah that's pretty much the goal of this program. I just have to count the number of instances that the particular substring occurred. but I'm having trouble constructing the proper loop to do the job. I'm thinking a while loop would be adequate..

  5. #5
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: How do I fix my program?

    facepalm.jpg

    So implement the while loop and come back to us if you have a specific problem with it...

Similar Threads

  1. Help with class program!!! STUCK! Program not doing what I Want!!!
    By sketch_flygirl in forum What's Wrong With My Code?
    Replies: 7
    Last Post: April 4th, 2011, 07:29 AM