Compilation error in Stock program

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 423 Views - Last Post: 30 September 2012 - 10:51 PM Rate Topic: -----

#1 sysh  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 15
  • Joined: 30-September 12

Compilation error in Stock program

Posted 30 September 2012 - 06:00 PM

So I have the pretty well-known Stock Program assigned to me. I have most of the code down for what I'm needed to do, and it compiles correctly when I use Eclipse. However, part of the assignment requires me to compile and execute it through another compiler to make sure it works for others. Putting it through javac with Command Prompt and Ideone gets me

/**
This program was created for the purpose of mathematically finding
 the total price of stocks, commissions made, and the net profit after imputing the
 stock information given.
*/
import java.util.Scanner;

public class StockTransaction
{

	Scanner input = new Scanner(System.in);

int numberOfShares;
double sharePrice, soldPrice, totalPrice, boughtPrice, commission1, commission2, profit;

public void getValues()
	{
	System.out.println("Enter the original share price: ");
	sharePrice = input.nextDouble(); //sharePrice is the price we start with
	System.out.println("Enter the number of shares bought: ");
	numberOfShares = input.nextInt(); //numberOfShares is the number of stock bought and sold
	System.out.println("Enter the sold share price: ");
	soldPrice = input.nextDouble(); //soldPrice is the price the shares are eventually worth
}

public void calculateResults()
	{
	
	totalPrice = sharePrice * numberOfShares; //totalPrice is the first total
	commission1 = sharePrice * numberOfShares * .02;
	boughtPrice = soldPrice * numberOfShares; //boughtPrice is the second total
	commission2 = soldPrice * numberOfShares * .02;
	profit = (boughtPrice - commission2) - (totalPrice - commission1); //I'm assuming the net profit doesn't include commission
	}

public void displayResults()

	{
	System.out.println("Amount Joe paid for the total stock bought = $" + totalPrice);
	System.out.println("Stockbroker's commission after stock was bought = $" + commission1);
	System.out.println("Amount Joe made for the total stock sold = $" + boughtPrice);
	System.out.println("Stockbroker's commission after stock was sold = $" + commission2);
	System.out.println("Net Profit = $" + profit);
	}

public static void main(String[] args)

	{
		StockTransaction x = new StockTransaction();
	
	x.getValues();
	x.calculateResults();
	x.displayResults();
	
	}

}





The error I receive is

Main.java:8: class StockTransaction is public, should be declared in a file named StockTransaction.java
public class StockTransaction
^
The one error is a compilation error.

I keep looking at the code over and over, but I can't find the mistake I am making. How is it that it works with Eclipse and not with anything else?

Is This A Good Question/Topic? 0
  • +

Replies To: Compilation error in Stock program

#2 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,056
  • Joined: 28-December 10

Re: Compilation error in Stock program

Posted 30 September 2012 - 06:12 PM

What are you compiling? Is it a file named StockTransaction.java? Or is the file named just StockTransaction and is only File type, not a Java file type?
Was This Post Helpful? 0
  • +
  • -

#3 sysh  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 15
  • Joined: 30-September 12

Re: Compilation error in Stock program

Posted 30 September 2012 - 06:54 PM

View PostfromTheSprawl, on 30 September 2012 - 06:12 PM, said:

What are you compiling? Is it a file named StockTransaction.java? Or is the file named just StockTransaction and is only File type, not a Java file type?


I'm compiling the file named StockTransaction.java
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

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

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Compilation error in Stock program

Posted 30 September 2012 - 07:11 PM

If the name of the class and the name of the file didn't match, Eclipse would also complain
The problem is somewhere else

Post the output of the DIR command on your folder... or ls if you are on Unix
Was This Post Helpful? 0
  • +
  • -

#5 sysh  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 15
  • Joined: 30-September 12

Re: Compilation error in Stock program

Posted 30 September 2012 - 07:24 PM

View Postpbl, on 30 September 2012 - 07:11 PM, said:

If the name of the class and the name of the file didn't match, Eclipse would also complain
The problem is somewhere else

Post the output of the DIR command on your folder... or ls if you are on Unix

My DIR
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

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

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Compilation error in Stock program

Posted 30 September 2012 - 07:38 PM

??? :sleep1:
Was This Post Helpful? 0
  • +
  • -

#7 sysh  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 15
  • Joined: 30-September 12

Re: Compilation error in Stock program

Posted 30 September 2012 - 07:40 PM

My bad.

Attached image(s)

  • Attached Image

Was This Post Helpful? 0
  • +
  • -

#8 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,056
  • Joined: 28-December 10

Re: Compilation error in Stock program

Posted 30 September 2012 - 07:40 PM

Well, I think his project is on an Eclipse work space and the class files aren't in the bin folder of the Java folder.

Hey sysh, he meant on the cmd where you use javac type DIR and press enter, then copy the results and post it here. That is if you're using Windows. If you're using another OS, I don't know how though.
Was This Post Helpful? 0
  • +
  • -

#9 sysh  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 15
  • Joined: 30-September 12

Re: Compilation error in Stock program

Posted 30 September 2012 - 07:52 PM

View PostfromTheSprawl, on 30 September 2012 - 07:40 PM, said:

Well, I think his project is on an Eclipse work space and the class files aren't in the bin folder of the Java folder.

Hey sysh, he meant on the cmd where you use javac type DIR and press enter, then copy the results and post it here. That is if you're using Windows. If you're using another OS, I don't know how though.


My project is on my Eclipse work space, but I've also saved the file into my bin folder to see if that would help. I still have the same issue.

I've added a cap of using DIR after setting the path back to my java folder again. Is this what you meant?Attached Image
Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

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

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Compilation error in Stock program

Posted 30 September 2012 - 07:53 PM

So where is StockTransaction.java ?
Where is the folder where you > javac StockTransaction.java ?
Was This Post Helpful? 1
  • +
  • -

#11 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,056
  • Joined: 28-December 10

Re: Compilation error in Stock program

Posted 30 September 2012 - 07:56 PM

Yeah, we need you to execute the dir command where you execute the javac command.
Was This Post Helpful? 1
  • +
  • -

#12 sysh  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 15
  • Joined: 30-September 12

Re: Compilation error in Stock program

Posted 30 September 2012 - 08:16 PM

I'm not sure what I did, but somehow I've gone from getting an error to just not having the file recognized. I'm retracing my steps right now to see where I went wrong, but I can't even run the code at this point.

Attached image(s)

  • Attached Image

Was This Post Helpful? -1
  • +
  • -

#13 fromTheSprawl  Icon User is offline

  • Monomania
  • member icon

Reputation: 513
  • View blog
  • Posts: 2,056
  • Joined: 28-December 10

Re: Compilation error in Stock program

Posted 30 September 2012 - 08:20 PM

There, on J:\New> execute dir.
Was This Post Helpful? 0
  • +
  • -

#14 sysh  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 15
  • Joined: 30-September 12

Re: Compilation error in Stock program

Posted 30 September 2012 - 08:41 PM

View PostfromTheSprawl, on 30 September 2012 - 08:20 PM, said:

There, on J:\New> execute dir.

Here it is.

Attached image(s)

  • Attached Image

Was This Post Helpful? -1
  • +
  • -

#15 pbl  Icon User is offline

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

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Compilation error in Stock program

Posted 30 September 2012 - 08:50 PM

This is coming ridiculous :v:
You complain not being able to compile StockTransaction.java doing
> javac StockTransaction.java

and you are not able to show us the directory where this file is !!!
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2