Hi :
I am beginner in using java , I tried to add bignumber using java
my program enter numbers and add them , stop when user enter 0 then show the summation.

import java.util.Scanner ; 
import java.math.BigDecimal;

public class CODE {

	/**
	 * @param args
	 */
	public static void main(String[] args)
	{
		BigDecimal n1 , n2; 
		Scanner in = new Scanner (System.in) ;
		n1 = in.nextBigDecimal() ; 
		do
		{
			n2 = in.nextBigDecimal() ;
			if(n2.equals(0))
			{
				System.out.println(n2);
				break ; 
			}
			n1 = n1.add(n2);
		}
		while(!n2.equals(0));
		
	}

}

The program runs but doesnot show the output !!!!!!!!!
I don't know why ??? can anyone help me!!!!!!

Thanks in advance

Recommended Answers

All 16 Replies

Have a closer look at your if statement.

Thanks first to answer me but
I want to write
if(n2==0)
{\\.......}
but I cannot do this in bigdecimal ??
Do you understand me??

And also the value that you are outputting may not be the one you want.

Because you have to compare the correct types. So you can use the int value of that BigDecimal to compare with int 0, or you can compare to BigDecimal.valueOf(0).

Thanks first to answer me but
I want to write
if(n2==0)
{\\.......}
but I cannot do this in bigdecimal ??
Do you understand me??

I wanted you to look at the whole of the if, but Ezzaral has given you a more explicit clue...

Ok i understand what you say
thanks

thaaaaaaaanks for your help (all) ,
but i have strange error how can i solve it ??????

import java.util.Scanner ; 
import java.math.BigDecimal;
 class CODE {
	public static void main(String[] args)
	{
		BigDecimal n1 , n2; 
		Scanner in = new Scanner (System.in) ;
		n1 = in.nextBigDecimal() ; 
		do
		{
			n2 = in.nextBigDecimal() ;
			if(n2.compareTo(BigDecimal.valueOf(0))==0)
			{
				System.out.println(n1);
				break ; 
			}
			n1 = n1.add(n2);
		}
		while(n2.compareTo(BigDecimal.valueOf(0))==1);
		
	}

}

This is the new one
The error is :
Main.java:6: class CODE is public, should be declared in a file named CODE.java
public class CODE {
^
1 error

Your file name needs to match the class name. You can't save a class named "CODE" in a file named "main".

How can i fix it ?????
can i fix this problem??

Well, I would certainly hope so. You can rename a class or a file so that they match, can't you?

when i rename the class
class CODE.java{
\\...}
appears 8_errors

when i rename the class
class CODE.java{
\\...}
appears 8_errors

Why would you do that? It still wouldn't match your file name "Main" and it's not a valid class name.

Have you tried naming the class "Main"?

Oh
It's realy the first time using java
and I coulden't understand how to name the Main ?
do you mean the:

public static void main(String[] args)
    {\\..}

No.
The file name must match the name of your class.

This means

public class [B]MyClass[/B] {

}

must be saved in a file named "MyClass.java".

So you can save your class in a file named "CODE.java" or you can rename this class to "Main", so that it matches your file name of "Main.java".

This has nothing at all to do with the main() method that is defined in the class.

Ohhhhhhhh
thaaaaaaaaaaaaaaaaaaaaaaanks so much
realy thank you for your help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.