How to find out whether a double contains a value?

  • (2 Pages)
  • +
  • 1
  • 2

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

#1 Sujath  Icon User is offline

  • New D.I.C Head

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

How to find out whether a double contains a value?

Posted 05 October 2013 - 07:42 PM

I want to know if a double value is empty or not I have tried using null because i am reading from a file and passing the string value as double, i have also tried double != "" but it won't work since double can't be compared like that and i have also tried Double.isNaN(rating) but that doesn't work either. I need to know how to do it since the java program won't let me compile my program properly.

This is the code i implemented
while(data.hasNextLine())
{
            newline = data.nextLine();
	    StringTokenizer stFile = new StringTokenizer(newline,",");
while(stFile.hasMoreTokens())
			{
rating = Double.parseDouble(stFile.nextToken());
			}
			if(Double.isNaN(rating))
			{
				
	System.out.println(songID + "\t" + song + "\t" + artist + "\t" + album + "\t" + duration + "\t" + genre  );
			}
			else
			{
System.out.println(songID + "\t" + song + "\t" + artist + "\t" + album + "\t" + duration + "\t" + genre + "\t" + rating );
			}


}



Is This A Good Question/Topic? 0
  • +

Replies To: How to find out whether a double contains a value?

#2 pbl  Icon User is offline

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

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

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

Posted 05 October 2013 - 07:49 PM

double has a value ? Does not make sense a double will always have a value even if it is NaN

StringTokeneizer is a deprecated class you shouldn't use it

You want to know if there is a valid double to read ?
Use a Scanner instead of a StrinkTokenizer and test if scan.hasDouble()
Was This Post Helpful? 2
  • +
  • -

#3 Atli  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3712
  • View blog
  • Posts: 5,964
  • Joined: 08-June 10

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

Posted 05 October 2013 - 07:50 PM

According to the docs, the Double.parseDouble method throws an exception if the passed string does not contain a valid double value. Which means you should be adding a try-catch clause, not testing the returned value after the fact. If it doesn't throw an exception, it's a valid double.

Edit: Spoke to soon. I'd recommend listening to pbl. He knows best! :)

This post has been edited by Atli: 05 October 2013 - 07:54 PM

Was This Post Helpful? 2
  • +
  • -

#4 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 05 October 2013 - 08:01 PM

I am trying to read the song file and put each of the data that is in the file into a variable that is the reason i
have used tokenizer so i can display the data from the file in a table but i have a problem
say in the file i got the following data:
1 Fridge 120.00 appliance 3.3
2 TV 200.00 appliance
3.PS3 200.00 console 5


As you can see in the second data there no double value because this appliance does not have an rating thats why i'm getting this error:
Exception in thread "main" java.util.NoSuchElementException
at java.util.StringTokenizer.nextToken(Unknown Source)
at songsDisplay.displaySongs(songsDisplay.java:45)
at mainMenu.case1Menu(mainMenu.java:99)
at mainMenu.main(mainMenu.java:40)

is there a way to fix this?
Was This Post Helpful? 0
  • +
  • -

#5 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2197
  • View blog
  • Posts: 5,224
  • Joined: 10-September 10

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

Posted 06 October 2013 - 02:21 AM

Use a Scanner object to read the data as pbl said, and don't argue that there is a valid reason for using a deprecated class/method, because there isn't, unless it's required by the instructor who hasn't updated his/her lessons in years.

Then, using a Scanner object, say 'input', read the entire line to an array of Strings using the String.split() method:

String[] lineOfData = input.nextLine().split( " " );

That way, you'll be able to tell by the size of lineOfData whether the last item, the 'double' rating, existed to be read.
Was This Post Helpful? 0
  • +
  • -

#6 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:01 AM

Okay so did like you told me but i get this error
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 6
at songsDisplay.displaySongs(songsDisplay.java:54)
at mainMenu.case1Menu(mainMenu.java:99)
at mainMenu.main(mainMenu.java:40)

