简体   繁体   中英

Error running and compiling java code related issues

Issue with running the code

Getting constant illegal escape character

declaring variables, and strings which seem to be declared properly.

 import java.swing.*;
 import java.text.DecimalFormat;

 public class TWP1Project {
    private static Object dataIn;


public static void main(String[] args) {
    // declare class variables
    int hours;
    double fee, rate, tuition;
    // call methods
    displayWelcome();
    hours = getHours();
    rate = getRate();
    tuition = calcTuition(hours);
    fee = myArray(1);
    diplayTotal(tuition+fee);

}
public static void displayWelcome()
{
     System.out.println("\Welcome to the Tuition and Fees Calculator");
}

} 

public static int getHours()
 {
      //declare variables
     String strHours;
     int hours = 0;

     //prompts user for input
     System.out.println("Enter the number of hours accrued in a class.");

     hours = dataIn.readLine();

     try
        {
            hours = Integer.parseInt(strHours);   
        }
     catch{NumberFormatException e};
     JOptionPane.showMessageDialog{null,"Your entry was not in the proper format.",
             "Error"JOptionPane.INFORMATION_MESSAGE};
             {
                 System.out.println("Please input whole numbers only");
             }
             return hours;
 }

 //the getRate() method ask user to input the rate per credit hours.
  public static double getRate()
  {
      int hours =12;
      if (hours>12)
          System.out.println("calculate the rate per credit hours");
      else
          if (hours<12) 
               System.out.println("credit hours is inaccurate");
      else
              System.out.println("zero");
  }
}
  return rate;
  //the calcTuition() allowed to calculate tuition.
  public static double calcTuition()
  {`enter code here`
      int hours
      double rate =0.0;
      double rate * hours         
  }
  return tuition;
  //
  public static myArray(int tuition)
  {
      int tuition
      double fee
  }
  return fee

Need help correcting if you can or offer some tips. Having issues running it in java correctly.

System.out.println("\\Welcome to the Tuition and Fees Calculator");删除\\ System.out.println("\\Welcome to the Tuition and Fees Calculator");

Or if you need "\\" in your message, put double backslash.

Try this:

public static void displayWelcome()
{
     System.out.println("\\Welcome to the Tuition and Fees Calculator");
}

More info: https://docs.oracle.com/javase/tutorial/java/data/characters.html

You have multiple errors, i have solved some of them, and given you some directions...Please, use an IDE such as Netbeans or Eclipse while writing the code...i have left some of the others to you, and explained what you should do about them

import java.io.Console;
import java.text.DecimalFormat;

import javax.swing.JOptionPane;

public class TWP1Project {
    private static Console dataIn; // if you want to read from keyboard or
                                    // something, this should be of type
                                    // Scanner, not Object - to use it you need
                                    // to also add import java.util.Scanner;

    public static void main(String[] args) {
        // declare class variables
        int hours;
        double fee, rate, tuition;
        // call methods
        displayWelcome();
        hours = getHours();
        rate = getRate();
        tuition = calcTuition(hours);
        fee = myArray(1); // you have
        diplayTotal(tuition + fee);

    }

    public static void displayWelcome() {
        System.out.println("Welcome to the Tuition and Fees Calculator"); // no
                                                                            // backslash
    } // erased one brace this

    public static int getHours() {
        // declare variables
        String strHours; // you didn't initialize strHours
        int hours = 0;

        // prompts user for input
        System.out.println("Enter the number of hours accrued in a class.");
        dataIn = System.console(); // need to initialize in order to read lines
                                    // from it
        hours = Integer.parseInt(dataIn.readLine()); // this doesn't prompt for
                                                        // input, you need a
                                                        // scanner object

        try {
            hours = Integer.parseInt(strHours); // didn't initialize strHours
        } catch (NumberFormatException e) {
        } // pay attention here
        JOptionPane.showMessageDialog(null,
                "Your entry was not in the proper format.", "Error",
                JOptionPane.INFORMATION_MESSAGE); // you need ( , not {
        {
            System.out.println("Please input whole numbers only");
        }
        return hours;
    }

    // the getRate() method ask user to input the rate per credit hours.
    public static double getRate() {
        int hours = 12;
        if (hours > 12)
            System.out.println("calculate the rate per credit hours");
        else if (hours < 12)
            System.out.println("credit hours is inaccurate");
        else
            System.out.println("zero");
        return rate; // you need to initialize the rate somewhere...i'll let
                        // this for you
    }

    // the calcTuition() allowed to calculate tuition.
    public static double calcTuition() {
        int hours;
        double rate = 0.0;
        double tuition = rate * hours; // i don't actually know what you meant
                                        // here, but you should declare the
                                        // tuition and then initialize it
        return tuition;
    }

    //
    public static myArray(int tuition) // you forgot the return type
    {
        // don't know what you meant here, but again, initialize
        double fee;

        return fee;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM