Link to home
Start Free TrialLog in
Avatar of puppyhuang
puppyhuang

asked on

Can anyone debug my program?

I know I should do it by myself, but I just can't find more samples to help me. So, please help me debug my program. Thank you.

CC

---------------------------------------
import java.io.*;

class LVDT{
public static void main(String args[]) throws IOException
{

double temperature, dest, strain;
String strD1, strD2, strD3;

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

System.out.println("Enter Temperature value: ");
strD1 =  br.readLine();
temperature = Double.parseDouble(strD1);

System.out.println("------------------");
System.out.println("You can input either value of the following variable");
System.out.println("1.Dest");
System.out.println("2.Strain");

     int s = System.in.read();
     //System.out.println(s);

switch (s){
     case 49:
           System.out.println("Enter Dest value: ");
           strD2 =  br.readLine();
           dest = Double.parseDouble(strD2);

           System.out.println("Subtract Temperature from Crack value is: ");
           System.out.print( dest + ((-0.0005)* temperature) -(0.0066) );

           break;

    case 50:
           System.out.println("Enter Strain value: ");
             strD3 =  br.readLine();
             strain = Double.parseDouble(strD3);

             System.out.println("Subtract Temperature from Strain value is: ");
           System.out.print( strain - ((-3.3866)* temperature) -(8.756) );

           break;

          }
}
}
ASKER CERTIFIED SOLUTION
Avatar of functionpointer
functionpointer

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of msterjev
msterjev

The error in you code is that when you read with:

    int s = System.in.read();

You read only one character and the \r\n is still in the input buffer, so when you try to read the line againt you have empty line. If you want to keep your stile you should do something like this:


int s = System.in.read();
System.in.read();
// then go on

Considering you have already gone through the trouble of wrapping System.in in a BufferedReader, it seems a shame to waste the overhead...  readLine() will take care of the \r\n, and block until you get one, which i think was the desired result.
Replace the line
       int s = System.in.read();
with
      int s = Integer.parseInt(br.readLine());

This will work fine.

Socrates-Chennai
India
Avatar of puppyhuang

ASKER

Thanks everybody help me on this program, thank you all.

CC
Replace the line
       int s = System.in.read();
with
      int s = Integer.parseInt(br.readLine());

This will work fine.

Socrates-Chennai
India
puppyhuang:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
Avatar of TimYates
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Accept functionpointer's comment as answer.

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

TimYates
EE Cleanup Volunteer