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: int's not adding together for some unknown reason

  1. #1
    Junior Member
    Join Date
    Mar 2018
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default int's not adding together for some unknown reason

    I for the life of my cannot figure out why this code isnt working.
    I want the integer trueLength to be the collective values of all the integers in the int array totals. For some reason I keep getting zero as the total even though totals contains numbers greater than zero.

    Please help.

    public int[] FrequencyFinder(File textfile) throws IOException
    {
    Scanner infile = new Scanner(textfile);
    int trueLength = 0;
    if(infile.hasNext())
    {
    file += infile.nextLine();
    //System.out.println(file);
    }
    String[] alphabet = {"a","b","c","d","e","f","g","h","i","j","k","l"," m","n","o","p","q","r","s","t","u","v","w","x","y" ,"z"};
    int[] totals = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,0, trueLength};

    for(int cc = 0; cc < file.length(); cc++)
    {
    for(int aa = 0; aa < alphabet.length; aa++)
    {
    if(file.substring(cc,cc + 1).equalsIgnoreCase(alphabet[aa]))
    totals[aa] ++;
    }

    }

    for(int x = 0; x < totals.length; x++)
    {
    trueLength += totals[x];
    System.out.println(totals[x]);
    }
    return totals;

  2. #2
    Junior Member
    Join Date
    Mar 2018
    Posts
    10
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: int's not adding together for some unknown reason

    Looks like the totals array was initialized with the starting value of trueLength, which was zero. Also it looks like the variable trueLength was calculated to be the total of the values in the totals array from 0 to 25 but totals[26] was not then updated with the final value of trueLength.

    So the question is was the final value of trueLength used to update the totals array?

  3. #3
    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: int's not adding together for some unknown reason

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 3
    Last Post: January 26th, 2013, 03:34 PM
  2. Reason for swing component overlapping?
    By Furious5k in forum AWT / Java Swing
    Replies: 1
    Last Post: November 6th, 2011, 08:40 AM
  3. Replies: 2
    Last Post: September 8th, 2011, 09:50 PM
  4. hmm for some reason the char.At isnt working
    By dvsumosize in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 2nd, 2010, 04:27 AM

Tags for this Thread