11 Replies - 359 Views - Last Post: 27 March 2012 - 08:26 PM Rate Topic: -----

#1 meowbits  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 107
  • Joined: 31-January 12

Data mismatch - How do I go about handling this error?

Posted 27 March 2012 - 06:25 PM

It has been too long since I used java. I am having some problems with code on line 29. The method that I am calling is compiled code so I cannot see it. I do know what it will return based on what I pass though.

It may return a double, or it may return a String.

This is the code I am using to test, not finished yet until I can get past this part.
Ideally I would like to be able to not hit the catch so I can work with the data.
But if I do have to hit the catch - is there a way to keep the program running? ie. catch 20 errors at once.
import java.io.*;

public class FunctionTest {

    public static void main(String[] args) {
        int i;
        double functionResult;
        String[] tokens, parameters, legalValues;
        String line;
        try {
            FileInputStream fileStream = new FileInputStream("testCases.txt");
            DataInputStream inStream = new DataInputStream(fileStream);
            BufferedReader myReader = new BufferedReader(new InputStreamReader(inStream));

            // read line
            while ((line = myReader.readLine()) != null) {
                // delimit & split
                tokens = line.split("\t");
                tokens[1] = tokens[1].replaceAll("\\s", "");
                parameters = tokens[1].split(",");

                tokens[2] = tokens[2].replaceAll("Legal values: ", "");
                tokens[2] = tokens[2].replaceAll("Package Type must be ", "");
                legalValues = tokens[2].split("\t");
                //System.out.println(tokens[2]);

                // perform test
                //System.out.println("char1= " + parameters[0].charAt(0) + "\tdouble= " + Double.valueOf(parameters[1]) + "\tchar2= " + parameters[2].charAt(0));
                functionResult = Function.shippingCost(parameters[0].charAt(0), Double.valueOf(parameters[1]), parameters[2].charAt(0));
                System.out.println("Result: " + functionResult);

                // analyze results
                if (functionResult == Double.parseDouble(tokens[2])) {
                    System.out.println("Match: y\n");
                } else {
                    System.out.println("Match: n\n");
                }
            }
            inStream.close();
        } catch (Exception e) {
            System.out.println("Error: " + e.getMessage());
        }
    }
}



Output - Result is a value returned, as is the error, "Legal values: B, or E
Result: 20.0
Match: y

Result: 10.0
Match: n

Error:
Legal values: B, or E
Press any key to continue . . .


Is This A Good Question/Topic? 0
  • +

Replies To: Data mismatch - How do I go about handling this error?

#2 pbl  Icon User is offline

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

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Data mismatch - How do I go about handling this error?

Posted 27 March 2012 - 06:30 PM

View Postmeowbits, on 27 March 2012 - 09:25 PM, said:

It may return a double, or it may return a String.

Simply impossible, a function can only return one datatype
If you have to different retunr statements, onme that returns a double, one that returns a String your code won't just compile

and what does
Function.shippingCost() looks like ?
Was This Post Helpful? 0
  • +
  • -

#3 meowbits  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 107
  • Joined: 31-January 12

Re: Data mismatch - How do I go about handling this error?

Posted 27 March 2012 - 06:31 PM

This is all I was given of the method.

public class Function
{
   public static double shippingCost(char packageType, double weight, char customerType)
   {
   .................
   .................
   }

}


Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

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

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Data mismatch - How do I go about handling this error?

Posted 27 March 2012 - 06:38 PM

Quote

Output - Result is a value returned, as is the error, "Legal values: B, or E

Impossible, the function returns a double not a String
this is printed somewhere else or in the function
Was This Post Helpful? 0
  • +
  • -

#5 meowbits  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 107
  • Joined: 31-January 12

Re: Data mismatch - How do I go about handling this error?

Posted 27 March 2012 - 06:45 PM

I will run it again and hard code values to produce the error.

import java.io.*;

public class FunctionTest {

