Results 1 to 11 of 11

Thread: Yo anyone who knows code and stuff

  1. #1
    Registered Users Regular Flaming Flamingo's Avatar
    Join Date
    Jan 2012
    Posts
    515

    Default Yo anyone who knows code and stuff

    I cannot figure out what the problem with this is. It is supposed to count the number of e's in a sentence but it keeps going into infinite loops every time I try to run it.

    public class wordStuff
    {
    public static int countEs(String sent)
    {
    int numE=0;
    int oneUPe=0;

    while(sent.substring(oneUPe,sent.length()).indexOf ("e")>=0)
    {
    numE++;
    oneUPe=sent.substring(oneUPe,sent.length()).indexO f("e")+1;
    }
    return numE;
    }
    public static void main(String []args)
    {
    System.out.print(countEs("cheese"));
    }
    }

  2. #2
    Defenestration is imminent pichubro's Avatar
    Join Date
    Mar 2005
    Location
    Kaleidoscope Of Mathematics
    Posts
    18,317

    Default

    because you are using a while loop, which are known to do infinite loops if you aren't careful.

    Try using a for loop instead.

  3. #3
    Registered Users Regular Flaming Flamingo's Avatar
    Join Date
    Jan 2012
    Posts
    515

    Default

    but this doesn't work either.

    public class wordStuff
    {
    public static int countEs(String sent)
    {
    int numE=0;

    for(int oneUPe=0; oneUPe<sent.length(); )
    {
    if(sent.substring(oneUPe,sent.length()).indexOf("e ")>=0)
    {
    numE++;
    oneUPe=sent.substring(oneUPe,sent.length()).indexO f("e")+1;
    }
    else
    {
    oneUPe=sent.length();
    }
    }
    return numE;
    }
    public static void main(String []args)
    {
    System.out.print(countEs("cheese"));
    }
    }

  4. #4
    Registered Users Regular Flaming Flamingo's Avatar
    Join Date
    Jan 2012
    Posts
    515

    Default

    I think I've got it

  5. #5
    princeso Kirby's Avatar
    Join Date
    Mar 2009
    Posts
    19,119

    Default

    hmm

    why do you make your letter counter equal to the length counter if the letter is not e, why aren't you just adding the letter counter (up I think) by one each time

  6. #6
    Registered Users Regular Flaming Flamingo's Avatar
    Join Date
    Jan 2012
    Posts
    515

    Default

    All I needed to do was add this to this line.

    oneUPe=sent.substring(oneUPe,sent.length()).indexO f("e")+1;

    oneUPe=sent.substring(oneUPe,sent.length()).indexO f("e")+oneUPe+1;

    or I could do this

    oneUPe+=sent.substring(oneUPe,sent.length()).indexO f("e")+1;

    now it works fine

    public class wordStuff
    {
    public static int countEs(String sent)
    {
    int numE=0;
    int oneUPe=0;

    while(sent.substring(oneUPe,sent.length()).indexOf ("e")>=0)
    {
    numE++;
    oneUPe=sent.substring(oneUPe,sent.length()).indexO f("e")+oneUPe+1;
    }
    return numE;
    }
    public static void main(String []args)
    {
    System.out.print(countEs("cheese"));
    }
    }
    Last edited by Flaming Flamingo; 02-19-2013 at 05:10 PM.

  7. #7
    Registered Users Regular Rayne's Avatar
    Join Date
    Mar 2009
    Posts
    8,614

    Default

    There's an easier way to do this. Convert the string to a character array using String.toCharArray() and then iterate through it, incrementing a counter whenever you find an 'e'.

  8. #8
    The One and Only trixie's Avatar
    Join Date
    Oct 2004
    Location
    In your dreams.
    Posts
    10,108

    Default

    I'd have just used a character array to begin with.
    Originally made by LM:


    ~ I have said nothing because there is nothing I can say that would describe how I feel as perfectly as you deserve it. -- Kyle Schmidt ~

    ~Silence is one of the hardest arguments to refute. -- Josh Billings ~

    * dragon_berry**Fallen_Wings*

  9. #9
    Registered Users Regular Flaming Flamingo's Avatar
    Join Date
    Jan 2012
    Posts
    515

    Default

    We're only supposed to use the methods on a key that we get for the AP test.

  10. #10
    Registered Users Regular Rayne's Avatar
    Join Date
    Mar 2009
    Posts
    8,614

    Default

    That's really dumb.

  11. #11
    bye felicia stiles's Avatar
    Join Date
    Aug 2012
    Posts
    1,583

    Default

    damn, i still havent even taken a computer science course. im still catching up on prereqs lol... college is going to take me forever

    Eggplant Friday
    A popular social media hashtag where men with large penis' take pictures of their bulges through their sweatpants which resemble an eggplant.
    Lisa : "Did you see johnnys eggplant Friday post on Instagram?"'
    Tonya : "OMG yes girl, he's packing some heat."

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •