How do i ignore invalid samples?

In my application I read numbers off of a text file. What can i do to my code so that it ignores invalid data e.g negative numbers or letters
import java.io.*;
import java.net.*;
import java.util.StringTokenizer;
public class SurveyApp {
     public static void main(String[] args)     {
     try{
          String urlName = "";
          switch (args.length) {
          case 0: urlName = getURL();
          break;
          case 1: urlName = args[0];
     URL url = new URL(urlName);
     URLConnection connection = url.openConnection();
     connection.connect();
     BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
     String line;
     line=in.readLine();
     int noOfPeople = Integer.parseInt(line);
     People people = new People(noOfPeople);
     while ((line = in.readLine()) != null)     {
     StringTokenizer st = new StringTokenizer(line,",");
     while (st.hasMoreTokens())     {  
     int age = Integer.parseInt( st.nextToken().trim() );
     float height = Float.parseFloat( st.nextToken().trim() );
     float weight = Float.parseFloat( st.nextToken().trim() );
     Person p = new Person(age, height, weight);
     people.add(p);
          if (line != null) System.out.println(". . .");
          System.out.println("The file used to supply the data was " +urlName);
          System.out.println("The total number of people surveyed was "+noOfPeople);
          System.out.println("The average age was "+people.mediumAge());
          System.out.println("The average height was "+people.mediumHeight());
          System.out.println("The average weight was "+people.mediumWeight());
          System.out.println();
     catch (FileNotFoundException e){
     System.err.println("This file cannot be accessed");
     catch (IOException e){
          System.err.println("Error: " + e);
     private static String getURL () throws IOException {
          urlInfo();
          String input;
          BufferedReader kbd = new BufferedReader(new InputStreamReader(System.in));
          System.out.println();
          System.out.print("Please enter a valid URL: ");
          input = kbd.readLine().trim();
          return input;     
private static void urlInfo () {
System.out.println();
System.out.println("The URL should be of the format <protocl:resource>");
System.out.println("Possible protocols are http: ftp: file:");
System.out.println("E.g. for a web page: http://java.sun.com");
System.out.println(" for a local file: file:c:/dir1/dir2/file.ext");
} // end urlInfo function
class People {
     private Person[] thePeople;
     private int noOfPeople;
     public People (int size) {
     thePeople = new Person[size];
     noOfPeople = 0;
     public void add (Person p) {
     if (noOfPeople < thePeople.length) {
     thePeople[noOfPeople] = p;
     noOfPeople++;
     public double mediumAge(){          
     int sumAge = 0;
     int numPeople = 0;
     for (int j = 0; j < thePeople.length; ++j)     {     
     if (thePeople[j] == null) continue;
     ++numPeople;
     sumAge += thePeople[j].Age();
     return ( (double) sumAge / numPeople);
     public double mediumHeight(){
     float sumHeight = 0;
     int numPeople = 0;
     for (int j = 0; j < thePeople.length; ++j)     {     
     if (thePeople[j] == null) continue;
     ++numPeople;
     sumHeight += thePeople[j].Height();
     return ( (double) sumHeight / numPeople);
     public double mediumWeight(){
     float sumWeight = 0;
     int numPeople = 0;
     for (int j = 0; j < thePeople.length; ++j)     {     
     if (thePeople[j] == null) continue;
     ++numPeople;
     sumWeight += thePeople[j].Weight();
     return ( (double) sumWeight / numPeople);
class Person {
     private int age;          
     private float height;          
     private float weight;          
     public Person (int a, float h, float w) {
     age = a; height = h; weight =w;
     public Person (Person p) {
     age = p.age; height=p.height;
     weight=p.weight;
     public int Age()
     { return age; }
     public float Height()
     { return height; }
     public float Weight()
     { return weight; }
     public void print() {
     System.out.println("Age: "+age);
     System.out.println("Height: "+height);
     System.out.println("Weight : "+weight);

I'm assuming you're talking about the following lines?
int age = Integer.parseInt( st.nextToken().trim() );
float height = Float.parseFloat( st.nextToken().trim() );
float weight = Float.parseFloat( st.nextToken().trim() );
Put a try-catch block around each. If/when you parse something with a letter, you'll get an error. After you determine that you have a number, then you can test it:
if(age < 0)
  System.err.println("Age is not valid "+age);
}

Similar Messages

  • What is a error 2041; invalid sample description mean in quicktime when im trying to open a coverted mp4 file?

    Im trying to import mp4's into itunes ver 11. The ones I have downloaded have gone in fine with a bit of tinkering but when i converted avi files to mp4 and try to open them in quicktime to check them i get an error saying ( error 2041: invalid sample description was found in the movie). These files wont show up in itunes, the progress bar starts loading them but then nothing appears anywhere even home video folder, Nothing. The mp4 files work in VLC player but not quicktime.
    I used dvd fab to covert them. Any help would be greatly appreciated.
    Thank

    I had the same problem few days ago.
    It appears that MP4 format is a lot restricted, and Quicktime/iTunes does not communicate a lot about that. Tipically, Quicktime does not think that MPeg 4 + MP3 is an acceptable MP4 compination.
    To make them a lot more permissive, just try to rename the file extension to .m4v, it'll do the job in many cases, and worked for me on a lot of files.
    Best regards

  • How can i include a sampling of type in ps7 type tool?

    how can i include a sampling of type in ps7 type tool?   i did this in ps3

    I guess in my zest to answer your question i had forgotton about Adobe Type Reunion.
    What operating system are you using photoshop 7 on?
    photoshop 3.05 on mac os 7.6.1

  • How to simulate a random sample with  Poisson distribution?

    Poisson, beta, gammar, and so on,
    How to get a random sample from such specific distribution in java?

    did you check colt?

  • How to find the invalid form session in oracle Apps

    Oracle Apps R12
    Hai All
    How to find the invalid form session in oracle Apps R12 and How to Kill the that.
    Thanks & Regards
    Srikkanth.M

    Hi,
    Please refer to these docs (for 11i and R12).
    How to Kill Inactive Sessions Based on the Information in the Monitor User Form [ID 103516.1]
    Script: How To Identify The apps User Using The O/S PID For Forms Users In 11i [ID 185762.1]
    Thanks,
    Hussein

  • How to transfer stock from sample usage to  unrestricted use

    Dear  sir
    how to transfer stock from sample usage to  unrestricted use

    I suppose user has accidently posted quantity to sample usage?
    Do you see material document with movement type 333 for this material?
    You can search material document list using MB51 transaction
    or If you know the inspection lot, You can list material documents list from inspection lot.
    If you can find the material document number -->Use MBST transaction to reverse the movement
    If you can not find material document --> use Mb1C transaction use movement type 561.
    In either case stock can not be return to same inspection lot. You can activate another inspection lot using inspection type 05 if needed.
    Hope this helps.

  • What does "Error-2041: an invalid sample description was found in the movie"means?

    what does "Error-2041: an invalid sample description was found in the movie"means?

    I have the exact same problem. I have a PPC 6700, and now I can't view movies I created with it in mp4. If change extension file to 3GP video works just fine.
    Also, when I try to see a webpage with a Quicktime video clip, I only get a Quicktime logo with a big question mark in it, but no play.
    I tried uninstalling and reinstalling, but nothing seems to work.
    I have Windows XP, and it worked fine until a couple weeks ago. Not sure if when upgrading to Quicktime 7.2, though.

  • Error -2041: an invalid sample description was found in the movie

    I just tried to use quick time to open mp4 on my PC (taken from my dopod/HTC pda phone), but NOW get "error -2041: an invalid sample description was found in the movie". I used to be able to watch the vids, but no longer......

    I have the exact same problem. I have a PPC 6700, and now I can't view movies I created with it in mp4. If change extension file to 3GP video works just fine.
    Also, when I try to see a webpage with a Quicktime video clip, I only get a Quicktime logo with a big question mark in it, but no play.
    I tried uninstalling and reinstalling, but nothing seems to work.
    I have Windows XP, and it worked fine until a couple weeks ago. Not sure if when upgrading to Quicktime 7.2, though.

  • How to recompile all invalid objects of APPS?

    Dear all:
    How to recompile all invalid objects of APPS? my environment is ORACLE 9.2.0.5 32bit.
    Regards
    Terry

    varun4dba wrote:
    hi Terry,
    did you tried adadmin?
    thanks.Do you mean:
    Compile/Reload Database Objects
    1. Compile APPS schema
    2. Compile menu information
    3. Recreate grants and synonyms for APPS schema
    4. Compile flexfield data in AOL tables
    5. Reload JAR files to database
    6. Return to Main Menu

  • How do I ignore a rollover on text object?

    The following code builds a custom button. It has a bug. The hand cursor becomes a mouse pointer and the rollout state is activated when I roll over the overlapping Label:
    1. How can I ignore a rollover on the Label?.
    2. How can I make the hand cursor  remain in place while I roll over any part of the HBox (including the Label)?
    Thank you!
    <?xml version="1.0" encoding="utf-8"?>
    <mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="init()"   buttonMode="true"
        useHandCursor="true">
    <mx:Script>
        <![CDATA[
    import mx.events.FlexEvent;
    [Bindable]        public var buttonTitle:String;
        public function init():void{
            hbox.width=textField1.width;
            hbox.height=40;
            fillDark()
            hbox.addEventListener(MouseEvent.MOUSE_OVER, handleRollOver)
            hbox.addEventListener(MouseEvent.MOUSE_OUT, handleRollOut)
        private function handleRollOver(e:MouseEvent):void{
            fillLight();
            textField1.styleName="sectionsButtonOver";
        private function handleRollOut(e:MouseEvent):void{
            fillDark();
            textField1.styleName="sectionsButton";
        private function fillDark():void{
            hbox.graphics.beginFill(0x2b2b2b)   
            hbox.graphics.drawRect(0,0,this.width-1,40)
            hbox.graphics.endFill()
            hbox.graphics.beginFill(0x414141)   
            hbox.graphics.drawRect(this.width-1,0,1,40)
            hbox.graphics.endFill()
        private function fillLight():void{
            hbox.graphics.beginFill(0x3f3f3f)   
            hbox.graphics.drawRect(0,0,this.width-1,40)
            hbox.graphics.endFill()
            hbox.graphics.beginFill(0x414141)   
            hbox.graphics.drawRect(this.width-1,0,1,40)
            hbox.graphics.endFill()
        ]]>
    </mx:Script>
    <mx:HBox id="hbox" buttonMode="true" useHandCursor="true" />
    <mx:Label id="textField1" text="{buttonTitle}"  y="13" selectable="false"  buttonMode="true" useHandCursor="true"
         styleName="sectionsButton"  />
    </mx:Canvas>

    Thanks for your reply. I modified your suggestion and set the following properties on a Canvas that I used as a wrapper:
    <mx:Canvas id="cover" width="100%"  height="100%"  mouseChildren="false" buttonMode="true" useHandCursor="true"  >
    </mx:Canvas>

  • Error -2041:  Invalid Sample Description on every MP4 file that I create.

    OK, Apple already has a poor reputation in my book and this just adds to it, because I'm not the only one struggling with this problem. Numerous students in my classes have complained about the same thing. We are training on Adobe After Effects 7.0 and our instructor has requested that we post our files as MP4 files, so we have tried MP4 Standard, MP4 Improved, and the H.264 settings. Regardless of which setting we use we get the error:
    Error -2041: an invalid sample description was found in the movie
    Now why should the software that automatically makes itself the default player for MP4 files not be able to actually open them? We've all downloaded the most recent player.
    Clearly, since I'm not the only one with the issue, there is a problem with the software that needs to be addressed. For now, I am changing the default player to something that can actually open and play the files without crying about it.

    I had the same problem few days ago.
    It appears that MP4 format is a lot restricted, and Quicktime/iTunes does not communicate a lot about that. Tipically, Quicktime does not think that MPeg 4 + MP3 is an acceptable MP4 compination.
    To make them a lot more permissive, just try to rename the file extension to .m4v, it'll do the job in many cases, and worked for me on a lot of files.
    Best regards

  • How to write Validations for Sample Data??

    Hi Frnds,
    I want to know how to write validations  for sample oracle data?? Can anyone explain anyone clearly??
    Regards,
    Raj

    Hi shushma,
    Simply put a check that:
    If(wdContext.current<YOUR_NODE>Element().get<YOUR_ATTRIBUTE_NAME> !=null){
    else{
    // Show the error messages
    //Displaying the error message is very easy.
    //You can report exception using the message manager API's. You will get this easily on SDN.
    I hope this helps! if you need ay further help please let me know.
    Thanks and Regards,
    Pravesh

  • How can we delete invalid postings

    Hi All,
    How can we delete invalid threads or postings in sdn.
    Some one can help me to delete the invalid threads.
    Thanks in advance,
    venkat
    Edited by: VENKAT BOORUGADDA on Sep 8, 2009 9:10 AM

    Only forum moderators have the capability of deleting invalid threads. It's up to their discretion as well as to whether a posting is invalid or not.

  • How do I ignore 10920 error

    I am using Labview with a MultiIO DAQCard-AI-16E-4. I am using both counters. I get a 10920 error... and my data aquisition stops. I want to ignore this error and continue aquiring data.
    I can easily replicate this error by removing the input wire to the GATE and then connecting it again while running(simulate an intermittennt short).
    How do I ignore this error and continue aquiring?
    I can replicat

    Use General Error Handler.vi (Functions palette --> Time & Dialog). It allows to cancel an error by setting exception action, exception code and exception source. Read the online help for more information.

  • Imports - EHP5 - Ignore Invalid Component Versions option

    Hello,
       EHP5 has introduced an additional option - "ignore invalid component version" import option. What is the code equivalent while using this unconditional mode option.
    tp import help says:
    U<nnn>                specify one or more unconditional modes
    <nnn>       umode digits, i.e. 0,1,2,..,9
    Which umode digit corresponds too  "ignore invalid component version"

    Hi,
    Meaning for the different unconditional modes with tp command:
    http://help.sap.com/SAPhelp_nw70/helpdata/en/3d/ad5b814ebc11d182bf0000e829fbfe/content.htm
    U mode 2 overwrite original objects could correspond to ignore component version option.
    Regards,
    Srikishan

Maybe you are looking for

  • External account

    Hi, after several months of PHD syncing nightmares I recently opted for an external account on a portable firewire drive. It seems to be working very nicely, allowing me to switch from a portable macbook pro to an imac rapidly and without problems. t

  • Integrate other directory servers with access manager

    How to integrate other directory servers with access manager ?

  • Can anyone help with iTunes error message (0x80092013) please?

    I just updated iTunes and now I get error message (0x80092013), does anyone know how to fix it please?  I am using Windows, not Mac.

  • Can't Highlight ANY text, please help.

    I can't highlight any text in any application. I can't drag my mouse to highlight, and when I press Ctrl+A it only highlights pictures and links on a web page. Please help, I need to be ably to copy text for my work and I can't even select it.

  • Breeze 5 Quiz Manager

    Hi, I am having the following issues while developing the question slides in Breeze 5. Backward and Forward Navigation : After publishing the presentation and reviewing the breeze presentation when I come across a question slide placed in the middle