File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

String formatting issue

 
Sen George
Ranch Hand
Posts: 76
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am using Java 1.5. I am facing a formatting issue. Following is the code which I am trying to compile.

public class fibonacci {
public static void main (String args[]){
int fib1 = 1;
int fib2 = 1;
int fib3;
String s;

System.out.println();
System.out.println("The first 25 Fibonacci numbers:");
System.out.println();
System.out.println("Fibonacci number 1: " + fib1);
System.out.println();
System.out.println("Fibonacci number 2: " + fib2);
System.out.println();

fib3 = fib1 + fib2;
for (int j=3; j<=25; j++) {

s = String.format("Fibonacci number %4d", fib3);
System.out.println(s);
System.out.println();

fib1 = fib2;
fib2 = fib3;
fib3 = fib1 + fib2;
}
}
}
------------------------------------------------------------
The following error comes up on compilation.
fibonacci.java:20 cannot resolve symbol
symbol : method format (java.lang.String,int)
location: class java.lang.String
s = String.format("Fibonacci number %4d",fib3);
^
1 error
------------------------------------------------------------

I would appreciate any feedback in getting this resolved.

Regards,
Sen.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think you really *are* using 1.5! What does this say:
 
Julien Grenier
Ranch Hand
Posts: 41
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apparently you are not using java 1.5 because your code does work on my machine. Make sure you are not compiling with -source 1.4 -target 1.4 .


Julien
 
Sen George
Ranch Hand
Posts: 76
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Gabby,

Thanks for the feedback. The version apparently has turned out to be 1.3.

I am a novice at this. I am pretty sure that I downloaded JDK 1.5 and am in the right directory while compiling the source file. 1.5 is not the only version present on my machine.

Please advise what needs to be done to point to the right version while compiling.

Regards,
Sen.
 
Layne Lund
Ranch Hand
Posts: 3061
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all, how do you compile and run your program? Are you using the command-line? If so, type "java -version" and "javac -J-version" to find out which version of java and javac are being used. Also, type "echo %PATH%" (assuming you are using Windows) to see what the PATH variable is. If you post the output from all of these commands, we can help you further to fix this problem.

Layne
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Having more than one JDK or JRE on your machine complicates things.
When you start defining packages and using jar files, that will
complicate things, too. Most beginners start tinkering with PATH
and CLASSPATH environment variables. But that can be problematic because
different apps may need different versions of libraries and different verions of JREs.

Two ways out of this are to (1) Use an IDE (of course, you'll be back
asking where is the 1.4/1.5 switch buried in your IDE) or (2) learn
to use a build tool, typically ant (http://ant.apache.org).

I know learning a programming language is a big enough task, but I think
in the long term Java programmers should also do *both* (1) and (2).
 
Sen George
Ranch Hand
Posts: 76
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Layne. I compile and run the program from the command line.

Following is the output of the version command.
-----------------------------------------------
java version"1.3.1_02"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.1_02-b02)
...

Following is the value of the PATH variable.
-----------------------------------------------
c:\temp\bin;C:\temp\jdk\bin;C:\orant\bin;C:\WorkFlow\bin;C:\Oracle\bin;C:\Program Files\Oracle\jre\1.1.8\bin;c:\temp\jdk\jre\bin\classic;c:\temp\jdk\jre\bin;c:\temp\jlib;C:\Program Files\Oracle\jre\1.3.1\bin;C:\PROGRA~1\Ticomm;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\system32\wbem;c:\program files\ati technologies\ati control panel;C:\Program Files\Common Files\Adaptec Shared\System;C:\Program Files\CA\pec\bin;C:\Program Files\CA\CCC_Harvest;C:\Program Files\Hummingbird\Connectivity\7.11\Accessories\;C:\Program Files\Microsoft SQL Server\80\Tools\BINN

Please advise.

Thanks.
Sen.
 
Kenneth Albertson
Ranch Hand
Posts: 59
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sen. This looks like a Windows problem rather than a Java-specific problem. An important question that determines what you need to do to fix this - Do you need to have multiple versions of Java on your PC?

Step 1 - I suggest that you begin by uninstalling ALL Java versions currently installed on your PC. Go to Control Panel, Add or Remove Programs, and they should all be listed. Get rid of all of them. Delete any junk left behind. Now you know the state of your machine.

Step 2 If you just want one version, just reinstall Java 5. My suggestion is that you put it in C:\Program Files\Java\version_id, which is where it should be on Windows. You should be ready to roll.

Step 2 If you need multiple Java versions, reinstall each in turn, making sure that each is in a separate ...\version_id directory. The version that is picked up from the command line at any time will be determined by the order of the entries in the PATH variable.

Disclaimer - I'm a Java beginner, and I haven't done this myself, so if there is more to it than that, someone might like to add the details.
 
Layne Lund
Ranch Hand
Posts: 3061
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Sen George:
Thanks Layne. I compile and run the program from the command line.

Following is the output of the version command.
-----------------------------------------------
java version"1.3.1_02"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.1_02-b02)
...

Following is the value of the PATH variable.
-----------------------------------------------
c:\temp\bin;C:\temp\jdk\bin;C:\orant\bin;C:\WorkFlow\bin;C:\Oracle\bin;C:\Program Files\Oracle\jre\1.1.8\bin;c:\temp\jdk\jre\bin\classic;c:\temp\jdk\jre\bin;c:\temp\jlib;C:\Program Files\Oracle\jre\1.3.1\bin;C:\PROGRA~1\Ticomm;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\system32\wbem;c:\program files\ati technologies\ati control panel;C:\Program Files\Common Files\Adaptec Shared\System;C:\Program Files\CA\pec\bin;C:\Program Files\CA\CCC_Harvest;C:\Program Files\Hummingbird\Connectivity\7.11\Accessories\;C:\Program Files\Microsoft SQL Server\80\Tools\BINN

Please advise.

Thanks.
Sen.


So as you can see, you are actually using Java 1.3, as you seem to have already figured out. At this point, I would suggest modifying the path. However, I need to know where you have Java 1.5 installed before I can describe how to do this. Please provide the path for the Java 1.5 installation and I will help you from there.

Layne
 
Sen George
Ranch Hand
Posts: 76
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have figured this out. I had installed the jdk in a personal directory in order to avoid messing up other tools running on my machine. I defined the relevant paths in the PATH variable and that fixed the problem.

My weekend is made.

A big thank you to Layne, Kym and Gabby.

Sen.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic