double cannot be dereferenced

stuck need some quick assistance!

Page 1 of 1

12 Replies - 19593 Views - Last Post: 28 April 2009 - 04:04 AM Rate Topic: -----

#1 seaneyb  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 111
  • Joined: 09-February 09

double cannot be dereferenced

Posted 27 April 2009 - 11:19 AM

C:\Users\Seaneyb\Desktop\Exam Revision\Programming\Java\Projects\exam b>build
src\exam.java:18: double cannot be dereferenced
if (Number.startsWith("+"))
^
1 error

import java.lang.*;
import java.util.*;
import java.io.*;
import java.net.*;

public class exam extends Object
{
	public static void main(String[] argStrings) throws Exception
	{
		ArrayList<Double> NumbersInput = new ArrayList<Double>();
		
		Scanner Fileinput = new Scanner(new File("../files/text.txt"));

		while (Fileinput.hasNextDouble())
		{
			double Number = Fileinput.nextDouble();
			
			if (Number.startsWith("+"))
			{
				System.out.println("Number: " + Number);
			}
				NumbersInput.add(Number);
		}
		Fileinput.close();

		//for (int Sum = 1; Sum < NumbersInput.size() - 1; Sum++)
		//{
		//	System.out.println(NumbersInput.get(Sum));
		//}
}
}



Basically i want to be able to only print out the positive numbers from the array list

234.5
104.5
-90.8
908.765

these are the values in the text file the last bit really isnt needed for what i want to do so i commented it out and used an if statement to only display the positive values. Anyone help with this error? i know its dead simple! i just cant see where im going wrong

Is This A Good Question/Topic? 0
  • +

Replies To: double cannot be dereferenced

#2 SayMoi  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 135
  • Joined: 08-April 09

Re: double cannot be dereferenced

Posted 27 April 2009 - 11:27 AM

Why does this list of numbers look oddly familiar? :P

Ok, the main problem is you're trying to check if a double begins with a String. (Which it doesn't) I'll give you another hint on that if you want. The second problem (if I have this right) is that numbers don't begin with +. :)
Was This Post Helpful? 0
  • +
  • -

#3 seaneyb  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 111
  • Joined: 09-February 09

Re: double cannot be dereferenced

Posted 27 April 2009 - 11:57 AM

View PostSayMoi, on 27 Apr, 2009 - 10:27 AM, said:

Why does this list of numbers look oddly familiar? :P

Ok, the main problem is you're trying to check if a double begins with a String. (Which it doesn't) I'll give you another hint on that if you want. The second problem (if I have this right) is that numbers don't begin with +. :)


haha it does because someone posted about it earlier not having a clue what there doing lol, however i do know what im doing slightly haha ive been fidling with it for the past hour and ive ripped bits out and stuffed new bits in ive lost my mind might need to take a breather and try again.

ok so the if statement im trying to do wont actually work.... i need to try and think this thing, or is it possiable to change the double to a string? then letting it run though and print it out on the screen.
Was This Post Helpful? 0
  • +
  • -

#4 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 520
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: double cannot be dereferenced

Posted 27 April 2009 - 12:15 PM

You cannot call a method on a primitive data type variable. I don't know how you're going to get around it, because I don't really know what you want the program to do...
Was This Post Helpful? 0
  • +
  • -

#5 SayMoi  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 135
  • Joined: 08-April 09

Re: double cannot be dereferenced

Posted 27 April 2009 - 12:17 PM

Alright, not gonna tell you right off, but here's your first tutorial:

http://leepoint.net/...num2string.html
Was This Post Helpful? 0
  • +
  • -

#6 333OnlyHalfEvil  Icon User is offline

  • D.I.C Addict

Reputation: 24
  • View blog
  • Posts: 664
  • Joined: 20-March 09

Re: double cannot be dereferenced

Posted 27 April 2009 - 12:18 PM

like SayMoi said, positive double numbers don't start with +. However, the negative integers start with -. If youre going to take this approach, you want to convert the doubles to strings as you go through the list and check if they have a - in front of them. Add in an else statement and make the if block do nothing(so negative numbers arent added to the list) and make the else block add the numbers to the list (this essentially adds the positive numbers to the list).

Also, you never have to write extends Object in your class name. Java always automatically adds this in even if its not explicitly written.
Was This Post Helpful? 0
  • +
  • -

#7 seaneyb  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 111
  • Joined: 09-February 09

Re: double cannot be dereferenced

Posted 27 April 2009 - 12:19 PM

ahh ok i see
import java.lang.*;
import java.util.*;
import java.io.*;
import java.net.*;

public class exam extends Object
{
	public static void main(String[] argStrings) throws Exception
	{
		ArrayList<Double> NumbersInput = new ArrayList<Double>();
		
		Scanner Fileinput = new Scanner(new File("../files/text.txt"));

		while (Fileinput.hasNextDouble())
		{
			double Number = Fileinput.nextDouble();
			
			System.out.println("Numbers: " + Number);
			NumbersInput.add(Number);
		}
		Fileinput.close();

		for (int Sum = 1; Sum < NumbersInput.size() - 1; Sum++)
		{
			System.out.println(NumbersInput.get(Sum));
		}
}
}



this is what the original code was like
any help of another way i know its quite simple to do.

i just want to display all the positive numbers

Numbers: 234.5
Numbers: 104.5
Numbers: -90.8
Numbers: 908.765
104.5
-90.8

this is what it prints atm.

i dont really know what the for loop does!

This post has been edited by seaneyb: 27 April 2009 - 12:22 PM

Was This Post Helpful? 0
  • +
  • -

#8 SayMoi  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 135
  • Joined: 08-April 09

Re: double cannot be dereferenced

Posted 27 April 2009 - 12:21 PM

And for your second hint, check out what the "!" sign does here:

http://java.sun.com/.../opsummary.html

Think about how you would use that with checking for positives and negatives.
Was This Post Helpful? 0
  • +
  • -

#9 333OnlyHalfEvil  Icon User is offline

  • D.I.C Addict

Reputation: 24
  • View blog
  • Posts: 664
  • Joined: 20-March 09

Re: double cannot be dereferenced

Posted 27 April 2009 - 12:21 PM

post your code within the code tags

Actually, forget what I said in my previous post. Just change your if statement to check if the number is > 0.

I don't know what i was thinking when i said to convert them to strings

This post has been edited by 333OnlyHalfEvil: 27 April 2009 - 12:24 PM

Was This Post Helpful? 1
  • +
  • -

#10 seaneyb  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 111
  • Joined: 09-February 09

Re: double cannot be dereferenced

Posted 27 April 2009 - 01:16 PM

Jesus why didn't I think of that!!!! Cool I'll do that now nd see if that works :)
Was This Post Helpful? 0
  • +
  • -

#11 seaneyb  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 111
  • Joined: 09-February 09

Re: double cannot be dereferenced

Posted 27 April 2009 - 01:22 PM

just to let you know it worked perfectly!

many thanks everyone :)
Was This Post Helpful? 0
  • +
  • -

#12 SayMoi  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 135
  • Joined: 08-April 09

Re: double cannot be dereferenced

Posted 27 April 2009 - 02:02 PM

Too cool to hit the thanks button? :P Glad it worked out for ya!

This post has been edited by SayMoi: 27 April 2009 - 02:02 PM

Was This Post Helpful? 0
  • +
  • -

#13 seaneyb  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 111
  • Joined: 09-February 09

Re: double cannot be dereferenced

Posted 28 April 2009 - 04:04 AM

i did :P
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1