Question about why I cant compile

compiling question

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 1092 Views - Last Post: 25 April 2008 - 11:04 AM Rate Topic: -----

#1 tami3113  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 03-November 07

Question about why I cant compile

Post icon  Posted 22 April 2008 - 08:44 AM

I am writing a program where you enter how much money you have recieved as a gift in either Yen, US Dollar or Euro. Then the program must convert each amount to US dollar, and keep a running balance.
The program consists of two classes, a bank class and a driver. The bank class is to do the conversions and handle the money, while the driver is to prompt the user and display everything.

I've written both codes, but I must be missing something because I cant compile correctly. Could someone take a look and tell me what I'm missing or offer any advice. I'll post the bank then the driver.

import javax.swing.JOptionPane;

public class Bank {  
	private final static double EuroToUS = .67;  
	private final static double YenToUS = 110.00;  
	 
	private double balance;  
	  
	// constructor   
	public Bank() {  
		balance = 0.0;  
	}  
	// constructor with initial balance  
	public Bank(double balance) {  
		this.balance = balance;  
	}  
	  
	public double getBalance() {  
		return balance;  
	}  
	public void addDollars(double dollars) {  
		balance += dollars;  
	}  
	public void addYens(double yens) {  
		balance += (yens / YenToUS);  
	}  
	public void addEuros(double euros) {  
		balance += (euros / EuroToUS);  
	}  
	  
	
}

import java.util.Scanner;

public class Driver 
{
	 public static void main(String[]args)
	 {
	 Scanner in=new Scanner(System.in);
	 Bank b=new Bank();
	 int x;
	 do

	 {
	 System.out.println("Choose one of the following choices");
	 System.out.println("1-Enter the amount of the gift");
	 System.out.println("2-Display balance amount");
	 System.out.println("3-Exit");
		
	 x=in.nextInt();
		
	 switch(x)
	{
	 case 1:
	 System.out.println("Enter the amount");
	 float amount=in.nextFloat();
	 b.addDollars(amount);
	 break;
		
	 case 2:
	 System.out.println("Current balance is  "+b.getBalance());
	 break;
		
	 case 3:
	 System.exit(0);
	 }
}//end do
		
	 while(x!=3);
	}

}


Is This A Good Question/Topic? 0
  • +

Replies To: Question about why I cant compile

#2 1lacca  Icon User is offline

  • code.rascal
  • member icon

Reputation: 44
  • View blog
  • Posts: 3,822
  • Joined: 11-August 05

Re: Question about why I cant compile

Posted 22 April 2008 - 09:02 AM

Please post the error message of the compiler as well!
Was This Post Helpful? 0
  • +
  • -

#3 tami3113  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 03-November 07

Re: Question about why I cant compile

Posted 22 April 2008 - 09:04 AM

I'm using JCreator, and when I Build the project I dont get any errors. But when i Run the project it says:

java.lang.NoSuchMethodError: main
Exception in thread "main"
Process completed.
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,904
  • Joined: 06-March 08

Re: Question about why I cant compile

Posted 22 April 2008 - 12:07 PM

View Posttami3113, on 22 Apr, 2008 - 09:04 AM, said:

I'm using JCreator, and when I Build the project I dont get any errors. But when i Run the project it says:

java.lang.NoSuchMethodError: main
Exception in thread "main"
Process completed.

You are trying to excute the class Bank
It is the class Driver that as the main() method
Was This Post Helpful? 0
  • +
  • -

#5 tami3113  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 03-November 07

Re: Question about why I cant compile

Posted 22 April 2008 - 01:04 PM

How do I change that? I ensured that I didnt have the cursor in the bank class when I attempted to run the program but it still gave me the same error.
I tried to just run each file and they come up with no errors, but it has to be something.
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,904
  • Joined: 06-March 08

Re: Question about why I cant compile

Posted 22 April 2008 - 01:10 PM

View Posttami3113, on 22 Apr, 2008 - 01:04 PM, said:

How do I change that? I ensured that I didnt have the cursor in the bank class when I attempted to run the program but it still gave me the same error.
I tried to just run each file and they come up with no errors, but it has to be something.

Works for me

Choose one of the following choices
1-Enter the amount of the gift
2-Display balance amount
3-Exit
1
Enter the amount

Are your two classes in the same source file ?
Was This Post Helpful? 0
  • +
  • -

