Java 有没有办法在另一个方法中使用一个方法的返回值?
我试图使用方法Java 有没有办法在另一个方法中使用一个方法的返回值?,java,methods,Java,Methods,我试图使用方法file()到方法nGram()的返回值fileName,以便将文件内容解析为n-gram。我有工作代码来做这件事,但我想有两个独立的方法 package ie.gmit.sw; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class Exampl
file()
到方法nGram()
的返回值fileName
,以便将文件内容解析为n-gram。我有工作代码来做这件事,但我想有两个独立的方法
package ie.gmit.sw;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Example {
private String fileName;
private int k;
public Example(String fileName, int k) {
this.fileName = fileName;
this.k = k;
}
public String file(String fileName) throws IOException {
//Open the file.
FileReader fr = new FileReader(fileName);
Scanner inFile = new Scanner(fr);
// Read lines from the file till end of file
while (inFile.hasNext()) {
// Read the next line.
String line = inFile.nextLine();
// Display the line.
System.out.println(line);
}
// Close the file.
inFile.close();
return fileName;
}
private void nGram() throws IOException{
List<String> ngrams = new ArrayList<>();
for (int i = 0; i <= fileName.length() - k; i++) {
ngrams.add(fileName.substring(i, i + k));
}
System.out.println(ngrams);
}
//Working
// private static void run() throws FileNotFoundException {
// // Open the file.
// FileReader fr = new FileReader(fileName);
// Scanner inFile = new Scanner(fr);
//
// // Read lines from the file till end of file
// while (inFile.hasNext()) {
// // Read the next line.
// String line = inFile.nextLine();
// // Display the line.
// System.out.println(line);
//
// List<String> ngrams = new ArrayList<>();
// for (int i = 0; i <= line.length() - k; i++) {
// ngrams.add(line.substring(i, i + k));
// }
// System.out.println(ngrams);
// }
//
// // Close the file.
// inFile.close();
// }
public static void main(String[] args) throws Exception {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter file: ");
String fileName = scanner.nextLine();
System.out.println("Enter kmers: ");
int k = scanner.nextInt();
scanner.close();
Example e = new Example(fileName, k);
e.file(fileName);
e.nGram();
}
}
要获取file()返回的值,只需在nGram参数中传递一个字符串,并在其中调用file(string)(因为file()已经返回了一个字符串)
要获取file()返回的值,只需在nGram参数中传递一个字符串,并在其中调用file(string)(因为file()已经返回了一个字符串)
解决方案:在方法文件()中调用方法nGram(line)
包ie.gmit.sw;
导入java.io.FileReader;
导入java.io.IOException;
导入java.util.ArrayList;
导入java.util.List;
导入java.util.Scanner;
公开课范例{
私有字符串文件名;
私有静态int k;
公共示例(字符串文件名,int k){
this.fileName=文件名;
这个。k=k;
}
公共静态字符串文件(字符串文件名)引发IOException{
//打开文件。
FileReader fr=新的FileReader(文件名);
扫描仪填充=新扫描仪(fr);
//从文件中读取行,直到文件结束
while(infle.hasNext()){
//读下一行。
字符串行=infle.nextLine();
//显示行。
//系统输出打印项次(行);
nGram(线路);
}
//关闭文件。
infle.close();
返回文件名;
}
私有静态void nGram(字符串j)引发IOException{
List ngrams=new ArrayList();
对于(int i=0;i解决方案:调用方法文件()中的方法nGram(line)
包ie.gmit.sw;
导入java.io.FileReader;
导入java.io.IOException;
导入java.util.ArrayList;
导入java.util.List;
导入java.util.Scanner;
公开课范例{
私有字符串文件名;
私有静态int k;
公共示例(字符串文件名,int k){
this.fileName=文件名;
这个。k=k;
}
公共静态字符串文件(字符串文件名)引发IOException{
//打开文件。
FileReader fr=新的FileReader(文件名);
扫描仪填充=新扫描仪(fr);
//从文件中读取行,直到文件结束
while(infle.hasNext()){
//读下一行。
字符串行=infle.nextLine();
//显示行。
//系统输出打印项次(行);
nGram(线路);
}
//关闭文件。
infle.close();
返回文件名;
}
私有静态void nGram(字符串j)引发IOException{
List ngrams=new ArrayList();
对于(int i=0;i首先,您需要删除静态修饰符,您会注意到存在上下文问题contexts@MarcosVasconcelos我删除了静态修饰符Removingstatic
可能并不是真的要从您的问题中删除它。我建议恢复编辑以获得完整的回答。理解为什么static
会导致问题很重要。file()方法的问题在于它读取文件sample.txt,然后返回字符串filename。您需要在file()中调用ngram()在读到的每一行中。@NomadMaker感谢您的帮助。首先,您需要删除静态修饰符,您会注意到存在上下文问题。请查看非静态与静态contexts@MarcosVasconcelos我删除了静态修饰符Removingstatic
可能并不是真的要从你的问题中删除它选项。我建议还原编辑以获得完整的答案。了解static
会导致问题的原因很重要。file()方法的问题在于它读取文件sample.txt,然后返回字符串文件名。您需要在file()中调用ngram()在读到的每一行上。@NomadMaker谢谢你的帮助
Hello world
Good Day okay
random text saying anything me laptop bye
[sa, am, mp, pl, le, e., .t, tx, xt]
private String file(String fileName){...}
private void nGram(String valueFromFile){...}
public static void main(String[] args) throws Exception {
...
Example e = new Example(fileName, k);
e.nGram(e.file(fileName));
}
package ie.gmit.sw;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Example {
private String fileName;
private static int k;
public Example(String fileName, int k) {
this.fileName = fileName;
this.k = k;
}
public static String file(String fileName) throws IOException {
//Open the file.
FileReader fr = new FileReader(fileName);
Scanner inFile = new Scanner(fr);
// Read lines from the file till end of file
while (inFile.hasNext()) {
// Read the next line.
String line = inFile.nextLine();
// Display the line.
//System.out.println(line);
nGram(line);
}
// Close the file.
inFile.close();
return fileName;
}
private static void nGram(String j) throws IOException{
List<String> ngrams = new ArrayList<>();
for (int i = 0; i <= j.length() - k; i++) {
ngrams.add(j.substring(i, i + k));
}
System.out.println(ngrams);
}
//Working
// private static void run() throws FileNotFoundException {
// // Open the file.
// FileReader fr = new FileReader(fileName);
// Scanner inFile = new Scanner(fr);
//
// // Read lines from the file till end of file
// while (inFile.hasNext()) {
// // Read the next line.
// String line = inFile.nextLine();
// // Display the line.
// System.out.println(line);
//
// List<String> ngrams = new ArrayList<>();
// for (int i = 0; i <= line.length() - k; i++) {
// ngrams.add(line.substring(i, i + k));
// }
// System.out.println(ngrams);
// }
//
// // Close the file.
// inFile.close();
// }
public static void main(String[] args) throws Exception {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter file: ");
String fileName = scanner.nextLine();
System.out.println("Enter kmers: ");
int k = scanner.nextInt();
scanner.close();
Example e = new Example(fileName, k);
file(fileName);
}
}