user3326587
user3326587

Reputation: 31

How to set up a java program in IDEone

i am pretty new to coding and to date have only been using bluej to code java. I want to switch to using IDEone to code so i can switch from the computers i use at school in my programming class to my home computer without having to copy the code from one to the other using a usb or something. The problem is i do not know how to write a program in IDEone. I have a couple of programs that i have made in bluej that compile and execute fine but when pasted into IDEone to see if it would work and i keep getting errors. Here is an example of one of the codes

import java.util.Scanner;
public class IncomeTaxCalculator{

    public static void main(String [] args){

        // Constants
        final double TAX_RATE = 0.20;
        final double STANDARD_DEDUCTION = 10000.0;
        final double DEPENDENT_DEDUCTION = 2000.0;

        Scanner reader = new Scanner(System.in);

        double grossIncome;
        int numDependents;
        double taxableIncome;
        double incomeTax;

        // Request the inputs
       System.out.print("Enter the gross income: ");
       grossIncome = reader.nextDouble();
       System.out.print("Enter the number of dependents: ");
       numDependents = reader.nextInt();

       //Compute the income tax
       taxableIncome = grossIncome - STANDARD_DEDUCTION - DEPENDENT_DEDUCTION * numDependents;
       incomeTax = taxableIncome * TAX_RATE;

       //Display the income tax
       System.out.println("The income TAX IS $" + incomeTax);
    }
}

in IDEone this gives me the error: Main.java:3: error: class IncomeTaxCalculator is public, should be declared in a file named IncomeTaxCalculator.java public class IncomeTaxCalculator{

What is the appropriate way for me to start a program in IDEone? how would i change this program so it will compile corectly

Upvotes: 2

Views: 2942

Answers (2)

Fishies
Fishies

Reputation: 5

As Mark suggested above, ideone is mainly for small snippets. Might I suggest you use something such as Eclipse?

It's a great general IDE that your school might have on their computers already.

And the error that you are getting is because (at least from what it looks like above) you just have

 public static void main(String [] args) ...

when your code should have

public class IncomeTaxCalculator {

at the top then you start your main method.

Then all of your code would be contained in a file also named IncomeTaxCalculator.java.

Hope this helps a bit!

Upvotes: 0

Mark Peters
Mark Peters

Reputation: 81124

This isn't really what ideone was designed for; it's meant to be a tool to share small snippets of code, e.g. for posting on StackOverflow. It's not meant to be an IDE for an entire project.

In this case, the error message is telling you that behind the scenes, ideone saves your code to a file Main.java and so it expects your class to be named Main, not IncomeTaxCalculator. Alternatively, the class can be made not public.

The following is the ideone template for Java snippets. It explains the requirements clearly:

/* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
    public static void main (String[] args) throws java.lang.Exception
    {
        // your code goes here
    }
}

Upvotes: 4

Related Questions