#7 tami3113  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 03-November 07

Re: Question about why I cant compile

Posted 22 April 2008 - 06:55 PM

They are two different classes, not in the same source file.
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,904
  • Joined: 06-March 08

Re: Question about why I cant compile

Posted 22 April 2008 - 08:05 PM

View Posttami3113, on 22 Apr, 2008 - 06:55 PM, said:

They are two different classes, not in the same source file.


So if you canot make JCreator to correctly select the class Driver correctly
(I have problem with that, don't know JCreator but there must be a way to say: run Driver as an application or in the project you should be able to say that Driver is the class to run)

at the console cd to the directory where are your class files
and then type

java Driver

that should do the trick
Was This Post Helpful? 0
  • +
  • -

#9 tami3113  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 03-November 07

Re: Question about why I cant compile

Posted 23 April 2008 - 12:34 PM

Alright, so I made some changes to my Driver to do some more things. I'm now trying to give the user 5 options: add dollars, add euros, add yen, see balance and exit.

When I build the file, for some reason, it still only offers me those first 3 choices that were in the initial code I had written. I cant figure out why.


1. Enter amount of gift
2. Get balance
3. Exit


I'm wondering if my problem is in this line of each case:

float amount=in.nextFloat();

I'm not sure. Code as follows.

import java.util.Scanner;

public class Driver 
{
	public static void main(String[]args)
	{
	
		Scanner in=new Scanner(System.in);
		Bank b=new Bank();
		
		int userChoice;
		
		do
		{   //offers the user choices of what they would like to enter
			System.out.println("Choose one of the following choices");
			System.out.println("1-Enter gift in dollars");
			System.out.println("2-Enter gift in euro");
			System.out.println("3-Enter gift in yen");
			System.out.println("4-Display balance amount");
			System.out.println("5-Exit");
			
		
			userChoice=in.nextInt();
		
			switch(userChoice)
			{
				case 1:
					System.out.println("Enter the amount");
					float amount=in.nextFloat();
					b.addDollars(amount);
					break;
					
				case 2:
					System.out.println("Enter the amount");
					float amount=in.nextFloat();
					b.addEuros(amount);
					break;
				
				case 3:
					System.out.println("Enter the amount");
					float amount=in.nextFloat();
					b.addYens(amount);
					break;
			
				case 4:
					System.out.println("Current balance is  "+b.getBalance());
					break;
	
				case 5:
					System.exit(0);
			
			}
		
		}//end do
		while(userChoice!=5);
	
		
	}

}

Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,904
  • Joined: 06-March 08

Re: Question about why I cant compile

Posted 23 April 2008 - 01:58 PM

BTW it seems you fix your main() method problem ?

View Posttami3113, on 23 Apr, 2008 - 12:34 PM, said:

Alright, so I made some changes to my Driver to do some more things. I'm now trying to give the user 5 options: add dollars, add euros, add yen, see balance and exit.

When I build the file, for some reason, it still only offers me those first 3 choices that were in the initial code I had written. I cant figure out why.


1. Enter amount of gift
2. Get balance
3. Exit


I'm wondering if my problem is in this line of each case:

float amount=in.nextFloat();


You are running the old program because the new one does not compile.
You cannot define more then one time the same variable in a switch statement

so instead of
switch (userCode)
   case 1:
	  float amount = in.nextFloat();
	  ...
   case 2:
	  float amount = in.nextFloat();



do

float amount;
switch (userCode)
   case 1:
	  amount = in.nextFloat();
	  ...
   case 2:
	  amount = in.nextFloat();


This post has been edited by pbl: 23 April 2008 - 01:59 PM

Was This Post Helpful? 0
  • +
  • -

#11 tami3113  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 03-November 07

Re: Question about why I cant compile

Posted 23 April 2008 - 08:18 PM

Ooooohhhhhhhh, that makes sense! Wow. Thanks.

One last question. I want to make my "case 4 with the balance" to round out 2 decimal places. Would I create that in the case or would I do that in the Bank class?
Was This Post Helpful? 0
  • +
  • -

#12 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,904
  • Joined: 06-March 08

Re: Question about why I cant compile

Posted 23 April 2008 - 08:29 PM

View Posttami3113, on 23 Apr, 2008 - 08:18 PM, said:

One last question. I want to make my "case 4 with the balance" to round out 2 decimal places. Would I create that in the case or would I do that in the Bank class?


Your Bank class getBalance() method returns a double not a String so it might return 1.234567
So it is the role of your GUI (the switch statement) to format that double into a String with only 2 decimal digits.

Unless you want to write a method

public String getBalance2digits() {
....
}

in your Bank class that performs that operation
Was This Post Helpful? 0
  • +
  • -

#13 tami3113  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 03-November 07

Re: Question about why I cant compile

Posted 24 April 2008 - 08:50 AM

Okay, I understand. I've made corrections to my driver. I'm getting an error code when I try to compile that says : incompatable types, and it directs me to the return balance line. Is that because I did not declare the balance correctly?
When I compile, it is not rounding only 2 two places. It is still giving me as many decimal places as it wants.

import javax.swing.JOptionPane;

public class Bank 
{  
	//conversions
	private final static double EuroToUS = .67;  
	private final static double YenToUS = 110.00;  
		  
	private double balance;  
	  
		// constructor   
		public Bank() {  
		balance = 0.0;  
		}
		  
		// constructor with initial balance  
		public Bank(double balance) {  
		this.balance = balance;  
		}  
		//adds dollars
		public void addDollars(double dollars) {  
		balance += dollars;  
		}  
		//converts yen to dollars
		public void addYens(double yens) {  
		balance += (yens / YenToUS);  
		}  
		//converts euros to dollars
		public void addEuros(double euros) {  
		balance += (euros / EuroToUS);  
		}  
	
		//rounds decimal two places in balance
		public String getBalance2digits() {  
		return balance;  
	}  
}  

This post has been edited by tami3113: 24 April 2008 - 09:05 AM

Was This Post Helpful? 0
  • +
  • -

#14 tami3113  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 03-November 07

Re: Question about why I cant compile

Posted 24 April 2008 - 09:46 AM

View Posttami3113, on 24 Apr, 2008 - 08:50 AM, said:

Okay, I understand. I've made corrections to my bank. I'm getting an error code when I try to compile that says : incompatable types, and it directs me to the return balance line. Is that because I did not declare the balance correctly?
When I compile, it is not rounding only 2 two places. It is still giving me as many decimal places as it wants.

I tried to sift through the snippets, but I'm not understanding what the one referring to decimals is illustrating.

import javax.swing.JOptionPane;

public class Bank 
{  
	//conversions
	private final static double EuroToUS = .67;  
	private final static double YenToUS = 110.00;  
		  
	private double balance;  
	  
		// constructor   
		public Bank() {  
		balance = 0.0;  
		}
		  
		// constructor with initial balance  
		public Bank(double balance) {  
		this.balance = balance;  
		}  
		//adds dollars
		public void addDollars(double dollars) {  
		balance += dollars;  
		}  
		//converts yen to dollars
		public void addYens(double yens) {  
		balance += (yens / YenToUS);  
		}  
		//converts euros to dollars
		public void addEuros(double euros) {  
		balance += (euros / EuroToUS);  
		}  
	
		//rounds decimal two places in balance
		public String getBalance2digits() {  
		return balance;  
	}  
}  

This post has been edited by tami3113: 24 April 2008 - 09:47 AM

Was This Post Helpful? 0
  • +
  • -

#15 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,904
  • Joined: 06-March 08

Re: Question about why I cant compile

Posted 24 April 2008 - 03:14 PM

	//rounds decimal two places in balance
	public String getBalance2digits() {  
	return balance;  
	}  
}  




You need to do more than that you are just returning the value as a String as you do in your printf

	//rounds decimal two places in balance
	public String getBalance2digits() {  
	return balance;  
	}  
	//rounds decimal three places in balance
	public String getBalance3digits() {  
	return balance;  
	}  
	//rounds decimal four places in balance
	public String getBalance4digits() {  
	return balance;  
	}  
}  



Now what is the difference between these 3 methods ?
By wgich magic Java would know 2, 3 or 4 decimal digits
You need a DecimalFormat

	//rounds decimal two places in balance
	public String getBalance2digits() {  
				   DecimalFormat df = new DecimalFormat("0.00");
	   return df.format(balance);  
	}  
}  
	//rounds decimal three places in balance
	public String getBalance3digits() {  
				   DecimalFormat df = new DecimalFormat("0.000");
	   return df.format(balance);  
	}  
}  


Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2