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

Thread: Help with Java program that counts strings and tallies results

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    20
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Help with Java program that counts strings and tallies results

    Basically my java program is supposed to calculate the length of each string in anArray. That isn't too much of problem HOWEVER I can't seem to figure out how to collect strings of the same length and have them as a tally. ie. if there is an array of strings "The dog isn't second first" it will print out length of each string "3 3 5 6 5" but also collect the same lengths "3 3", "5 5" and have them as an output: |Value: 3 Tally:2 Value:5 Tally:2| (note: it doesn't have to be in this form). Anyone have any ideas?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with Java program that counts strings and tallies results

    Please post your code and any specific questions about problems you are having.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    20
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Help with Java program that counts strings and tallies results

    [CODE = java]
    int anArray[] = new int[max]; //This is just a value i determined earlier that calculated the max length of the array
    int sum = 1;
    int count = 0;
    int i = 0;
    String sentence[] = new String[max];
    for (count = 0; count < anArray.length; count++) {
    System.out.println("Length: " + args[count].length());
    //Now it supposed to calculate the sum after every count which is equal. This is a problem as it looks retarded and it is retarded because it doesn't work.
    } if (sentence[count]==sentence[count]||
    sentence[count]==sentence[count+1]||
    sentence[count]==sentence[count+2]||
    sentence[count]==sentence[count+3]||
    sentence[count]==sentence[count+4]||
    sentence[count]==sentence[count+5]){
    sum ++;
    System.out.println("Tally: "+sum);
    }

    }
    }
    [/CODE]

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with Java program that counts strings and tallies results

    What problems are you having? Do you have any specific questions?

    BTW There should not be spaces in first code tag: [code=java]
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Mar 2013
    Posts
    20
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Help with Java program that counts strings and tallies results

    oh right.
    int anArray[] = new int[max]; //This is just a value i determined earlier that calculated the max length of the array
    int sum = 1;
    int count = 0;
    int i = 0;
    String sentence[] = new String[max];
    for (count = 0; count < anArray.length; count++) {
    System.out.println("Length: " + args[count].length());
    //Now it supposed to calculate the sum after every count which is equal. This is a problem as it looks retarded and it is retarded because it doesn't work.
    } if (sentence[count]==sentence[count]|| 
    sentence[count]==sentence[count+1]||
    sentence[count]==sentence[count+2]||
    sentence[count]==sentence[count+3]||
    sentence[count]==sentence[count+4]||
    sentence[count]==sentence[count+5]){
    sum ++;
    System.out.println("Tally: "+sum);
    }
     
    }
    }
    I'm getting anArray index out of bounds exception even though the length after the for loop still prints out. What I'm also looking for is a method which 1, isn't as retarded as mine, and 2, actually works properly.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with Java program that counts strings and tallies results

    getting anArray index out of bounds exception
    Where? What is the value of the index and the size of the array?
    Array indexes range in value from 0 to the array length-1

    What I'm also looking for is a method
    What steps does the method need to do to solve the problem? You need to make a design for the program before trying to code it. Make a list of the steps the program needs to do and then work on writing the code to do those steps.

    BTW the code is NOT formatted properly. ALL the Statements should not start in the first column.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Mar 2013
    Posts
    20
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Help with Java program that counts strings and tallies results

    Oh I forgot, my array is "the dog isn't first second" I can change it to anything but thats what it is at the moment. The max length is therefore the longest word, which is 6 characters. 4
    The error (an unusual one) is split by the for loop after the count reaches 2 (it reads: Exception in thread "main" Length: 5) and then at the bottom line reads:
    java.lang.ArrayIndexOutOfBoundsException: 5
    at Words.main(Words.java:18)
    I can't really make anything of it because the max length is 6 so it shouldn't be out of bounds

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with Java program that counts strings and tallies results

    java.lang.ArrayIndexOutOfBoundsException: 5
    If the array has 5 elements, the max valid index is 4.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Mar 2013
    Posts
    20
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Help with Java program that counts strings and tallies results

    Hmm ok. I'll have a think and get back to you. Maybe change some things in my code. Thanks for the help.

    --- Update ---

    Hey right now I fixed my problem of the exception out of bounds (which is great!) but now I have a another problem:
    Exception in thread "main" java.lang.NullPointerException
    at Words.main(Words.java:20)
    This is what is causing the problem:
    if (sentence[0].length() == sentence[0].length()
    				|| sentence[0].length() == sentence[1].length()
    				|| sentence[0].length() == sentence[2].length()
    				|| sentence[0].length() == sentence[3].length()
    				|| sentence[0].length() == sentence[4].length()) {
    			sum++;
    			System.out.println("Tally: " + sum);
    		}
    	}
    }
    I made it a bit simpler to understand for now so I took it out of the for loop and replaced count with what it supposed to do for the first loop. Do you suggest any solutions?

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with Java program that counts strings and tallies results

    Exception in thread "main" java.lang.NullPointerException
    at Words.main(Words.java:20)
    There is a variable being used on line 20 that has a null value. Look at line 20, find the variable with the null value and then back track to see why that variable does not have a valid non-null value.

    What is the code in post#9 supposed to do? It looks like it should be replaced with a loop. All those specific array indexes is the wrong way to work with arrays. What if the size of the array is changed?
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Junior Member
    Join Date
    Mar 2013
    Posts
    20
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Help with Java program that counts strings and tallies results

    Yes you're right. Both my arrays have no Strings in them which is why they don't produce any result. How would I make a new array which uses the strings from (String []args) without being called args?

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with Java program that counts strings and tallies results

    Do you want to copy the contents of one array to another array?
    Define the target array and use a loop to copy the elements from one array to the other.

    What is wrong with the name: args?
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Junior Member
    Join Date
    Mar 2013
    Posts
    20
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Help with Java program that counts strings and tallies results

    Quote Originally Posted by Norm View Post
    What is wrong with the name: args?
    Nothing really actually I just changed my code to args[count].length instead of sentence[count].length. I don't know what I was thinking having arrays with no value.
    Use a loop to copy the elements from one array to the other
    This sounds REALLY useful! How would I do this in a code?

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with Java program that counts strings and tallies results

    define the target array the same size as the source array
    use a for loop for the length of the array
    use an assignment statement to copy element from source to target array element
    end loop
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Junior Member
    Join Date
    Mar 2013
    Posts
    20
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Help with Java program that counts strings and tallies results

    Sorry I usually understand examples a lot better. Could you show me an example?

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help with Java program that counts strings and tallies results

    Give it a try and post the code if you have problems.
    There are three statements needed:
    define target array
    for loop
    assignment statement
    If you don't understand my answer, don't ignore it, ask a question.

  17. The Following User Says Thank You to Norm For This Useful Post:

    IneedHelpwithJava (April 30th, 2013)

  18. #17
    Junior Member
    Join Date
    Mar 2013
    Posts
    20
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Help with Java program that counts strings and tallies results

    OK. Anyway thanks for the help, honestly couldn't do the rest of the code without it!

Similar Threads

  1. unexpected results to simple program
    By Pajaro in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 30th, 2013, 12:51 PM
  2. Replies: 0
    Last Post: March 1st, 2013, 08:13 PM
  3. Simple computation program with strings...
    By alias22 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 6th, 2012, 01:11 PM
  4. Replies: 1
    Last Post: November 26th, 2011, 12:13 PM
  5. A program that counts the number of punctuation marks in a text file
    By Twoacross in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 21st, 2011, 04:03 PM

Tags for this Thread