How to find out whether a double contains a value?

  • (2 Pages)
  • +
  • 1
  • 2

23 Replies - 505 Views - Last Post: 07 October 2013 - 03:53 PM Rate Topic: -----

#16 Flukeshot  Icon User is offline

  • A little too OCD
  • member icon

Reputation: 415
  • View blog
  • Posts: 1,030
  • Joined: 14-November 12

Re: How to find out whether a double contains a value?

Posted 06 October 2013 - 03:29 PM

Quite the contrary to the belief that catch blocks only relay error messages, it's possible for your program to obtain infallibility. You'd have to be a god to achieve that level of robustness, but it's still possible in theory.
Was This Post Helpful? 0
  • +
  • -

#17 Sujath  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 18-April 13

Re: How to find out whether a double contains a value?

Posted 06 October 2013 - 03:36 PM

Alright i'll try this out if i need more more i'll message you
Was This Post Helpful? 0
  • +
  • -

#18 Sujath  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 18-April 13

Re: How to find out whether a double contains a value?

Posted 06 October 2013 - 07:10 PM

Okay i had a read through the info u have gave me but it didn't give me any info on how to deal with an error like mine would you mind explaining what approach i should take?
Was This Post Helpful? 0
  • +
  • -

#19 Flukeshot  Icon User is offline

  • A little too OCD
  • member icon

Reputation: 415
  • View blog
  • Posts: 1,030
  • Joined: 14-November 12

Re: How to find out whether a double contains a value?

Posted 06 October 2013 - 11:53 PM

It was not an error, it was an exception.

Errors that are not compiler errors (bad syntax and whatnot) are usually out of your control, but exceptions can and should be handled.

Every exception can be handled with a try/catch block, and so you should catch your ArrayIndexOutOfBoundsException and deal with it appropriately.

Perhaps by having the catch block call a method that does the same thing, but assumes there is no 7th token?
Was This Post Helpful? 0
  • +
  • -

#20 Sujath  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 18-April 13

Re: How to find out whether a double contains a value?

Posted 07 October 2013 - 01:02 AM

View PostFlukeshot, on 06 October 2013 - 11:53 PM, said:

It was not an error, it was an exception.

Errors that are not compiler errors (bad syntax and whatnot) are usually out of your control, but exceptions can and should be handled.

Every exception can be handled with a try/catch block, and so you should catch your ArrayIndexOutOfBoundsException and deal with it appropriately.

Perhaps by having the catch block call a method that does the same thing, but assumes there is no 7th token?

Sorry i don't quite understand what u mean by that is it possible for you to give a brief example with that theory so i can clearly know what u talking about as you know i'm new to java
Was This Post Helpful? 0
  • +
  • -

#21 Sujath  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 18-April 13

Re: How to find out whether a double contains a value?

Posted 07 October 2013 - 01:09 AM

and i'm using split method not tokens
Was This Post Helpful? 0
  • +
  • -

#22 Flukeshot  Icon User is offline

  • A little too OCD
  • member icon

Reputation: 415
  • View blog
  • Posts: 1,030
  • Joined: 14-November 12

Re: How to find out whether a double contains a value?

Posted 07 October 2013 - 01:28 AM

Split divides a string up into 'tokens', you are using tokens, you are not using StringTokenizer anymore.

So, you have a file with a scanner, the file has 6 or 7 pieces of information per line.

You want to sort those lines out into workable data in your program.

File fileName = new File("your file goes here");
Scanner in = new Scanner(fileName);
while(in.hasNext()) {
   String currentLine[] = in.nextLine().split(",");
   song = currentLine[0];
   artist = currentLine[1];
   ...
   ...

   try {
      rating = currentLine[6];
   } catch (ArrayIndexOutOfBoundsException e) {
      //there's no 7th value, do something about that
      //I suggest either ignore it, or give it some default value, such as 0
   }
}


This can't be made any clearer.

This post has been edited by Flukeshot: 07 October 2013 - 01:56 AM
Reason for edit:: typing error

Was This Post Helpful? 1
  • +
  • -

#23 Sujath  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 18-April 13

Re: How to find out whether a double contains a value?

Posted 07 October 2013 - 01:43 AM

Thanks man you are a legend it worked out
Was This Post Helpful? 0
  • +
  • -

#24 pbl  Icon User is offline

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

Reputation: 8329
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: How to find out whether a double contains a value?

Posted 07 October 2013 - 03:53 PM

while(data.hasNextLine()) {
		
		newline = data.nextLine();
		String[] songs = newline.split(",");
		songID = Integer.parseInt(songs[0]);
                song = songs[1];
                artist = songs[2];
                album = songs[3];
                duration = Double.parseDouble(songs[4]);
                genre = songs[5];
                if(songs.length == 7)
                    rating = Double.parseDouble(songs[6]);
                else
                    rating = 0.0;


Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2