Here is my java code
public class Account {
private String accName;
private String accId;
private int balance;
Account()
{
System.out.println("This is an empty constructor.");
}
Account(String name)
{
accName = name;
System.out.println("This is an valued constructor.");
}
public class main{
public Static void main(String[] args)
{
Account a1 = new Account("raff");
}
}
}
cmd says ' "(identifier)" expected
public Static void main(String[] args)'
I can't get the problem yet......
You have several problems there:
public class within public class
main shouldn't be a class there at all
"Static", a mispelling of the static keyword
Probably this is how you meant to write that:
public class Account {
private String accName;
private String accId;
private int balance;
Account() {
System.out.println("This is an empty constructor.");
}
Account(String name) {
accName = name;
System.out.println("This is an valued constructor.");
}
public static void main(String[] args) {
Account a1 = new Account("raff");
}
}
The static keyword, like all other Java keywords, should be written in lowercase:
public static void main(String[] args) {
Related
I am trying to use a getter to retrieve some variables that the user will input into a GUI, however it gives me an "non-static method cannot be referenced from a static context" error. I have tried making my getters and setters static and that did not work. I am not sure where to go from there. Here is my code:
Main:
public class creditInfoSystem {
String name = infoGUI.getName();
public static void main(String[] args) {
new infoGUI();
}
public void getData() {
}
}
Getters+Setters From GUI Class:
public void setName(String newName){
name = newName;
}
public String getName(){
return name;
}
public void setCustNum(double newCustNum){
custNum = newCustNum;
}
public double getCustNum(){
return custNum;
}
public void setCreditLimit(double newCreditLimit){
creditLimit = newCreditLimit;
}
public double getCreditLimit(){
return creditLimit;
}
public void setPrevBalance(double newPrevBalance){
prevBalance = newPrevBalance;
}
public double getPrevBalance(){
return prevBalance;
}
public void setCurrentPurchases(double newCurrentPurchases){
currentPurchases = newCurrentPurchases;
}
public double getCurrentPurchases(){
return currentPurchases;
}
public void setPayments(double newPayments){
payments = newPayments;
}
public double getPayments(){
return payments;
}
public void setCreditsReturns(double newCreditsReturns){
creditsReturns = newCreditsReturns;
}
public double getCreditsReturns(){
return creditsReturns;
}
public void setLateFees(double newLateFees){
lateFees = newLateFees;
}
public double getLateFees(){
return lateFees;
}
I can provide more parts of the code if needed.
The problem is that the creditInfoSystem class has no idea which infoGUI class you are referring to. Try and make infoGUI a parameter of creditInfoSystem and retrieve the data from there.
public class creditInfoSystem {
private InfoGUI infoGUI;
public creditInfoSystem(InfoGUI gui){
infoGUI = gui;
name = infoGUI.getName();
}
public static void main(String[] args) {
InfoGUI infoGUI = new infoGUI();
CreditInfoSystem sys = new creditInfoSystem(infoGUI);
}
}
If you are building a swing/FX GUI:
One problem I can predict is knowing when the name in the GUI is actually set. You should maybe consider making the GUI the main program where CreditInfoSystem is an attribute of the GUI. Then when a button is clicked or some other action, the input from the GUI is taken and passed to the CreditInfoSystem.
First of all, rename your classs InfoGUI and CreditInfoSystem, starting with an upper case, it is a Java convention.
Then, when you do InfoGUI.getName() you are trying to access a class method, which would have to be static. What you want to do is create an instance of InfoGUI and access it's instance method.
Also your name variable should be static because you use it in a static method, ie public static void main
public class CreditInfoSystem {
private static String name;
public static void main(String[] args) {
// Create an instance of your InfoGUI class
InfoGUI infoGuiInstance = new InfoGUI();
// Call the getter on the instance you just created
name = infoGuiInstance.getName();
}
}
And the renamed InfoGUI class:
class InfoGUI {
String name;
public void setName(String newName) {
name = newName;
}
public String getName() {
return name;
}
}
I am trying to declare an array from my main class with setters I don' t know if this is the correct way.In my other class i have all the methods and they work fine but i need to declare the array and it compiles but it doesn't seem like the arrays CC and ccBal are declared.
Here is my code and this is where the problem is. I dont know if i am initializing the array with the setters correctly.
public class handleCustomers {
public static void main(String[] args) {
Customer [] CC = new Customer[1];
CC [0] = new Customer();
CC[0].setCC(new String[]{"1234567894123569"});
CC[0].setCCBal(new double []{3070.00});
}
This is the Customer class
public class Customer {
private String[] CC;
private double[] ccBal;
public Customer() {}// default constructor
public Customer(String [] CreditCards){
CC = CreditCards;
}
public Customer(double [] creditBalance){
ccBal = creditBalance;
}
public String [] getCC(){// Getters
return CC;
}
public double [] getCCBal() {
return ccBal;
}
public void setCC(String [] CreditCards){// Setters
CC = CreditCards;
}
public void setCCBal(double [] creditBalance){
ccBal = creditBalance;
}
I'm not sure what you trying to do, I can't ask you in comments because I'm new in StackOverflow. Provide me more information. But that's how you go with this type of problem. And I don't know why you creating array as you just have only one value. Best
public class handleCustomers {
public static void main(String[] args) {
Customer bank = new Customer("1234567894123569",3070.00);
System.out.println(bank.toString());
}
}
Customer Class
public class Customer {
private String CC;
private double ccBal;
public Customer(String CC, double ccBal) {
this.CC = CC;
this.ccBal = ccBal;
}
public String getCC(){// Getters
return this.CC;
}
public double getCCBal() {
return this.ccBal;
}
public void setCC(String CC){// Setters
this.CC = CC;
}
public void setCCBal(double creditBalance){
this.ccBal = ccBal;
}
public String toString() {
return "CC:" +CC + "balance: " +ccBal;
}
}
how do I call a void method from another class to a new class with main?
I have two classes, but I don't see the error I am making.
public class Person {
private int age;
private String name;
public Person(int a, String n) {
a = age;
n = name;
}
public void printInfo() {
System.out.println(age + name);
}
//
public class Main {
public static void main(String[] args) {
Person obj1 = new Person(22, "Dan");
obj1.printInfo();
}
}
EDIT: Move the main method to a different class and done.
TestPerson.java
public class TestPerson {
public static void main(String[] args) {
Person obj1 = new Person(22, "Dan");
obj1.printInfo();
}
}
You have few mistakes in your code. It should be like as follows :
public class Person {
private int age;
private String name;
//Your constructor was wrong
public Person(int a, String n) {
age = a;
name = n;
}
public void printInfo() {
System.out.println(age + name);
}
}
This question already has answers here:
Missing method body, or declare abstract in Java
(4 answers)
Closed 7 years ago.
public class Hw7pr1
{
public static void main(String[] args);
String eName;
double payrate;
public Hw7pr1(String name, double rate)
{
eName = name;
payrate = rate;
}
public String getEName()
{
return eName;
}
public void setEName(String name)
{
eName = name;
}
public double getPayrate()
{
return payrate;
}
public void setPayrate(double payrate)
{
this.payrate = payrate;
}
}
This is the mistake I get when compiling it.
Hw7pr1.java:3: error: missing method body, or declare abstract
public void main(String[] args);
^
Take out the semicolon after your main method declaration
should be like this
public static void main(String[] args) {
...
}
Remove ";" at the end of your main method and add instead a bracket
public static void main(String[] args){
and at the end of your main method (which seems not really used - empty) a closing one
}
The class:
public class Hw7pr1 {
public static void main(String[] args) {
// do something
}
String eName;
double payrate;
public Hw7pr1(String name, double rate) {
eName = name;
payrate = rate;
}
public String getEName()
{
return eName;
}
public void setEName(String name)
{
eName = name;
}
public double getPayrate()
{
return payrate;
}
public void setPayrate(double payrate)
{
this.payrate = payrate;
}
}
This question already has answers here:
Cannot make a static reference to the non-static method
(8 answers)
Closed 8 years ago.
I need to create a class for the methods described in the interface as follows:
public interface MyStringInterface {
// Sets the value of the current string
public void setString(String str);
// Returns the current string
public String getString();
}
When I create a class with methods as below, there is always this error and wont let me proceed. What am I doing wrong? How should I proceed?
package seclass.assgn;
public class MyString implements MyStringInterface{
static String name;
public static void main(String[] args) {
setString("This is my sentence");
System.out.println(getString());
}
public void setString(String str){
name = str;
}
public String getString() {
return name;
}
}
You need an instance of MyString -
public static void main(String[] args) {
MyString my = new MyString();
my.setString("This is my sentence");
System.out.println(my.getString());
}
also
static String name;
should be
private String name;
Alternatively, if you don't want an instance just use
public static void main(String[] args) {
name = "This is my sentence";
System.out.println(name);
}
First of all, you don't have to repeat "public" in interfaces.
public interface MyStringInterface {
void setString(String str);
String getString();
} // end of class MyStringInterface
Then you must make an Object of your implementation:
public class MyString implements MyStringInterface {
String name;
public static void main(String[] args) {
new MyString().top();
}
void top() {
setString("This is my sentence.");
System.out.println(getString());
}
#Override
public void setString(String str) {
this.name = str;
}
#Override
public String getString() {
return this.name;
}
} // end of class MyString