4 Replies - 1387 Views - Last Post: 10 March 2011 - 05:27 PM Rate Topic: -----

#1 Boggle_Man  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 07-March 11

Java Try Catch Error Program Problem

Posted 07 March 2011 - 05:25 PM

Hello all, I'm a bit new with the Try Catch method I've tried looking at the example given in the code snippets and tried to incorporate this into my program but now I've hit a brick wall and if anyone can give me a bit of guidance please as to where I'm going wrong that would be great help.
here is the code to my program.
import java.util.*;
import java.util.Scanner;

public class Pythagoras_Error {

	public static void main(String args[]) {

		double side1, side2;
		char choice;
		
		Scanner sc = new Scanner(System.in);
			
				 System.out.println("Enter value for side 1 of the triangle");
					side1 = sc.nextDouble();
					System.out.println("Enter value for side 2 of the triangle");
					side2 = sc.nextDouble();
					
					boolean goodInput;
			    	do{
			    		goodInput = true;
						try{
							side1= sc.nextDouble();
							side2 = sc.nextDouble();
						}
						catch(InputMismatchException ex) {
							System.out.println("I asked for an Int value, please try again!");
							goodInput = false;
						}
						/*clear the buffer*/
						side1 = sc.nextDouble();
						side2 = sc.nextDouble();
					}while(!goodInput);
			    }
}

do {			
					
			if (side1 > 0 && side2 > 0)//
				System.out.println("Hypotenuse : " + calcHyp(side1, side2));
			else if (side1 <= 0) //if side1 is <=0 an error mssg prints out//
				System.out.println("Side 1 must be positive, you have " + side1);
			else if (side2 <= 0) //if side2 is <=0 an error mssg prints out//
				System.out.println("Side 2 must be positive, you have " + side2);
			System.out.println("Do you want to repeat the program Y or N: "); 
//THIS ASKS IF THE USER WANTS TO CONTINUE WITH THE PROGRAM//
			choice = sc.next().charAt(0); //THE USER ENTERS THEIR CHOICE//
			} while (choice != 'N'); //WHILE THE CHOICE IS NOT EQUAL TO NO, IT WILL CONTINUE TO INPUT NUMBERS//
	System.out.println("END OF PROGRAM.");
	
	
	}


	private static double calcHyp(double s1In, double s2In) {
		return Math.sqrt(calcHypSquared(s1In, s2In));
	}

	private static double calcHypSquared(double s1In, double s2In) {
		return square(s1In) + square(s2In);
	}
	private static double square(double sIn) {
		return sIn + sIn;
					
			}
		
	}
	



Is This A Good Question/Topic? 0
  • +

Replies To: Java Try Catch Error Program Problem

#2 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 2388
  • View blog
  • Posts: 5,012
  • Joined: 11-December 07

Re: Java Try Catch Error Program Problem

Posted 07 March 2011 - 05:36 PM

Your try catch looks good. I think you have to clear the buffer with nextLine() though. :)
Was This Post Helpful? 1
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8378
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: Java Try Catch Error Program Problem

Posted 07 March 2011 - 08:53 PM

Looks good ? You read twice 2 doubles


Remove these 2 lines they have nothing to do there what are you trying to do

/*clear the buffer*/
side1 = sc.nextDouble();
side2 = sc.nextDouble();
and if you have garbadge they wont remove it

Always better to read line and parse after

String line1 = sc.nextLine();
String line2 = sc.nextLine();
try {
... side1 = Double.parseDouble(line1);
... side2 = Double.parseDouble(line2);
}
cacth(InputMistMatchException ex)

and no need to re-read faulty lines

This post has been edited by pbl: 07 March 2011 - 08:55 PM

Was This Post Helpful? 2
  • +
  • -

#4 Boggle_Man  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 07-March 11

Re: Java Try Catch Error Program Problem

Posted 10 March 2011 - 05:20 PM

Thanks for your help with this.. I'll have a look at the code and see what I can do with it.

Rgds,
BM..
Was This Post Helpful? 0
  • +
  • -

#5 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 2388
  • View blog
  • Posts: 5,012
  • Joined: 11-December 07

Re: Java Try Catch Error Program Problem

Posted 10 March 2011 - 05:27 PM

Quote

Looks good ? You read twice 2 doubles


Yeah, that's the bit I told him to change. ;)

Pbl's code is a much better way of doing this. It's best practice to read the whole line and then parse it. I was just trying to get your code to work with the least modification. :)
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1