Today I learned about the .equals() method for comparing strings to find out if they are equal or not. Using the == equality operator returns true if both objects are the same but it using a reference in memory to compare the two instead of the value of the string. So the best way to do it would be …
String string1 = "Hello";
String string2 = "Hello";
String string3 = "Another String";
System.out.println(myStr1.equals(string2)); // Returns true because they are equal
System.out.println(myStr1.equals(string3)); // false
// instead of doing the following
string1 == string 2
I also went over how to use the .substring() method to select the characters in a string. For example:
String string = new String("Lets Code");
// We can use the .substring() method to select a part of a string.
System.out.println(string.substring(8));
// This will print out: de because the index of d is 8 and the substring method prints out the rest of the string.
// You can also select a beginning and end to pull out just the middle of a string:
System.out.println(string.substring(2, 4));
// This would pring out: et because it selects 2nd index which is "e" and the last index it selects will print the letter before it "t" because the character at index 4 is "s".
I also made a basic interest calculator and a compound interest calculator to practice. Here is the code.
package com.skillstorm.general;
import java.util.Scanner;
public class InterestCalculator {
public static void main(String[] args) {
// Scanner to read user input
Scanner in = new Scanner(System.in);
System.out.println("-----------------------------------------------------------------------");
System.out.println(" - Casey Spaulding's Simple Interest Calculator - ");
System.out.println("-----------------------------------------------------------------------");
System.out.println("");
System.out.println("Welcome to the Simple Interest Calculator! ");
System.out.println("");
System.out.println("Please enter your principle:");
System.out.println("");
double principle = in.nextDouble();
System.out.println("Thank you. Next, please enter your rate as a percentage:");
System.out.println("");
double rate = in.nextDouble();
System.out.println("Next, please enter the amount of years:");
double years = in.nextDouble();
// Calculate simple interest
double simpleInterest = (principle * rate * years) / 100;
// Result
System.out.println("Compound Interest Amount = $" + simpleInterest);
//Part 2
System.out.println("-----------------------------------------------------------------------");
System.out.println(" - LEVEL 2! Casey Spaulding's Compound Interest Calculator - ");
System.out.println("-----------------------------------------------------------------------");
System.out.println("");
System.out.println("Welcome to the Compound Interest Calculator! ");
System.out.println("");
System.out.println("Please enter your principle:");
System.out.println("");
double principle2 = in.nextDouble();
System.out.println("Thank you. Next, please enter your rate as a percentage:");
System.out.println("");
double rate2 = in.nextDouble();
System.out.println("Next, please enter the amount of years:");
double years2 = in.nextDouble();
System.out.println("Enter the time interest is compounded per year: ");
double n = in.nextDouble();
// Calculate compound interest
double amount = principle2 * Math.pow(1 + (rate / n), n * years);
double compoundInterest = amount - principle2;
// Result
System.out.println("Compound Interest after " + years + " years is: = " + compoundInterest);
System.out.println("");
System.out.println("Thank you for trying my program! Have a great day!");
}
}