The moose likes Beginning Java and the fly likes C and Java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "C and Java" Watch "C and Java" New topic

C and Java

Zafar Ali
Ranch Hand

Joined: May 11, 2002
Posts: 64
Hey there!
I am learning C and Java together i have a problem in java.
the code which display in c language i wana output same in Java
but java compiler diplay an error.
please help me.
void main(){
int count, total;
for(count=0,total=0;count<=10; count++)
total +=count;
printf("count=%d,\ttotal=%d\n", count,total);

count=0; total=0
count=1; total=1
count=2; total=3
count=3; total=6
count=4; total=10
count=5; total=15
count=6; total=21
count=7; total=28
count=8; total=36
count=9; total=45
public class Loop{
public static void main(String arg[]){
int count, total;
for(count=0;count<10; count++)
System.out.println(count++, total);

Zafar <br />***Your behaviour should affect.***
Ilja Preuss

Joined: Jul 11, 2001
Posts: 14112
What is the exact error message?

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Zafar Ali
Ranch Hand

Joined: May 11, 2002
Posts: 64
Hi Ilja
i compile at Textpad the error diplay :

C:\jdk1.4\bin\Loop.java:9: cannot resolve symbol
symbol : method println (int,java.lang.String)
location: class java.io.PrintStream
System.out.println(count++,"and" + total);
1 error
Tool completed with exit code 1
David Weitzman
Ranch Hand

Joined: Jul 27, 2001
Posts: 1365
There are several common functions in C/C++ that can take a variable number of arguments, printf being one of them. Java functions cannot, so System.our.println() is just expecting one item. You'll need to contruct a single String containing everything you want to display (or, as an alternative, use multiple calls to print() and println()). Try this:
System.out.println("count=" + (count++) + "; total=" + total);
Bert Bates

Joined: Oct 14, 2002
Posts: 8801
Zafar -
I'm sure you'll catch this, but it also looks like you are incrememting 'count' twice in every 'for' iteration.

Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Robbie shi

Joined: Jan 05, 2003
Posts: 28
System.out.println(count++,"and" + total);
your method is wrong~~~
try this one
System.out.println(count++ +"and"+total);
1.java IDE tool : JawaBeginer
2.Java Jar tool : JavaJar
Zafar Ali
Ranch Hand

Joined: May 11, 2002
Posts: 64
Thanx all,
Nice ranchers.
Divya Venkatesh

Joined: Nov 05, 2002
Posts: 28
These were the Errors:
You increment count two times.
You did not initialise total initially.
and your print Syntax was incorrect.
This code works,I made changes just to the JAVA code.

Hope this helps.

Divya Venkatesh

Joined: Nov 05, 2002
Posts: 28
I have a basic Question:When i compiled this program it raised a compiler error where it said total was uninitialised.
Dosent Java automatically initialise all its variables?
thanks in advance.
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
Class and instance variables (i.e. static and nonstatic member variables) are initialized to default values (0, null, false) if not otherwise initialized. But local variables (those declared inside a method or constructor) are not - you must assign a value to the variable before it can be used.

"I'm not back." - Bill Harding, Twister
I agree. Here's the link: http://aspose.com/file-tools
subject: C and Java
Similar Threads
Misbehaving variable?
more wait() and notify() - deadlock situation
Two dimensional array total and sort
double array file cleared up
One more question, then thats it I promise :) ...