• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

C and Java

 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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);
}}}
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the exact error message?
 
Zafar Ali
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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);
 
author
Posts: 9034
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanx all,
Nice ranchers.
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Note to self: don't get into a fist fight with a cactus. Command this tiny ad to do it:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic