?
Solved

Can anyone debug my program?

Posted on 2003-02-26
8
Medium Priority
?
239 Views
Last Modified: 2010-03-31
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;

          }
}
}
0
Comment
Question by:puppyhuang
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 2

Accepted Solution

by:
functionpointer earned 100 total points
ID: 8031379
try changing this...


int s = Integer.parseInt( br.readLine() );
        //System.out.println(s);

    switch (s){
        case 1:
              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 2:
0
 
LVL 3

Expert Comment

by:msterjev
ID: 8031436
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

0
 
LVL 2

Expert Comment

by:functionpointer
ID: 8031444
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.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Expert Comment

by:socratesk
ID: 8031507
Replace the line
       int s = System.in.read();
with
      int s = Integer.parseInt(br.readLine());

This will work fine.

Socrates-Chennai
India
0
 

Author Comment

by:puppyhuang
ID: 8031545
Thanks everybody help me on this program, thank you all.

CC
0
 
LVL 1

Expert Comment

by:socratesk
ID: 8031573
Replace the line
       int s = System.in.read();
with
      int s = Integer.parseInt(br.readLine());

This will work fine.

Socrates-Chennai
India
0
 

Expert Comment

by:CleanupPing
ID: 9059046
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.
0
 
LVL 35

Expert Comment

by:TimYates
ID: 9671403
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
0

Featured Post

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses
Course of the Month9 days, 6 hours left to enroll

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question