My counselor in a camp I went to over the summer gave me a code challenge. To take 6 challenges (listed below) and have all of them in one program, and have the user choose which one to use. Please review all of my code. If there is anything wrong, inefficient, or if I use bad practices, let me know; I'm still new at this. PS: If there are any methods I made that are already built into Java, ignore them, I had to reinvent the wheel
Given a list of numbers, add up all the even numbers from that list and return the sum.
Given a list of numbers, return whether that list is in order or not
Given two words, return a list of letters that occur in both words
Given a long string representing a sentence, and a shorter string representing a word, find if the word is in the sentence.
Given a string return which letter is most frequent.
Given a string, make it a palindrome. You may only add characters to the end of the string.
package Challenges;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
public class ChallengeOneToFive {
@SuppressWarnings("resource")
public static void main(String[] args) {
System.out.println("Which Challenge would you like to see?\n1: Even Number Adder \n2: Number Sorter \n3: Word Letter Occurance \n4: Sentence Words \n5: Frequent Letters \n6: Palindromes");
Scanner scan = new Scanner(System.in);
String choice = scan.nextLine();
if(choice.equals("1")) {
Challenge1();
} else if(choice.equals("2")) {
Challenge2();
} else if(choice.equals("3")) {
Challenge3();
} else if(choice.equals("4")) {
Challenge4();
} else if(choice.equals("5")) {
Challenge5();
} else if(choice.equals("6")) {
Challenge6();
}
}
@SuppressWarnings("resource")
//Challenge 1 is Completed
public static int Challenge1() {
System.out.println("How many numbers will you be entering?");
Scanner scan = new Scanner(System.in);
int numberOfNumbers = scan.nextInt();
int sum = 0;
for(int i = 0; i < numberOfNumbers; i++) {
System.out.println("Enter a number");
int number = scan.nextInt();
if(number % 2 == 0) {
sum+=number;
}
}
System.out.println("The Sum is " + sum);
return sum;
}
@SuppressWarnings({ "resource" })
public static boolean Challenge2() {
boolean inOrder = false;
System.out.println("How many numbers will you be entering?");
Scanner scan = new Scanner(System.in);
int numberOfNumbers = scan.nextInt();
int[] numbers = new int[numberOfNumbers];
for(int i = 0; i < numberOfNumbers; i++) {
System.out.println("Enter a number");
int number = scan.nextInt();
numbers[i] = number;
if(i != 0){
if((numbers[i] - numbers[i - 1]) > 0) {
inOrder = true;
}
if((numbers[i] - numbers[i - 1]) < 0){
inOrder = false;
}
} else if(i == 0){
numbers[0] = 0;
}
}
System.out.println("The fact that your numbers are in order is " + inOrder);
return inOrder;
}
//Challenge 3 is Completed
@SuppressWarnings({ "resource", "unused" })
public static String Challenge3() {
System.out.println("Enter a String");
Scanner scan = new Scanner(System.in);
String word1 = scan.nextLine();
System.out.println("Enter another String");
String word2 = scan.nextLine();
String list = "";
System.out.println("");
Character[] word1Chars = toCharacterArray(word1);
Character[] word2Chars = toCharacterArray(word2);
Set<String> s1 = new HashSet<>( Arrays.asList( word1.split( "" ) ) );
Set<String> s2 = new HashSet<>( Arrays.asList( word2.split( "" ) ) );
s1.retainAll( s2 );
for( String c: s1 ){
System.out.print(c + " ");
}
return list;
}
public static boolean Challenge4() {
boolean isInSentence = false;
System.out.println("Enter a sentence");
Scanner scan = new Scanner(System.in);
String sentence = scan.nextLine();
System.out.println("Now enter a word");
String word = scan.nextLine();
scan.close();
if(sentence.contains(word)) {
isInSentence = true;
}
if(isInSentence == true) {
System.out.println("The word you entered is in the string");
} else {
System.out.println("The word you entered is not in the string");
}
return isInSentence;
}
//Challenge 5 is Completed
@SuppressWarnings("resource")
public static char Challenge5() {
int characterCount = 0;
int maxCharacter = 0;
char maxCharacterChar = '.';
System.out.println("Enter a string");
Scanner scan = new Scanner(System.in);
String word = scan.nextLine();
Character[] cArray = toCharacterArray(word);
for(int i =0; i < cArray.length; i++)
{
int characterASCII = (int)cArray[i];
characterCount = 0;
for(int x = 0; x < cArray.length; x++)
{
if(characterASCII == (int)cArray[x])
{
characterCount ++;
//System.out.print("Character Count for " + characterASCII + " " + characterCount + "\n");
if(characterCount > maxCharacter)
{
maxCharacter = characterCount;
maxCharacterChar = cArray[i];
}
}
}
}
System.out.println("The character that occurs the most is " + maxCharacterChar);
return maxCharacterChar;
}
@SuppressWarnings("resource")
public static String Challenge6() {
System.out.println("Enter a string to be palindrome'd");
Scanner scan = new Scanner(System.in);
String palindrome = scan.nextLine();
StringBuilder sb = new StringBuilder(palindrome);
String sb2 = sb.toString();
Character[] charArray = toCharacterArray(sb2);
Character[] reverse = new Character[charArray.length];
for(int o = 0; o < sb2.length(); o++) {
reverse[o] = sb2.charAt(o);
}
for(int i = reverse.length - 1; i > -1; i--) {
sb2 = sb2 + reverse[i];
}
System.out.println(sb2);
return sb2;
}
public static Character[] toCharacterArray( String s ) {
if ( s == null ) {
return null;
}
int len = s.length();
Character[] array = new Character[len];
for (int i = 0; i < len ; i++) {
array[i] = new Character(s.charAt(i));
}
return array;
}
}
"1232"
and"22"
return[2]
, or[2, 2]
? \$\endgroup\$["b"]
. \$\endgroup\$