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

Thread: Need help with array error

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

    Default Need help with array error

    I've been doing this project for school, and everything works fine unless the length of text is longer than about 50 characters. If it is longer, everything will work except the array, which crashes the browser unless the script is stopped.

    I am using the latest version of firefox.
    I am fairly sure the problem is not my computer.

    Here is my code:


    <html>
    <head>

    <script language="JavaScript">


    function MsgBox (textString){
    alert(textString);
    var pass=textString;
    var numWo1=pass.split(" ");
    var numWo=numWo1.length;
    alert('The number of words: ' + numWo);

    var numLet=pass.replace(/[^A-Z]/gi, "");
    alert('The number of letters: ' + numLet.length);

    var numSen1=pass.split(".");
    var numSen2=pass.split("?");
    var numSen3=pass.split("!");
    var numSen=numSen1.length + numSen2.length + numSen3.length - 3
    alert('The number of sentences: ' + numSen);

    var avgWor=numWo / numSen;
    alert('Average words per sentence: ' + avgWor);

    var freFre1=pass.replace(/[.,!?-]/g, "");
    var freFre=freFre1.split(" ");



    var avgLet=numLet.length / numWo;
    alert('Average letters per word: ' + avgLet);
    var numCoun = 0;
    var freLet1 = 0;
    var freLet2 = 0;
    var freLet3 = 0;
    var freLet4 = 0;
    var freLet5 = 0;
    var freLet6 = 0;
    var freLet7 = 0;
    var freLet8 = 0;
    var freLet9 = 0;
    var freLet10 = 0;
    var freLet11 = 0;
    var freLet12 = 0;
    var freLet13 = 0;
    var freLet14 = 0;
    var freLet15 = 0;
    var freLet16 = 0;
    var freLet17 = 0;
    var freLet18 = 0;
    var freLet19 = 0;
    var freLet20 = 0;


    while(numCoun != freFre.length){
    if(freFre[numCoun].length == 1){
    freLet1 = freLet1 + 1
    numCoun = numCoun + 1}
    else if(freFre[numCoun].length == 2){
    freLet2 = freLet2 + 1
    numCoun = numCoun + 1}
    else if(freFre[numCoun].length == 3){
    freLet3 = freLet3 + 1
    numCoun = numCoun + 1}
    else if(freFre[numCoun].length == 4){
    freLet4 = freLet4 + 1
    numCoun = numCoun + 1}
    else if(freFre[numCoun].length == 5){
    freLet5 = freLet5 + 1
    numCoun = numCoun + 1}
    else if(freFre[numCoun].length == 6){
    freLet6 = freLet6 + 1
    numCoun = numCoun + 1}
    else if(freFre[numCoun].length == 7){
    freLet7 = freLet7 + 1
    numCoun = numCoun + 1}
    else if(freFre[numCoun].length == 8){
    freLet8 = freLet8 + 1
    numCoun = numCoun + 1}
    else if(freFre[numCoun].length == 9){
    freLet9 = freLet9 + 1
    numCoun = numCoun + 1}
    else if(freFre[numCoun].length == 10){
    freLet10 = freLet10 + 1
    numCoun = numCoun + 1}
    else if(freFre[numCoun].length == 11){
    freLet11 = freLet11 + 1
    numCoun = numCoun + 1}
    else if(freFre[numCoun].length == 12){
    freLet12 = freLet12 + 1
    numCoun = numCoun + 1}
    else if(freFre[numCoun].lenth == 13){
    freLet13 = freLet13 + 1
    numCoun = numCoun + 1}
    else if(freFre[numCoun].length == 14){
    freLet14 = freLet14 + 1
    numCoun = numCoun + 1}
    else if(freFre[numCoun].length == 15){
    freLet15 = freLet15 + 1
    numCoun = numCoun + 1}
    else if(freFre[numCoun].length == 16){
    freLet16 = freLet16 + 1
    numCoun = numCoun + 1}
    else if(freFre[numCoun].length == 17){
    freLet17 = freLet17 + 1
    numCoun = numCoun + 1}
    else if(freFre[numCoun].length == 18){
    freLet18 = freLet18 + 1
    numCoun = numCoun + 1}
    else if(freFre[numCoun].length == 19){
    freLet19 = freLet19 + 1
    numCoun = numCoun + 1}
    else if(freFre[numCoun].lenth == 20){
    freLet20 = freLet20 + 1
    numCoun = numCoun + 1}
    }
    alert('Frequency of word length' + '\n1 Letter: ' + freLet1 + '\n2 Letters: ' + freLet2 + '\n3 Letters: ' + freLet3 + '\n4 Letters: ' + freLet4 + '\n5 Letters: ' + freLet5 + '\n6 Letters: ' + freLet6 + '\n7 Letters: ' + freLet7 + '\n8 Letters: ' + freLet8 + '\n9 Letters: ' + freLet9 + '\n10 Letters: ' + freLet10 + '\n11 Letters: ' + freLet11 + '\n12 Letters: ' + freLet12 + '\n13 Letters: ' + freLet13 + '\n14 Letters: ' + freLet14 + '\n15 Letters: ' + freLet15 + '\n16 Letters: ' + freLet16 + '\n17 Letters: ' + freLet17 + '\n18 Letters: ' + freLet18 + '\n19 Letters: ' + freLet19 + '\n20 Letters: ' + freLet20);



    }

    </script>

    </head>



    <body>

    <form>
    <input name="passa" TYPE=Text>
    <input name="submit" TYPE=Button VALUE="Show Me" onClick="MsgBox(form.passa.value)">
    </form>

    </body>
    </html>

    --- Update ---

    well found my first error, mispelled the word length xD

    --- Update ---

    even though i fixed that error, the code still does not work if it is the string is too long


  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: Need help with array error

    The code does not look like java.

    Try asking the question on a javascript forum.
    If you don't understand my answer, don't ignore it, ask a question.

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

    Default Re: Need help with array error

    Quote Originally Posted by Norm View Post
    The code does not look like java.

    Try asking the question on a javascript forum.
    theres a difference?

  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: Need help with array error

    Yes. Look at any of the other threads on this forum for java code examples.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Help with error on second run through array
    By tyneframe in forum Collections and Generics
    Replies: 18
    Last Post: November 25th, 2012, 08:13 PM
  2. Replies: 2
    Last Post: October 23rd, 2012, 09:37 AM
  3. Replies: 1
    Last Post: October 22nd, 2012, 07:19 PM
  4. error: array dimension missing x 2. Any help?
    By giga in forum What's Wrong With My Code?
    Replies: 7
    Last Post: June 30th, 2012, 09:50 AM
  5. Error for a simple array
    By DudeJericho in forum Collections and Generics
    Replies: 4
    Last Post: April 25th, 2011, 02:46 PM

Tags for this Thread