I have really simple code, I have deleted odd code.
So this my class, one of his method is static and I would like to use it later in Main class:
public class TradeInformationReader {
private static String tradeType = "FX_SPOT";
public static double tradePrice = -1;
private double price;
public static int setTradeInformation(String path_to_file) {
return 1;
}
}
And here how I trying to call this last method:
public class Main {
public static int main(String[] args) {
String path_to_file = "D:\\1.txt";
if (0 > TradeInformationReader.setTradeInformation(path_to_file)) {
return -1;
}
return 1;
}
}
I read many posts with a similar issue, but couldn't find a solution. Everything looks fine to me. IDE doesn't show any mistakes and I'm trying just to call static method setTradeInformation, why it does not recognize it (cannot find symbol method setTradeInformation)? Any ideas? I will really appreciate your help.
Your main is not a valid main, so I guess your IDE cannot find a launching class. This should be
public static void main(String[] args)
First you have to put TradeInformationReader class in a seperate file called : TradeInformationReader.java
as follows : `
public class TradeInformationReader {
private static String tradeType = "FX_SPOT";
public static double tradePrice = -1;
private double price;
public static int setTradeInformation(String path_to_file) {
//integer to identify whether the file is found or not 1 if found and 0 if not
int isFileFound = 1;
// the code required to get the file and modify the state of the of isFileFound variable
return isFileFound;
}
}
`
then the main class should have void return type and should be in a file that has the same name as the Main Class as follows:
public class firstApp {
public static void main(String[] args) {
String path_to_file = "D:\\1.txt";
if (0 > TradeInformationReader.setTradeInformation(path_to_file)) {
System.out.println("File not found");
}
}
}
`
Related
I have an extremely simple piece of code where I am calling a method that I defined, and it wont run because it says I haven't defined the method?
class Main {
public static void main(String[] args) {
go(7,3);
}
}
/// in a separate file named go.java -->
class go{
public static int go(int x, int y){
if(x <= 1)
return y;
else
return go(x - 1, y) + y;
}
}
You have two options, use the class name go to identify the class containing the method
public static void main(String[] args) {
go.go(7,3);
}
or static import it like
import static go.go;
public class Main {
public static void main(String[] args) {
go(7,3);
}
}
First thing first, Class name should start with upper case, your another class should be Go.java , to access static method
Refer Class name that is Go.go(7,3);
I wonder if there is a way (a gradle script or any script or any other way without an IDE) to remove methods annotated with certain annotations. Example:
class x {
public static void main(String[] args) {
int x = getValue();
System.out.println(x);
}
#RemoveEnabled(id = "getValueMethod1", return = "10")
int getValue() {
return 20;
}
}
Now when I run the script or gradle target, it should remove the getValue() method, and the output code should become:
class x {
public static void main(String[] args) {
int x = 10;
System.out.println(x);
}
}
Is there an existing script or way to achieve this? It might be achievable with grep and String parsing etc., but I'm looking for a cleaner solution which is able to get all methods by an annotation id, and remove them with formatting. I tried searching on Google, Stack Overflow etc., but couldn't find a solution.
I wrote a module to process similiar task.
https://github.com/KnIfER/Metaline
#StripMethods(keys={"ToRemove","AlsoRemove"})
public class YourCLZ{
void ToRemove(){} // will be removed
void AlsoRemove(){} // will be removed
#StripMethods(strip=true)
void AnotherTest(){} // will also be removed
}
in your case
#StripMethods(keys="getValue")
class yourx {
public static void main(String[] args) {
int x = getValue();
System.out.println(x);
}
int getValue() {
return 20;
}
}
will become:
class yourx {
public static void main(String[] args) {
System.out.println(x);
}
}
you will get a compilation error since for now my module simply remove any methods or method body statements that contain the key you specified.
I'm reading the types of error in Code Smells and trying to understand in the category Inappropriate Intimacy,
I found the solution move field. Can someone exemplify and explain me that solution "Move field"?
I found something in https:refactoring.guru/move-field but I still do not understand.
For example field MOVE_ME is used only in different class (MyRealUssage). so you can move it:
public class Unused {
public String moveMe = "This is used only in other classes"
}
public class MyRealUssage {
public static void main(String[] args) {
System.out.println(new Unused().moveMe);
}
}
Move field will update valid field location:
public class Unused {
}
public class MyRealUssage {
public String moveMe = "This is used only in other classes";
public static void main(String[] args) {
System.out.println(new MyRealUssage().moveMe);
}
}
Obviously a better code will also removing unused Unused and use getter for field,as
public class MyRealUssage {
public String moveMe = "This is now used only this class";
public static void main(String[] args) {
System.out.println(getMoveMe());
}
public String getMoveMe() {
return moveMe;
}
}
This will probably will get down voted, so if you do down vote me, can you provided a link to where I can find this?
What am I doing wrong here? I am very new and it seems like this should work. I just don't know what I am doing wrong. This is my error
public class Test
{
public static long calculate(long n)
{
n = args[0];
return n;
}
public static void main(String[] args)
{
long answer;
answer = calculate();
}
}
Exception:
Test.java:6: error: cannot find symbol
n = args[0];
^
symbol: variable args
location: class Test
Test.java:13: error: method calculate in class Test cannot be applied to given types;
answer = calculate() ;
^
required: long
found: no arguments
reason: actual and formal argument lists differ in length
2 errors
args is a String array local to the main method.
So firstly it is a local variable of the main method and it is not visible inside the calculate method which explains the first error: error: cannot find symbol.
Secondly calculate expects a long parameter and your are trying to supply a String. For that you are getting error: method calculate in class Test cannot be applied to given types;
So pass args[0] to the calculate after converting it to long as a parameter.
public class Test
{
public static long calculate(long n)
{
return n;
}
public static void main(String[] args)
{
long answer = 0L;
try{
answer = calculate(Long.parseLong(args[0]));
}catch (ArrayIndexOutOfBoundsException ae){
ae.printStackTrace();
}catch (NumberFormatException nfe){
nfe.printStackTrace();
}
System.out.println(answer);
}
}
In whole class there is no instance variable defined with named args, variable which you are trying to use is a parameter in main method and accessible only inside main method.
By considering your code your doing nothing inside calculate so you can write main method as follows:
public static void main(String[] args)
{
long answer;
answer = Long.parseLong(args[0]);
}
Both code will do same work.
Below code can solve your issue
public class Test
{
public static long calculate(String[] args)
{
long n = Long.parseLong(args[0]);
return n;
}
public static void main(String[] args)
{
long answer;
answer = calculate(args);
}
}
I am fairly new to java, and am having what I assume is a simple problem with my program.
For the method arrayTest2, I cannot import it into main due to an error on compilation:
"Cannot find symbol, symbol: variable dataStorage".
I have tried also tried the declarations:
arrayTest2(dataStorage[][])
and
arrayTest2(dataStorage[5][5])`
but they don't work.
Any help would be greatly appreciated.
Cheers
import io.*;
public class TrialArray
{
public static void main(String [] args)
{
arrayTest();
arrayTest2(dataStorage);
}
public static void arrayTest()
{
int[][] dataStorage = new int[5][5];
dataStorage[1][2] = 1;
System.out.printf("THIS PART WORKS");
}
public static void arrayTest2(int[][] dataStorage)
{
dataStorage[2][2] = 3;
System.out.printf("THIS DOESNT");
}
}
The problem here is the scope: Something defined in one function is not visible in another. What you will normally do to solve this is to return the value. Something like this:
import io.*;
public class TrialArray
{
public static void main(String [] args)
{
int[][] dataStorage = arrayTest();
arrayTest2(dataStorage);
}
public static int[][] arrayTest()
{
int[][] dataStorage = new int[5][5];
dataStorage[1][2] = 1;
System.out.printf("THIS PART WORKS");
return dataStorage;
}
public static void arrayTest2(int[][] dataStorage)
{
dataStorage[2][2] = 3;
System.out.printf("THIS DOESNT");
}
}
Alternatively you could have your dataStorage field as a global variable, this is however potentially very confusing. To do that you'd define
public class TrialArray
{
private static int[][] dataStorage;
// ...
public static void arrayTest() {
dataStorage = new int[5][5];
dataStorage[1][2] = 1;
System.out.printf("THIS PART WORKS");
}
// ...
}
on this line
arrayTest2(dataStorage);
You are passing parameter to method, that has one argument "dataStorage", but you don't declare it.
You try to pass dataStorage to your arrayTest-function, but dataStorage is not a field of the class, neither is it a local variable of main (aka dataStorage does not exist in main).
public static void main(String [] args) {
arrayTest();
arrayTest2(dataStorage); //<------- What is dataStorage?
}
Here is a little tutorial on variable scopes in Java. You probably want to return the array you created in arrayTest() and use it, but I am just guessing what you want to do.
You cant access variables in declared inside other methods.
To make it work, you would have to do this:
public class TrialArray
{
int[][] dataStorage;
public static void main(String [] args)
{
dataStorage = new int[5][5];
arrayTest();
arrayTest2(dataStorage);
}
public static void arrayTest()
{
dataStorage[1][2] = 1;
System.out.printf("THIS PART WORKS");
}
public static void arrayTest2(int[][] dataStorage)
{
dataStorage[2][2] = 3;
System.out.printf("THIS DOESNT");
}
}