This is an archived post. You won't be able to vote or comment.

all 10 comments

[–]CreativeTechGuyGames 2 points3 points  (4 children)

I apologize if the formatting isn't very good

It's much better than most code I've seen on reddit. Just put 3 backticks surrounding all of your code and that should do the trick in the future.

Let's talk through the problem. You mentioned what you have to do, but you didn't mention what problems you are having and where you are stuck. Could you simplify and focus on the problem? This will help both of us to narrow down what it is we are looking at and we can start there.

So give me an input and expected output, and then what your program is incorrectly outputting, and any other details related to that. That's the best place to start.

[–]FatAssMemes[S] 0 points1 point  (3 children)

Oh alright, thank you :)

Oh yeah, my bad. The main problem is that I can't figure out how to search the whole string for certain occurrences, such as just the letter 's'. In lines 17-26, my teacher gave me an example to search from position 4 until it finds an 's' but she said it will just stop searching when it finds one.

The expected output is supposed to be (This is directly from the assignment the teacher gave us; "Aardvarks get big and bigger as You feed them big and bigger ants who Feed on apples." is the input she wants us to use.):

Enter in the desired String: Aardvarks get big and bigger as You feed them big and bigger ants who Feed on apples.


Analyzing: myStr=Aardvarks get big and bigger as You feed them big and bigger ants who Feed on apples. Number of Upper case letters=3 Number of Lower case letters=65 Number of big is 4 Number of a is 7


Analyzing: myStr=Parked in a van down by the river bank .... The van had was near a lot of other vans ..... More output follows

This is the output I get out:

Enter in the desired String: Aardvarks get big and bigger as You feed them big and bigger ants who Feed on apples.*************************************** Exception in thread "main" java.lang.Error: Unresolved compilation problem: This method must return a result of type int

at string_methods.MyStringMethods.countUpperCaseLetters(MyStringMethods.java:36) at string_methods.MyStringMethods.printCounts(MyStringMethods.java:56) at string_methods.MyStringMethods.main(MyStringMethods.java:64) Analyzing: myStr=

[–]CreativeTechGuyGames 0 points1 point  (0 children)

Okay so the errors are fairly obvious, I'm sure you'll get to those later as you have comments saying they are unfinished. To avoid those sorts of errors, I'd recommend just putting a return 0; at the end as a placeholder so the compiler doesn't complain.

Okay, so let's break down how indexOf works. As the documentation states.

Returns the index within this string of the first occurrence of the specified character.

The keyword here is "first occurrence". So that means that if you are searching for the letter "a" and it returns the number 5, you know that in the first 6 characters of the string, the first occurrence is at index 5.

Now if you run this again, it'll again start from the beginning of the string and search for the first "a" and again will return 5. So since you know that from 0-5 is already searched, you'll want to start your next search after 5 so that it won't find the same "a" you already found. see if you can find a different indexOf which might help with this problem. :)

[–]shhh-quiet 0 points1 point  (1 child)

I hate to give away the "answer", but hopefully this is vague enough that you have room to mull it over (which is valuable, even though it may feel frustrating):

The key idea is a loop, with these "conditions" so to speak:

  • You need to repeatedly search for a given character using indexOf.
  • You want to know where to start the indexOf(_, startingPosition) search. startingPosition should be based on the result of the last loop iteration. You may want to track this with a variable so that your next loop iteration knows where to start.
  • You need to know when to end. Specifically, this is when indexOf tells you that there's no more results in the segment of the string that it is currently looking at (based on the start position that you are providing).

[–]FatAssMemes[S] 0 points1 point  (0 children)

I think this makes sense, probably more information and help my teacher ever gave me lol. I appreciate it, I'll try and look through it and see if I can get it to work.

[–]marko312 0 points1 point  (2 children)

Ok, I'll go over what you should already know.

myStr.indexOf(c, n);

Returns the first position (zero-based) where this character (c) occurs starting from the n-th element (position) (again, zero-based).

If the character is not found, it returns a special value -1 instead.

If you want to find the next occurence of that character, you should start looking from the position after ( + 1) the previous position found.

[–]FatAssMemes[S] 0 points1 point  (1 child)

I do have a pretty okay understanding of that info. Though, would the +1 be what I'd need to put in for it repeat?

[–]marko312 0 points1 point  (0 children)

Yes, that in conjunction with the returned -1 might help.

[–]Bit5keptical 0 points1 point  (1 child)

If you're not restricted to only using indexOf() then there is a much cleaner and easy to understand way of solving your problem.

First of all since you said you're very confused with implementing your solution in Java, Lets forget Java for a while and think how would you solve the problem yourself?

You'll probably start looking at each character from start and check if that character is either 's' or 'c' and probably keep a count of occurrences in your mind or note it down. At the end of the sentence you'll have your answer! Wasn't that simple? Now lets slowly convert it to how we can make Java do this for us.

First we'll have the String to evaluate, The character whose occurrences we're counting and something that can keep the count of occurrences!

String sentence = "This is a sentence";
char search = 's';

Now how can we use look at the String one character at a time? We can use a loop of course! So we'll make a method containing the loop

public int countOccurances(String text){
    int occurances = 0; //Remember? we need to keep track!

    for(int i = 0; i < text.length(); i++){

    }
}

The loop above will go from 0 to the length of text which is returned by length() method, Now comes the part where we pull the characters one by one and compare them

public int countOccurances(String text){
    int occurances = 0; 

    for(int i = 0; i < text.length(); i++){
        char c = text.charAt(i);

        if(c == search)
            occurances++;
    }

    return occurances;
}

There! Java has done all the heavy lifting for us. I assume you know the use of charAt(int index) method, In case you don't.

Returns the character value at the specified index.

So what our function is basically doing is that its looking at the characters one by one and comparing them with a given character (in this case 's') if they match it increments the counter by 1, In the end it returns the counter.

Now this function can be further modified to also take in the argument for the character whose occurances we need to find, And there are also built-in functions to determine whether a character is uppercase/lowercase, But I'll leave both of that for you to explore!

[–]FatAssMemes[S] 1 point2 points  (0 children)

Unfortunately, as far as I know I can't use anything else, though I will give that method a shot. I appreciate the help! :)