Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
class Demo2
{
int i=1,j=2;
void fun1()
{
i=i+1;
j=j+1;
}
public static void main(String[] args)
{
Demo2 d1=new Demo2();
d1.fun1();
d1.fun1();
d1.fun1();
System.out.println("Hello World!");
}
}
symbol cannot find and func cant be applied errors are showing Please help me i am a basic learner....
There are several errors in the code. I've commented and suggested some working code.
class Demo2
{
void fun1()
{
i=i+1; //i has not been initialized
j=j+1; //j has not been initialized
}
public static void main(String[] args)
{
Demo2 d1=new Demo2();
d1.fun1(1);//"fun1" does not accept a parameter.
d1.fun1();
d1.fun1();
System.out.println("Hello World!");
}
}
Here is a working Demo2 class that may help you along:
class Demo2
{
int i = 0;
int j = 0;
public void fun1(int param)
{
i=i+param;
j=j+param;
}
public static void main(String[] args)
{
Demo2 d = new Demo2();
d.fun1(1);//adds 1 to both i and j
d.fun1(2);//adds 2 to both i and j
System.out.println("i is equal to " + i);
System.out.println("j is equal to " + j);
}
}
You Does not declare i and j and doesnot defined a method fun1 with one argument
It's very important to understand why your code does not compile, or you will not be able to do anything else even if it gets fixed.
For starters, declare i and j by putting them right under your class declaration
class Demo2 {
int i = 0;
int j = 0;
Now look at where you defined void fun1(). You don't allow it to accept any parameters. So d1.fun1(); is allowed but d1.fun1(1); is not. You did not define a function named fun1 that accepts an int as a parameter. You would have to define it like so:
void fun1(int input) {
// Whatever you wanted this function to do
}
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I am new to java, trying to learn by making mini projects right now. I have two classes and when I run my program I have this error : Cannot make a static reference to the non-static field Game.balance.
Not quite sure why I am getting it and wondering if anyone knows any fixes.
import java.util.Random;
import java.util.Scanner;
public class Mainone {
public static void main(String[] args) {
System.out.println("You have $1000. I hope you make good choices!");
Scanner user = new Scanner(System.in);
Game print = new Game(1000,0,0,true);
System.out.print(Game.operation);
}
}
this is the second class below (new file)
import java.util.Random;
public class Game {
int balance = 1000;
int operationAmount;
int randOperation;
boolean ad = true;
public Game(int b, int o, int r, boolean a) {
balance = b;
operationAmount = o;
randOperation = r;
ad = a;
}
}
System.out.print(Game.operation);
change to
System.out.print(print.operation);
In java, you can't access Object field values by Class directly. But you can access the static field by using Class.
public class Game {
public static String ABC = "1"; // can access by Game.ABC
int balance = 1000;
int operationAmount;
int randOperation;
boolean ad = true;
public Game(int b, int o, int r, boolean a) {
balance = b;
operationAmount = o;
randOperation = r;
ad = a;
}
}
public static void main(String[] args) {
System.out.println("You have $1000. I hope you make good choices!");
Scanner user = new Scanner(System.in);
Game print = new Game(1000,0,0,true);
System.out.print(Game.ABC); // here you can access the static field
}
you are trying to call Game.operation. Instead of this try print.operation.
I assume operation is function in class Game.
Here the state is managed by the print object of Game class.
Please call System.out.print(print.operation);
I'm trying to use RandomGrid rGrid in the compareGrids method of my project, however I can't seem to figure out how to do so. I origionally had the RandomGrid constructor outside the main method, but I got a stack overflow error whenever I tried to run it. I have another class very similar to this one with a ButtonGrid constructor outside of the main method and it works fine. Thanks for any help!
public static void main(String[] args) {
new ButtonGrid(WIDTH, LENGTH);
RandomGrid rGrid = new RandomGrid(WIDTH, LENGTH);
}
public int compareGrids() {
String[] args = {};
ButtonGrid.main(args);
int numCorrect = 0;
for (int i = 0; i < WIDTH; i++) {
for (int j = 0; j < LENGTH; j++) {
if (grid[i][j].getBackground() == ButtonGrid.main(rGrid).grid[i][j].getBackground()) {
numCorrect++;
}
}
}
System.out.println(numCorrect);
return numCorrect;
}
Simple answer:
Make compareGrids() static and add arguments
public static int compareGrids(Grid grid1, Grid grid2) {
...
}
This is an entry level question, You may want to consult a simple java tutorial to understand basic class elements and modifiers
To use rGrid in compareGrids it has to be class level variable .
Declare
RandomGrid rGrid = new RandomGrid(WIDTH, LENGTH);
outside main.
It should solve your issue.
Presently scope of rgrid ends as soon as main method finishes
Please paste stack overflow error that you mentioned
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I am writing a java method which runs through an array and if a value is present, then it returns the index of the value. It is not compiling, but I don't know what part of my code isn't comprehensive.
import java.util.*;
import static java.lang.System.out;
public class Lab26 {
public static void main(String[] args) {
}
public static int simpleSearch(int[] nums, int value) {
int nul = 0;
for (int i = 0; i < nums.length; i++) {
if (nums[i] == value) {
return i;
}
}
}
}
What if if (nums[i] == value) is never satisfied? You will not return anything but the method signature expects you to return an int.
in simpleSearch method, return an integer at the end.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I just started learning Java and I have a problem with defining a class.
I defined some variables in a class, but everywhere I wrote a command for printing a value of a string (or other variables) I got an error that says "cannot find symbol". Does that mean I can't use a print command in a class? Can you do a favor and simply explain me what to do?
Here is my codes (it's just for testing):
class Variable {
int m = 15;
boolean myb = true;
double mon = 2.4;
}
if you have
class Variable {
public int m = 15;
public boolean myb = true;
public double mon = 2.4;
}
iv'e added public modifier so fields are accessible
then your print code should be
public static void main(String[] args) {
Variable object=new Vairable(); //create instance of class
System.out.println(object.m);
System.out.println(object.myb);
System.out.println(object.mon);
}
You would need to print from a method within the class, not just in the class.
class Variable {
int m = 15;
boolean myb = true;
double mon = 2.4;
public void printVars() {
System.out.println("m: " + m);
System.out.println("myb: " + myb);
System.out.println("mon: " + mon);
}
}
After that, you would create a new Variable and call it's printVars() method.
Or, if this class is what you're trying to run, you could put your main in here.
class Variable {
static int m = 15;
static boolean myb = true;
static double mon = 2.4;
public static void main(String[] args) {
System.out.println("m: " + m);
System.out.println("myb: " + myb);
System.out.println("mon: " + mon);
}
}
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I'm trying to create a class with a method that prints all integers between any two given integers. This is what I've got now-
public class IntList {
public static void main(String[] args) {
int start = Integer.parseInt(args[0]);
int stop = Integer.parseInt(args[1]);
for (int i = start + 1; i < stop; i++) {
System.out.print(i);
}
}
}
This won't compile, I get 2 errors saying "reached end of file while parsing", once each for lines 4 and 5.
Wrong main() method declaration. You have to pass an array as the only parameter for this function.
Then either declare your start and stop variables as local, and do the job inside the main method itself, or create a new function you call from the main() method.
No more explanation needed, this is Java basics. You should read a Java lesson.
There are 2 problem with this code
Main method doesn't have proper signature
make main like
public static void main(String ar[]){
}
and create another static method to accept two int variable
duplicate local variable declaration
remove
int i;
You already declare and initialize as a part of for loop
It will give you duplicate local variable error
Your main method declaration is incorrect. The argument list in a Java application's main method is required to be a String array. Read the start and stop values from the first 2 values of the String array after removing the duplicate declaration of the variable i:
public static void main(String[] args) {
int start = Integer.parseInt(args[0]);
int stop = Integer.parseInt(args[1]);
for (int i = start + 1; i < stop; i++) {
System.out.print(i);
}
}
Don't forget to pass in the start & stop values to the application
java IntList 1 10
You need public static void main(String[]) in your class in order it to be executed.
import java.util.Random;
public class IntList {
public static void main(int start, int stop){
for (int i = start + 1; i < stop; i++) {
System.out.print(i);
}
}
public static void main(String args[]){
main(random.nextInt(20),random.nextInt(100));
}
}