Java 解决加密程序中的错误,其中/如何在null时声明变量可以';不可用
我将非常感谢您对我遇到的这个问题的任何帮助。在下面代码的第27行,我得到一个错误,位置无法解析为变量。(BufferedReader br=Parse.parseLocation(位置,键);) 初始化为null在我的程序中不起作用(我没有得到我想要的结果),我一直在想办法解决这个问题。应在何处以及如何正确申报? 为了更加清晰,我还粘贴了下面的解析文件。 提前谢谢Java 解决加密程序中的错误,其中/如何在null时声明变量可以';不可用,java,parsing,encryption,Java,Parsing,Encryption,我将非常感谢您对我遇到的这个问题的任何帮助。在下面代码的第27行,我得到一个错误,位置无法解析为变量。(BufferedReader br=Parse.parseLocation(位置,键);) 初始化为null在我的程序中不起作用(我没有得到我想要的结果),我一直在想办法解决这个问题。应在何处以及如何正确申报? 为了更加清晰,我还粘贴了下面的解析文件。 提前谢谢 import java.util.*; import java.io.*; public class Main { publi
import java.util.*;
import java.io.*;
public class Main {
public static void main(String[] args){
System.out.println("Enter Cipher Key:");
Scanner s = new Scanner(System.in);
String key = s.nextLine();
System.out.println("Enter a text file or URL to encrypt:");
System.out.flush();
Scanner r = new Scanner(System.in);
String filename = r.nextLine();
File f = new File(filename);
r.close();
s.close();
try{
Porta cipher = new Porta(key);
cipher.ensureCapacity((int)f.length());
FileWriter fw = new FileWriter("out.txt");
BufferedReader br = Parse.parseLocation(location, key);
String line = null;
while((line = br.readLine()) != null){
String encrypted = cipher.encrypt(line.toUpperCase());
fw.write(encrypted + "\n" );
}
br.close();
fw.flush();
fw.close();
}catch(Exception e){
System.out.println("Yikes! Something nasty happened");
e.printStackTrace();
}
}
}
以及上述解析文件,如下所示:
import java.io.*;
import java.net.URL;
public class Parse {
private static BufferedReader br;
private static final String FORM = "http://"; //Identify a URL header
public static BufferedReader parseLocation(String location, String key)
{
try{
if(location.startsWith(FORM)){
URL url = new URL(location);
br = new BufferedReader(new InputStreamReader(url.openStream()));
System.out.println("\nURL successfully read");
}
else{ //If input is a file
File file = new File(location); //New instance of URL
if(file.exists()){
br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
}
System.out.println("\nFile successfully read");
}
}
catch(IOException e)
{
e.printStackTrace(); //Prints the stack trace of the Exception to System.err.
}
return br;
}
}
这里您将location
作为参数传递,但尚未声明location
变量
在调用Parse.parseLocation(…)
之前,需要将其声明为全局变量或局部变量。像这样的
String location = "http://http://stackoverflow.com";
BufferedReader br = Parse.parseLocation(location, key);
String location = "http://http://stackoverflow.com";
BufferedReader br = Parse.parseLocation(location, key);