    public static void main(String[] args) {
        int i;
        double functionResult;
        String[] tokens, parameters, legalValues;
        String line;
        try {
//            FileInputStream fileStream = new FileInputStream("testCases.txt");
//            DataInputStream inStream = new DataInputStream(fileStream);
//            BufferedReader myReader = new BufferedReader(new InputStreamReader(inStream));
//
//            // read line
//            while ((line = myReader.readLine()) != null) {
//                // delimit & split
//                tokens = line.split("\t");
//                tokens[1] = tokens[1].replaceAll("\\s", "");
//                parameters = tokens[1].split(",");
//
//                tokens[2] = tokens[2].replaceAll("Legal values: ", "");
//                tokens[2] = tokens[2].replaceAll("Package Type must be ", "");
//                legalValues = tokens[2].split("\t");
//                //System.out.println(tokens[2]);
//
//                // perform test
//                //System.out.println("char1= " + parameters[0].charAt(0) + "\tdouble= " + Double.valueOf(parameters[1]) + "\tchar2= " + parameters[2].charAt(0));
//                functionResult = Function.shippingCost(parameters[0].charAt(0), Double.valueOf(parameters[1]), parameters[2].charAt(0));
//                System.out.println("Result: " + functionResult);
//
//                // analyze results
//                if (functionResult == Double.parseDouble(tokens[2])) {
//                    System.out.println("Match: y\n");
//                } else {
//                    System.out.println("Match: n\n");
//                }
//            }
//            inStream.close();

            functionResult = Function.shippingCost('P',10,'H');

        } catch (Exception e) {
            System.out.println("Error: " + e.getMessage());
        }
    }
}



Output with just the code above.
Error:
Legal values: B, or E
Press any key to continue . . .

Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

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

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Data mismatch - How do I go about handling this error?

Posted 27 March 2012 - 06:49 PM

So it is in Function.shippingCost() that there is a println("Error:...")
or most propable a throw new Exception("Error....");
Was This Post Helpful? 0
  • +
  • -

#7 meowbits  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 107
  • Joined: 31-January 12

Re: Data mismatch - How do I go about handling this error?

Posted 27 March 2012 - 06:52 PM

I guess that would make sense. Is there a way to handle it and continue the program?
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

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

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Data mismatch - How do I go about handling this error?

Posted 27 March 2012 - 07:00 PM

If it is a thrown exception simply do not

System.out.println("Error: " + e.getMessage());
Was This Post Helpful? 0
  • +
  • -

#9 meowbits  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 107
  • Joined: 31-January 12

Re: Data mismatch - How do I go about handling this error?

Posted 27 March 2012 - 07:49 PM

Sorry, do you mean to just not have the output? I tried that and I still only get to the third cycle of the loop.

This post has been edited by meowbits: 27 March 2012 - 07:49 PM

Was This Post Helpful? 0
  • +
  • -

#10 shantics  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 17-March 12

Re: Data mismatch - How do I go about handling this error?

Posted 27 March 2012 - 08:13 PM

Is this what your looking for

try{
    functionResult = Function.shippingCost(parameters[0].charAt(0), 
                     Double.valueOf(parameters[1]), 
                     parameters[2].charAt(0));
	             System.out.println("Result: " + functionResult);
}catch(exeption e){
System.out.println(e.msg);
}
//and carry on


Was This Post Helpful? 1
  • +
  • -

#11 pbl  Icon User is offline

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

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Data mismatch - How do I go about handling this error?

Posted 27 March 2012 - 08:15 PM

So make a try/catch just for the call to this function

		try {
			FileInputStream fileStream = new FileInputStream("testCases.txt");
			DataInputStream inStream = new DataInputStream(fileStream);
			BufferedReader myReader = new BufferedReader(new InputStreamReader(inStream));

			// read line
			while ((line = myReader.readLine()) != null) {
				// delimit & split
				tokens = line.split("\t");
				tokens[1] = tokens[1].replaceAll("\\s", "");
				parameters = tokens[1].split(",");

				tokens[2] = tokens[2].replaceAll("Legal values: ", "");
				tokens[2] = tokens[2].replaceAll("Package Type must be ", "");
				legalValues = tokens[2].split("\t");
				//System.out.println(tokens[2]);

				// perform test
				try {
					functionResult = Function.shippingCost(parameters[0].charAt(0), Double.valueOf(parameters[1]), parameters[2].charAt(0));
					System.out.println("Result: " + functionResult);

					// analyze results
					if (functionResult == Double.parseDouble(tokens[2])) {
						System.out.println("Match: y\n");
					} else {
						System.out.println("Match: n\n");
					}
				}
				catch(Exception e) {
					/// ignore error in shipping cost
				}
			}
			inStream.close();
		} catch (IOException e) {
			System.out.println("Error: " + e.getMessage());
		}
	}


:^:
Was This Post Helpful? 1
  • +
  • -

#12 meowbits  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 107
  • Joined: 31-January 12

Re: Data mismatch - How do I go about handling this error?

Posted 27 March 2012 - 08:26 PM

Wow thanks!
I was always a novice with java and I hadn't used it in forever. Spent most of the day just re-learning the basics.

Saves me a big headache to finally move forward, really appreciate it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1