问题描述
我想一劳永逸地理解这一点。
I would like to understand this once and for all.
请原谅下面粘贴的大量代码,但我不想遗漏任何细节。
With this please excuse the mass of code pasted below, but I do not want to leave out any details.
我唯一改变的是加载的URL。但这不会导致错误。
The only thing I changed is the URL loaded. But this is not causing the error.
我想将我的功能称为 readPosiitons 。轻松解决方案,使其静止。真正的解决方案,我不确定。
I would like to call my function "readPosiitons". Easy solution, make it static. Real solution, I am not sure of.
请帮助我更好地了解如何以正确的方式解决此错误。
Please help me to better understand how to solve this error in the correct way.
谢谢!!
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package PandL;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Scanner;
import toolBox.Secretary;
import toolBox.Secretary.positionObj;
/**
*
* @author Jason
*
*/
public class GarageComm {
public static void main(String[] args) throws MalformedURLException, IOException{
String retStr;
String startM;
String endM;
String myURL;
String[] Split1=null;
Integer lnCount;
HashMap hashPos=new HashMap();
hashPos= readPositions("holdingsBU.txt");//the error is here
myURL="http://myUrl?s=";
URL url = new URL(myURL);
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
in.close();
}
public HashMap readPositions(String destFile){
HashMap<String, Secretary.positionObj> hashPositions=new HashMap<String,positionObj>();
Secretary mySecretary=new Secretary();
try{
File F=new File(destFile);
if(F.exists()){
System.out.println("File Exists: "+F.exists());
System.out.println(destFile);
Scanner sC= new Scanner(F);
while (sC.hasNext()){
String[] Splitter1;
Secretary.positionObj position=mySecretary.new positionObj();
Splitter1=sC.nextLine().split(",");
position.positionDate=Double.parseDouble(Splitter1[0]);
position.positionTicker=(Splitter1[1]);
position.positionOpen=Double.parseDouble(Splitter1[2]);
position.positionPrice=Double.parseDouble(Splitter1[3]);
position.positionSMA=Double.parseDouble(Splitter1[4]);
position.positionUpdated=Double.parseDouble(Splitter1[5]);
position.priceUpdated=Double.parseDouble(Splitter1[6]);
position.updateDate=Double.parseDouble(Splitter1[7]);
hashPositions.put(position.positionTicker.trim(), position);
}
}else{
System.out.println("File Created: "+ F.createNewFile());
System.out.println("----No previous positions----");
}
}catch (Exception E){
System.err.println(destFile + " does not exist.");
hashPositions.put("ERROR", null);
E.printStackTrace();
}
return hashPositions;
}
}
推荐答案
Real解?不要在 main()
方法中放入太多东西。那是为了新手。
Real solution? Don't put so much stuff in the main()
method. That's for noobs.
Java是一种面向对象的语言。将逻辑放在与 GarageComm
类关联的方法中。 main()
除了实例化一个实例并调用它的方法之外,应该做的不多。
Java's an object-oriented language. Put the logic inside methods associated with the GarageComm
class. main()
should do little more than instantiate an instance and call its methods.
像这样改变:
GarageComm gc = new GarageComm();
hashPos= gc.readPositions("holdingsBU.txt");//the error is here
这篇关于无法从静态上下文引用非静态方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!