/**
 * Simple math application using a scanner input
 * @version 1.00 2010/1/26
 */
import java.util.*;
import java.text.*;
import java.io.*;
public class mathopr {

    public static void main(String[] args) {
        double num1,answer1,answer2,answer3=0;
        String myname;
        String reply;
        char replyChar=;
        Scanner input = new Scanner(System.in);
        DecimalFormat df= new DecimalFormat("00.00");
        while (replyChar=='y'){

        System.out.println("Please enter your name");
        myname=input.nextLine();
        System.out.println("Please enter your number");
        num1=input.nextDouble();
        answer1=Math.pow(num1,2);
        System.out.println("Thank you Mr./Mrs. "+myname+" The value of num1 is"+num1+"Raised to the second power is"+df.format(answer1));
        answer2=Math.sqrt(num1);
        System.out.println("Thank you Mr./Mrs. "+myname+" The value of num1 is"+num1+"The square root of num1 is"+df.format(answer2)); 
        System.out.println("Do you want to continue? Y or N?");
        reply=input.nextLine();
        replyChar=reply.charAt(0);
          }
    }

This is just a basic application using some math operators. I am having trouble looping however. It says my char variable may need to be initialized. I'm not quite sure how to fix this. Any help would be greatly appreciated. Thanks in advance!

Regards,

Dalymiddleboro

This line is probably giving you trouble

char replyChar=;

What are you trying to do here?

EDIT
Follow the next post. AndreiDMS gives a good solution to your problem.

Hi,

String reply;
char replyChar=; // your char variable needs to be initialized
Scanner input = new Scanner(System.in);

and you should initialize it with 'y' if you want to get into the while loop at least 1 time

I apologize the ' = ' sign shouldn't have been there, I did however initialize it to y but after I did that it had an unhandled exception inside of the loop.

Here is the new code:

/**
* Simple math application using a scanner input
* @version 1.00 2010/1/26
*/
import java.util.*;
import java.text.*;
import java.io.*;
public class mathopr {

public static void main(String[] args) {
double num1,answer1,answer2,answer3=0;
String myname;
String reply;
char replyChar='y';
Scanner input = new Scanner(System.in);
DecimalFormat df= new DecimalFormat("00.00");
while (replyChar=='y'){

System.out.println("Please enter your name");
myname=input.nextLine();
System.out.println("Please enter your number");
num1=input.nextDouble();
answer1=Math.pow(num1,2);
System.out.println("Thank you Mr./Mrs. "+myname+" The value of num1 is"+num1+"Raised to the second power is"+df.format(answer1));
answer2=Math.sqrt(num1);
System.out.println("Thank you Mr./Mrs. "+myname+" The value of num1 is"+num1+"The square root of num1 is"+df.format(answer2)); 
System.out.println("Do you want to continue? Y or N?");
reply=input.nextLine();
replyChar=reply.charAt(0);
}
}

Now here is my error message:

--------------------Configuration: mathopr - JDK version 1.6.0_17 <Default> - <Default>--------------------
Please enter your name
Mike
Please enter your number
4
Thank you Mr./Mrs. Mike The value of num1 is4.0Raised to the second power is16.00
Thank you Mr./Mrs. Mike The value of num1 is4.0The square root of num1 is02.00
Do you want to continue? Y or N?
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
    at java.lang.String.charAt(String.java:686)
    at mathopr.main(mathopr.java:32)

Process completed.

Any help would be greatly appreciated!

regards,

Dalymiddleboro

try

reply=input.next();

instead of

reply=input.nextLine();

... I'm not sure it will work ...

Thank you AndreiDMS I did what you suggested and it went through however after I select 'y' I recieve the following error message and I'm not even able to run it again.

--------------------Configuration: mathopr - JDK version 1.6.0_17 <Default> - <Default>--------------------
Please enter your name
mike
Please enter your number
3
Thank you Mr./Mrs. mike The value of num1 is3.0Raised to the second power is09.00
Thank you Mr./Mrs. mike The value of num1 is3.0The square root of num1 is01.73
Do you want to continue? Y or N?
y
Please enter your name
Please enter your number
mike
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:840)
at java.util.Scanner.next(Scanner.java:1461)
at java.util.Scanner.nextDouble(Scanner.java:2387)
at mathopr.main(mathopr.java:25)


Any ideas what the issue here could be?

replace

reply=input.nextLine();
replyChar=reply.charAt(0);

with

replyChar=System.in.read();
/**
 * @(#)mathopr.java
 *
 * mathopr application
 *
 * @author 
 * @version 1.00 2010/1/26
 */
import java.util.*;
import java.text.*;
import java.io.*;
public class mathopr {

    public static void main(String[] args) {
        double num1,answer1,answer2,answer3=0;
        String myname;
        String reply;
        char replyChar='y';
        Scanner input = new Scanner(System.in);
        DecimalFormat df= new DecimalFormat("00.00");
        while (replyChar=='y'){
        System.out.println("Please enter your name");
        myname=input.nextLine();
        System.out.println("Please enter your number");
        num1=input.nextDouble();
        answer1=Math.pow(num1,2);
        System.out.println("Thank you Mr./Mrs. "+myname+" The value of num1 is"+num1+"Raised to the second power is"+df.format(answer1));
        answer2=Math.sqrt(num1);
        System.out.println("Thank you Mr./Mrs. "+myname+" The value of num1 is"+num1+"The square root of num1 is"+df.format(answer2)); 
        System.out.println("Do you want to continue? Y or N?");
        replyChar=System.in.read();
          }
    }
}

That's my updated code, and I am still recieving one error:
"possible loss of precision" ------ from line31

No one knows the answer to this? I'm getting an error that says possible loss of precision.

if you tell us which is line 31 ...

and please close the code tag properly like this (code)

double post, sorry

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.