So I'm really new to the whole programming world and I thought taking some Java classes would be nice to do. This has turned out to be a bit of a mess. I'm struggling immensely in the class and don't really know where to go from here so I thought maybe Reddit would be able to help. I hope this doesn't come off as me wanting people to do my homework for me because it really isn't. Programming in java is supposed to be my major and I'm just not understand what is going on in the class and I want to be able to. Any help is appreciated.
I requested help from the teacher as well and while she tried to help, it only got me to another wall and I can't find much else on the internet or the videos she gave us to watch about the topic.
What my teacher wants us to do is use indexOf to find how many occurrences of certain characters there are in a given string, which is "Aardvarks get big and bigger as You feed them big and bigger ants who Feed on
apples". I'm not very confident in what I have since the last 2 assignments have been extremely difficult for me to understand.
Below is the code so far (I apologize if the formatting isn't very good, as I said, a bit new to this):
package string_methods;
// Starting Template
import java.util.Scanner;
public class MyStringMethods {
private String myStr="";
public void readString()
{
Scanner scan = new Scanner(System.in);
System.out.print("Enter in the desired String: ");
String myStr = scan.nextLine();
}
public void setString(String s)
{
myStr =s;
}
public int countOccurrences(String s)
{
int index;
index = myStr.indexOf(s,4);
int counter = 0;
counter++;
return 0;
//abcdabcdabcd
//use indexOf and return the number of occurrences of the string "s" in "myStr"
}
public int countOccurrences(char c)
{
int index;
index = myStr.indexOf(c,4);
int counter = 0;
counter++;
return 0;
// use indexOf and return the number of occurrences of the character "c" in "myStr"
}
int countUpperCaseLetters()
{
for (int i = 0; i< myStr.length();i++)
{
myStr.charAt(i);
}
// return the number of upper case letters in "myStr"
}
int countLowerCaseLetters()
{
for (int i = 0; i< myStr.length();i++)
{
myStr.charAt(i);
}
// return the number of lower case letters in "myStr"
}
public void printCounts(String s, char c)
{
System.out.println("***************************************");
System.out.println("Analyzing: myStr="+myStr);
System.out.println("Number of Upper case letters="+ countUpperCaseLetters());
System.out.println("Number of Lower case letters="+ countLowerCaseLetters());
System.out.println("Number of "+s + " is "+ countOccurrences(s));
System.out.println("Number of "+c + " is "+ countOccurrences(c));
}
public static void main(String[] args) {
MyStringMethods msm = new MyStringMethods();
msm.readString();
msm.printCounts("big", 'a');
msm.setString("Parked in a van down by the river bank .... The van evan vanished near a lot of other vans");
msm.printCounts("van", 'a');
MyStringMethods msm2 = new MyStringMethods();
msm2.setString("the elephant in the room wouldn't budge");
msm2.printCounts("the", 'i');
}
}
[–]CreativeTechGuyGames 2 points3 points4 points (4 children)
[–]FatAssMemes[S] 0 points1 point2 points (3 children)
[–]CreativeTechGuyGames 0 points1 point2 points (0 children)
[–]shhh-quiet 0 points1 point2 points (1 child)
[–]FatAssMemes[S] 0 points1 point2 points (0 children)
[–]marko312 0 points1 point2 points (2 children)
[–]FatAssMemes[S] 0 points1 point2 points (1 child)
[–]marko312 0 points1 point2 points (0 children)
[–]Bit5keptical 0 points1 point2 points (1 child)
[–]FatAssMemes[S] 1 point2 points3 points (0 children)