If this has been asked before please do link the question post or somehow send it in my direction. I tried searching very hard on this site for a similar question but haven't yet to find one.
I'm doing a late assignment for my Java lvl 1 online class (yep I'm new to Java) and I think my while loop and switch statement is fine but I'm having trouble figuring out how to loop it again. The loop is supposed to go back to asking for user input. Before the user can input the next function this occurs in the console output:
This calcuclator requires you to enter a function and a number.
The functions are as follows:
S - Sine
C - Cosine
T - Tangent
R - Square Root
N - Natural Log
X - Exit the program
Enter a function:
s
Enter your value
40
The sine of your number is : 0.7451131604793488
S - Sine
C - Cosine
T - Tangent
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
R - Square Root
N - Natural Log
X - Exit the program
Enter a function
at java.lang.String.charAt(String.java:658)
at assigment9.Assigment9.main(Assigment9.java:110)
C:\Users\r3ds1\AppData\Local\NetBeans\Cache\8.1\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 5 seconds)
Here is my Code Sample:
import java.util.Scanner;
//whileSwitch
public class Assigment9 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("This calcuclator requires you to enter a function and a number.");
System.out.println("The functions are as follows: ");
//options
System.out.println("S - Sine");
System.out.println("C - Cosine");
System.out.println("T - Tangent");
System.out.println("R - Square Root");
System.out.println("N - Natural Log");
System.out.println("X - Exit the program");
//user input
System.out.println("Enter a function: ");
String input = in.nextLine();
char operation = input.charAt(0);
//supposed to stop when user inputs 'x'
while(!input.equals("x"))
{
switch(Character.toUpperCase(operation))
{
//Sine
case 'S':
System.out.println("Enter your value ");
double s;
double theSine;
s = in.nextDouble();
theSine = Math.sin(s);
System.out.println("The sine of your number is : " + theSine );
break;
//Cosine
case 'C':
System.out.println("Enter your value ");
double c;
c = in.nextDouble();
double theCosine;
theCosine = Math.cos(c);
System.out.println("The Cosine of your number is : " + theCosine );
break;
//tangent
case 'T':
System.out.println("Enter your value ");
double t;
t = in.nextDouble();
double theTangent;
theTangent = Math.cos(t);
System.out.println("The Tangent of your number is : " + theTangent );
break;
//Square root
case 'R':
System.out.println("Enter your value ");
double r;
r = in.nextDouble();
double theSqrt;
theSqrt = Math.cos(r);
System.out.println("The Square Root of your number is : " + theSqrt );
break;
//Natural Log
case 'N':
System.out.println("Enter your value ");
double n;
n=in.nextDouble();
double theLog;
theLog = Math.cos(n);
System.out.println("The Natural Log of your number is : " + theLog );
break;
//Exit
case 'X':
System.out.println("Thanks for using this calculator. ");
break;
}
//options
System.out.println("S - Sine");
System.out.println("C - Cosine");
System.out.println("T - Tangent");
System.out.println("R - Square Root");
System.out.println("N - Natural Log");
System.out.println("X - Exit the program");
System.out.println("Enter a function");
input = in.nextLine();
operation = input.charAt(0);
}
}
}`
So before the user can enter the next function the program spits out a StringIndexOutOfBoundsException: String index out of range: 0. How would I fix this and an explanation of why this is occurring would be very much appreciated!
Edit:Posted all of code
second edit: corrected misinformation/ gave more.