Here is the code:
while(data.hasNextLine())
			{
				newline = data.nextLine();
				StringTokenizer stFile = new StringTokenizer(newline, ",");
				
				int n = stFile.countTokens();
				
				String[] songs = newline.split(",");
				int i = 0;
				//while(stFile.hasMoreTokens())
				//{
					songID = Integer.parseInt(songs[i]);
                    song = songs[i+1];
                    artist = songs[i+2];
                    album = songs[i+3];
                    duration = Double.parseDouble(songs[i+4]);
                    genre = songs[i+5];
                    rating = Double.parseDouble(songs[i+6]);
                    
					
					i=i+7;
				//}
				

Was This Post Helpful? 0
  • +
  • -

#7 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2652
  • View blog
  • Posts: 11,190
  • Joined: 20-September 08

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

Posted 06 October 2013 - 03:04 AM

Your larger problem is a classic garbage in, garbage out one. If one of the fields is missing (because of no rating) then how can you be sure that the missing token IS the rating field and not, say, one of the others?
In that case, counting the tokens from String.split won't help you. You need a more rationalized file format

Quote

Okay so did like you told me but i get this error
You didn't - because you didn't check the number of tokens as GregBrannon recommended (but see my comment above). You're also STILL using StringTokenizer, which you shouldn't without a very good reaspn

This post has been edited by g00se: 06 October 2013 - 03:05 AM
Reason for edit:: typo

Was This Post Helpful? 0
  • +
  • -

#8 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:05 AM

as you can see the tokenizer is there as a comment its not in the actual code
Was This Post Helpful? 0
  • +
  • -

#9 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:10 AM

and this is number of tokens in each line
7,7,7,7,7,7,7,7,7,7,7,6,7,7,7,7,7,6,7,7,7,7,7,7,7,7,7,7,6,7,7,7,7,7,6,7,7,7,7,7,7,7,7,7,7,6,6,6,7,7,7,7,6,7,7
Was This Post Helpful? 0
  • +
  • -

#10 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2197
  • View blog
  • Posts: 5,224
  • Joined: 10-September 10

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

Posted 06 October 2013 - 03:17 AM

Sometimes you have to deal with "garbage in," but if the variation in the input is known, as in some have user ratings and some don't, then the program can be easily designed to be flexible to accommodate variations in the data.

In this case, your program does not have the necessary flexibility to adapt to variations in the data. Don't assume there will always be 7 elements in the input line when you know there will sometimes be 6. There are a number of ways to accomplish this for this specific case, but all should include checking the size of songs[], which you have not yet considered.

This post has been edited by GregBrannon: 06 October 2013 - 03:17 AM

Was This Post Helpful? 0
  • +
  • -

#11 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:18 AM

Atli also made a good point about using a try/catch block to catch a NumberFormatException, which has been completely ignored by the OP.
Was This Post Helpful? 0
  • +
  • -

#12 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:19 AM

View PostFlukeshot, on 06 October 2013 - 03:18 AM, said:

Atli also made a good point about using a try/catch block to catch a NumberFormatException, which has been completely ignored by the OP.

Yeh if i do that it would just ignore the rest of the lines
Was This Post Helpful? 0
  • +
  • -

#13 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2197
  • View blog
  • Posts: 5,224
  • Joined: 10-September 10

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

Posted 06 October 2013 - 03:25 AM

Your responses often indicate that you easily dismiss the advice given because you know better or have decided that it's useless because of some misunderstandings you have. We don't typically give useless advice, so rather than dismissing given advice quickly, if you don't understand how a piece of advice might be useful, ask for an explanation.

Try the "brain open / mouth closed" approach when requesting advice, and I think you'll have greater success.
Was This Post Helpful? 2
  • +
  • -

#14 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 - 02:26 PM

Sorry if i sound like that i'm new to java and like you said i would like an explanation
Was This Post Helpful? 0
  • +
  • -

#15 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2197
  • View blog
  • Posts: 5,224
  • Joined: 10-September 10

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

Posted 06 October 2013 - 02:34 PM

Thanks for admitting there are things you have yet to learn. Please try to keep that attitude, especially when you are asking for help.

There are many ways to use try/catch blocks. I'm not sure what your assumptions are, but the catch part of the block is not just to exit the program and inform the user that an error has occurred.

For example, if the logic is expecting the user or file to provide numeric input, and the input is not numeric, then the catch block could be used to inform the user that the input was incorrect or process the data file further for the proper data. You can learn more about how to use try/catch in the Oracle Java tutorials here.
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2