ERROR HANDLING for my String tokenizer

else if (sCarRental.equals("EP")) { sStationCode = st.nextToken().trim(); sName=st.nextToken().trim(); sLocationCode = st.nextToken().trim(); sAddress1=st.nextToken().trim(); sAddress2=st.nextToken().trim(); sPostCode=st.nextToken().trim(); sCity=st.nextToken().trim(); sStationName=sName+sAddress1+sAddress2+sPostCode+sCity; sStationType="C"; if(sLocationCode.equalsIgnoreCase("c")) { sStationArea="C"; } else if (sLocationCode.equalsIgnoreCase("S")) { sStationArea="S"; } else if (sLocationCode.equalsIgnoreCase("N")) { sStationArea="N"; } else if (sLocationCode.equalsIgnoreCase("E")) { sStationArea="E"; } else if (sLocationCode.equalsIgnoreCase("W")) { sStationArea="W"; } else if (sLocationCode.equalsIgnoreCase("T")) { sStationArea="T"; } else if (sLocationCode.equalsIgnoreCase("X")) { sStationArea="R"; } else if (sLocationCode.equalsIgnoreCase("L")) { sStationArea="R"; } else { sStationArea="C"; } sSupplierCode ="EP"; sLocationCode=sStationCode.substring(0, 3); sCrsCode = "EP"; }

I want to read a csv file and write it to a database

But here i want to add some error handling .. So how can i do that ..normally my csv file should contain 7 values what if if there are only 4 values

help appreciated ..

-------------Problems Reply------------

You probably could do something like this... create an API to return the value if exist, otherwise throw an exception:-

private String getValue(StringTokenizer st, String name) {
if (st.hasMoreTokens()) {
return st.nextToken().trim();
else {
throw new RuntimeException("Missing value for " + name);

In your code, instead of calling st.nextToken().trim(), you call that API:-

else if (sCarRental.equals("EP")) {

sStationCode = getValue(st, "station code");
sName=getValue(st, "name");
sLocationCode = getValue(st, "location code");
sAddress1=getValue(st, "address 1");
sAddress2=getValue(st, "address 2");
sPostCode=getValue(st, "post code");
sCity=getValue(st, "city ");


Category:java Views:0 Time:2011-02-15

Related post

  • What is the better error handling method for decoding Python bytes to unicode strings? 2010-04-08

    I have an old C# program that is being ported to Python 3 for different reasons. Basically, what the program does is to fetch a website and search its content (and process it, but that is not really relevant). I have never really had any issues with

  • Error handling String to Integer and inserting empty values in database 2010-10-18

    I am new to Java and I am creating an application in JSF and Hibernate. I have a form which has several input fields but only one field that will be an Integer, Age. The rest of them will be Strings. Age is not a required field so the user can enter

  • Error handling in BASH 2008-09-15

    What is your favorite method to handle errors in BASH? The best example of handling errors in BASH I have found on the web was written by William Shotts, Jr at William Shotts, Jr suggests using the following function for

  • Using events rather than exceptions to implement error handling 2008-09-29

    I'm working on some code that uses a pattern in its business and data tiers that uses events to signal errors e.g. resource = AllocateLotsOfMemory(); if (SomeCondition()) { OnOddError(new OddErrorEventArgs(resource.StatusProperty)); resource.FreeLots

  • Good error handling practice 2008-09-30

    What is a good error handling practice for an site? Examples? Thanks! --------------Solutions------------- As with any .net project I find the best way is to only catch specific error types if they are may to happen on the given page. For exa

  • PHP - Custom error handling. Redirected 404 is being hijacked by AVG Anti-Virus. How to stop? 2008-10-03

    I have a website which uses the custom 404 error handling in PHP/Apache to display specific pages. e.g. I've a feeling this is a bad way of doing this, but it's code I inherited... Although the page displays correctly on mo

  • C++ Error Handling - Good Sources of Example Code? 2008-10-23

    Just about every piece of example code everywhere omits error handling (because it "confuses the issue" that the example code is addressing). My programming knowledge comes primarily from books and web sites, and you seldom see any error handling in

  • error-handling-when-taking-user-input part2 2008-10-25

    I made previously a question: and I made the suggested changes: char displayMainMenu() { char mainMenuChoice; cout << "\nQuadratic equation: a*X^2 + b*X + c = 0 mai

  • jQuery Ajax error handling, show custom exception messages 2008-12-18

    Is there some way I can show custom exception messages as an alert in my jQuery Ajax error message? For example, if I want to throw an exception on the server side via Struts by "throw new ApplicationException("User name already exists");", I want to

  • Error handling in C code 2008-12-22

    What do you consider "best practice" when it comes to error handling errors in a consistent way in a C library. There are two ways I've been thinking of: Always return error code. A typical function would look like this: MYAPI_ERROR getObjectSize(MYA

  • JAX-RS / Jersey how to customize error handling? 2009-02-24

    I'm learning JAX-RS (aka, JSR-311) using Jersey. I've successfuly created a Root Resource and am playing around with parameters: @Path("/hello") public class HelloWorldResource { @GET @Produces("text/html") public String get( @QueryParam("name") Stri

  • How to re-enable the default error handling in VB6 2009-04-10

    I have some code with various "On Error Goto" error handlers in a few places to handle some broken third party hardware. I was getting an overflow error (read from the Err variable) in a routine that doesn't have an error trap but is called by a rout

  • Short circuit error handling in C 2009-04-13

    I was wondering if there was a better way of handling the case in C where you want to exit a function as soon as you encounter an error in a series of expressions. (in this case, its a function that returns a NULL on error) e.g. in some C code where

  • Error handling in MS Excel VBA 2009-05-14

    I am having a bit of trouble with errors occurring in a loop in VBA. First, here is the code I am using dl = 20 For dnme = 1 To 3 Select Case dnme Case 1 drnme = kt + " 90" nme = "door90" drnme1 = nme Case 2 drnme = kt + " dec" nme = "door70" 'decora

  • Correct error-handling practices for the data-layer 2009-05-28

    What are good things to check for, with respect to error-handling, when you are dealing with the data-access-layer? For example, let's assume I have this function.. Public Function UserExists(ByVal userName As String) As DataTable Dim dt As Object =

  • ASP.Net MVC Exception Logging combined with Error Handling 2009-06-23

    I am looking for a simple solution to do Exception Logging combined with Error Handling in my ASP.Net MVC 1.0 application. I've read lots of articles, including Questions posted here on StackOverflow, which all provide varying solutions for different

  • Good Patterns For VBA Error Handling 2009-06-24

    What are some good patterns for error handling in VBA? In particular, what should I do in this situation: ... some code ... ... some code where an error might occur ... ... some code ... ... some other code where a different error might occur ... ...

  • ASP.NET MVC Custom Error Handling Application_Error Global.asax? 2009-07-23

    I have some basic code to determine errors in my MVC application. Currently in my project I have a controller called Error with action methods HTTPError404(), HTTPError500(), and General(). They all accept a string parameter error. Using or modifying

  • App-wide error handling for ASP.NET MVC2 web app? 2009-07-23

    I have some basic code to determine errors in my MVC application. Currently in my project I have a controller called Error with action methods HTTPError404(), HTTPError500(), and General(). They all accept a string parameter error. Using or modifying

Copyright (C), All Rights Reserved.

processed in 0.246 (s). 11 q(s)