Skip to main content

Cannot Find Symbol confusion

2 replies [Last post]
rlc102187
Offline
Joined: 2007-03-21

I'm writing a program that will take a signed decimal and convert it into a binary float. I'm not sure how much of my code is correct at this point because I get this compilation error that is confusing me.

:40: cannot find symbol
symbol : method parseFloat(java.lang.String)
location: class Float
float g = Float.parseFloat(line);
^
:41: cannot find symbol
symbol : method intBitsToFloat(java.lang.String)
location: class Float
float f = Float.intBitsToFloat(line);
^
2 errors

These cannot find symbol errors always make me horribly confused, at first I thought it was because it can't take a string as the argument, but it gives the same error when using an int. The line is written the exact same way as in the assignment, any help would be greatly appreciated.

<br />
import java.io.*;<br />
import java.lang.String;<br />
public class Float<br />
{<br />
	public static void main(String[] args)<br />
	{<br />
		System.out.print("Enter a signed dicaml, ex: [-]ddd.ddd: ");<br />
		String line = "";<br />
		InputStreamReader input = new InputStreamReader(System.in);<br />
		BufferedReader reader = new BufferedReader(input);<br />
		try<br />
		{<br />
			line = reader.readLine();<br />
		}<br />
		catch(Exception e){}</p>
<p>		int signBit = 0, pos = 0;<br />
		if(line.charAt(0) == '-')<br />
		{<br />
			signBit = 1;<br />
			pos++;<br />
		}<br />
		else if(line.charAt(0) == '+')<br />
		{<br />
			pos++;<br />
		}<br />
		else<br />
		{<br />
			pos++;<br />
		}</p>
<p>		int decPt = line.indexOf('.');<br />
		String intStr = line.substring(pos,decPt);<br />
		String fractStr = line.substring(decPt);<br />
		int intPart = Integer.parseInt(intStr);<br />
		double fractPart = Double.parseDouble(fractStr);<br />
		float g = Float.parseFloat(line);<br />
		float f = Float.intBitsToFloat(line);<br />
		int exp = 31 - Integer.numberOfLeadingZeros(intPart);</p>
<p>		int i;<br />
		for(i = 0; fractPart != 0.0; i++)<br />
		{<br />
			double t = fractPart * 2;<br />
			int bit = (int) t;<br />
			intPart <<= 1;<br />
			intPart += bit;<br />
			t -= (double) bit;<br />
			fractPart = t;<br />
		}</p>
<p>		System.out.println(signBit + " = signBit " + intStr + " = intStr, " + fractStr + " = fractStr, "<br />
		+ exp + " = exp" + g + " = Float.parseFloat(line)");<br />
	}<br />
}<br />

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
tarbo
Offline
Joined: 2006-12-18

The problem here is a namespace conflict. On the one hand, you want to use the java.lang.Float class; on the other, you define a Float class of your own in the default namespace. Basically, the compiler finds [i]a[/i] Float class, but not the one you intended. You have two choices to resolve this issue:

1) You change the name of your class (f.i. FloatExample).
2) You use the fully specified name for java.lang.Float.

When resolving symbols, the compiler looks through these namespaces in sequence:
1) local (current class) or fully specified
2) imported classes
3) same package
4) imported packages
5) default imported packages (java.lang)

(I could be wrong on the exact order of 2 through 4, but they are certainly between 1 and 5.)

Note that even adding [i]import java.lang.Float;[/i] will not help, because your class is in scope 1, and imports are in scopes 2, 4, or 5. So either use the fully specified name, or change your class name. I'd recommend the latter.

Good luck,
Jonathan

weller60
Offline
Joined: 2009-07-09

Thanks a lot, just new to Java programming and that cleared up an issue I had earlier =)