Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

C and Java

 
Zafar Ali
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
#include<stdio.h>
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++)
{
total+=count;
System.out.println(count++, total);
}}}
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the exact error message?
 
Zafar Ali
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1365
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author
Sheriff
Posts: 8898
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Zafar -
I'm sure you'll catch this, but it also looks like you are incrememting 'count' twice in every 'for' iteration.
 
Robbie shi
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
System.out.println(count++,"and" + total);
your method is wrong~~~
try this one
:
System.out.println(count++ +"and"+total);
--
Robbies
-----------------------------
1.java IDE tool : JawaBeginer
2.Java Jar tool : JavaJar
http://www.pivotonic.com
 
Zafar Ali
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanx all,
Nice ranchers.
 
Divya Venkatesh
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic