Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

help please

Name: Anonymous 2011-01-13 20:48

trying to finish this lab and keep getting these odd runtime errors, here's my errors
--------------------Configuration: <Default>--------------------
Exception in thread "main" java.lang.NullPointerException
    at Words.setWords(Words.java:32)
    at Words.<init>(Words.java:23)
    at Lab16c.main(Lab16c.java:16)

Process completed.


MAIN CODE

//© A+ Computer Science  -  www.apluscompsci.com
//Name -
//Date -
//Lab  -

import java.util.Scanner;
import java.util.ArrayList;
import java.util.Collections;
import static java.lang.System.*;

public class Lab16c
{
    public static void main( String args[] )
    {
        String wordList = "one, two, three, four, five, six, seven, alligator";
        Words test = new Words(wordList);
        out.println(test);
        out.println("word with 2 vowels = "+test.countWordsWithXVowels(2));
        out.println("word with 3 vowels = "+test.countWordsWithXVowels(3));
        out.println("word with 4 vowels = "+test.countWordsWithXVowels(4));
        out.println("word with 2 chars = "+test.countWordsWithXChars(2));
        out.println("word with 4 chars = "+test.countWordsWithXChars(4));
        out.println("word with 5 chars = "+test.countWordsWithXChars(5));
        test.removeWordsWithXChars(3);
        out.println("\nafter removing words with 3 chars \n"+test);
        out.println("\n\n");
        System.out.printLn(test.setWords);


        //more test cases

    }
}

Name: BY THE WAY THIS IS JAVA 2011-01-13 20:49

//© A+ Computer Science  -  www.apluscompsci.com
//Name -
//Date -
//Lab  -

import java.util.Scanner;
import java.util.ArrayList;
import java.util.Collections;
import static java.lang.System.*;

public class Words
{
    private ArrayList<String> words;
    String bob;

    public Words()
    {
        setWords("");
    }

    public Words(String wordList)
    {
        setWords(wordList);

    }

    public void setWords(String wordList)
    {
        Scanner sc = new Scanner(wordList);
        while(sc.hasNext())
        {
            words.add(sc.next());
        }
        System.out.println(words);
    }

    public int countWordsWithXChars(int size)
    {
        int count=0;
        for(int i = 0; i < words.size(); i++)
        {
            if(words.get(i).length()==size)
            {
                count++;
            }
        }
        return count;
    }

    public void removeWordsWithXChars(int size)
    {
        for(int i=0; i<words.size(); i++)
        {
            if(words.get(i).length()==size)
            {
                words.remove(i);
            }
        }






    }

    public int countWordsWithXVowels(int numVowels)
    {
        int count=0;
        String str = "";
        for(int i = 0; i < words.size(); i++)
        {
            for ( int b = 0; i < words.get(i).length(); i++)
            {
                str += words.get(i).charAt(b);
                if(str.equalsIgnoreCase("a")||str.equalsIgnoreCase("e")||str.equalsIgnoreCase("i")||str.equalsIgnoreCase("o")||str.equalsIgnoreCase("u"))
                {
                    count++;
                }
                str="";
            }
        }

        return count;
    }

    public String toString()
    {
        return "after re";

    }
}

Name: Anonymous 2011-01-13 23:39

I don't blame the JVM for hating you.  In fact, I wouldn't complain if any compiler decided it should save itself from you by turning the whole computer off.  But enough of that.

import static java.lang.System.*;
Least of all concerns, you don't need to import SystemSystem can everywhere you go.  But what's the point of making it a static import?

In Main ...
System.out.printLn(test.setWords);
I'm sure you wanted to do something witty here.

In Words.setWords() ...
System.out.println(words);
I didn't know you could print an ArrayList like that.

Please use this more, where it is warranted, and I don't think Words benefits from being a public class.

I'll throw you a bone:
public int countWordsWithXVowels(int numVowels) throws Exception
{
   int count = 0, i = 0;
   for(; i < this.words.size(); ++i)
   {
      String str = ((String)this.words.get(i)).toLowerCase();
      if(str.matches("[aeiou]"))
      {
         str = str.replaceAll("[b-z^[eiou]]", "");
         if(str.length() == numVowels) ++count;
   }
   return count;
}

Now we should be counting WORDS with a specific number of VOWELS.  Please doublecheck the regex parameters for me; I've never been good with that.

Name: Anonymous 2011-01-13 23:52

System can everywhere you go.
The whole thing?

Name: Anonymous 2011-01-14 3:50

>>2
import static java.lang.System.*;
Stopped reading right there. I bet you also write using namespace std; in C++. Faggot.

Name: Anonymous 2011-01-14 5:25

>>4
Not in space.

Name: Anonymous 2011-01-14 6:28

>>4
I lol'd

Name: Anonymous 2011-01-14 12:38

>>4
nice.

Name: Anonymous 2011-01-14 12:48

>>6
When properly rad-hardened and shielded, it should be fine.

Name: Anonymous 2011-02-04 17:08

Name: Anonymous 2011-02-17 19:54

check my doubles
Don't change these.
Name: Email:
Entire Thread Thread List