在C#中,您可以轻松读取给定程序集中的所有类.
我正在寻找Java中的等效功能.我需要这个来自动将EJB bean绑定到我的Guice模块.
我必须验证文件1中的单词1与文件2中的单词2的相似性,依此类推.如果单词1(文件1).equals到单词2(文件2),则文件3将是输出以显示True和False.下面是编码,但是当没有错误但没有输出时我被卡住了.我是JAVA的初学者.
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
public class test2 {
private static ArrayList<String> load(String f1) throws FileNotFoundException {
Scanner reader = new Scanner(new File(f1));
ArrayList<String> out = new ArrayList<String>();
while (reader.hasNext()) {
String temp = reader.nextLine();
String[] sts = temp.split(" ");
for (int i = 0; i < sts.length; i++) {
if (sts[i].equals("") && sts[i].equals(" ") && sts[i].equals("\n")) {
out.add(sts[i]);
}
}
}
return out;
}
private static void write(ArrayList<String> out, String fname) …Run Code Online (Sandbox Code Playgroud) Scanner scanner = new Scanner();
int number = 1;
do
{
try
{
option = scanner.nextInt();
}
catch (InputMismatchException exception)
{
System.out.println("Integers only, please.");
}
}
while (number != 0);
Run Code Online (Sandbox Code Playgroud)
尽管存在异常处理,但在给出非整数输入时,此代码将进入无限循环.它不是Scanner暂停在下一次迭代中收集输入,而是继续抛出InputMismatchExceptions直到程序被杀死.
扫描整数(或其他类型,我猜)输入,丢弃无效输入并正常继续循环的最佳方法是什么?
我正在通过O'Reillys Java Cookbook(2ed)查看一些好东西,发现Scanner.create()方法大约10次.但是在API或类声明\实现中没有这样的.例如:页面示例
我的netbeans"/ build/classes"目录中有一个.txt文件("file.txt").
在同一目录中,为以下代码编译了.class文件:
try {
File f = new File("file.txt");
Scanner sc = new Scanner(f);
}
catch (IOException e) {
System.out.println(e);
}
Run Code Online (Sandbox Code Playgroud)
调试代码("Scanner sc .."中的断点)将启动一个异常并打印以下内容:
java.io.FileNotFoundException:file.txt(系统找不到指定的文件)
我也尝试使用"/file.txt"和"//file.txt"但结果相同.
提前感谢您的任何提示
到目前为止我有这个:
public double checkValueWithin(int min, int max) {
double num;
Scanner reader = new Scanner(System.in);
num = reader.nextDouble();
while (num < min || num > max) {
System.out.print("Invalid. Re-enter number: ");
num = reader.nextDouble();
}
return num;
}
Run Code Online (Sandbox Code Playgroud)
还有这个:
public void askForMarks() {
double marks[] = new double[student];
int index = 0;
Scanner reader = new Scanner(System.in);
while (index < student) {
System.out.print("Please enter a mark (0..30): ");
marks[index] = (double) checkValueWithin(0, 30);
index++;
}
}
Run Code Online (Sandbox Code Playgroud)
当我测试这个,它不能采取双数,我得到这个消息:
Exception in thread …Run Code Online (Sandbox Code Playgroud) 我正在运行一个简单的扫描程序来解析一个字符串,但是我发现如果经常调用,我会得到OutOfMemory错误.此代码作为对象的构造函数的一部分调用,该对象是为字符串数组重复构建的:
编辑:这是更多信息的构造函数; 关于Scanner的try-catch之外没有更多的事情发生
public Header(String headerText) {
char[] charArr;
charArr = headerText.toCharArray();
// Check that all characters are printable characters
if (charArr.length > 0 && !commonMethods.isPrint(charArr)) {
throw new IllegalArgumentException(headerText);
}
// Check for header suffix
Scanner sc = new Scanner(headerText);
MatchResult res;
try {
sc.findInLine("(\\D*[a-zA-Z]+)(\\d*)(\\D*)");
res = sc.match();
} finally {
sc.close();
}
if (res.group(1) == null || res.group(1).isEmpty()) {
throw new IllegalArgumentException("Missing header keyword found"); // Empty header to store
} else {
mnemonic = res.group(1).toLowerCase(); // Store …Run Code Online (Sandbox Code Playgroud) 因为我知道更好的做法是尽可能减少代码重复,所以我决定在整个类中只声明一个扫描程序,但是我应该在哪里关闭扫描程序对象,或者它不一定要关闭它,关闭扫描程序做什么呢? .
private Scanner scanner;
/**
* Constructor for objects of class Scanner
*/
public Ssss()
{
// initialise instance variables
scanner = new Scanner(System.in);
}
public void enterYourName()
{
System.out.println("Enter your Name");
String name = scanner.nextLine();
System.out.println("Your name is:" + name);
}
public void enterYourAge()
{
System.out.println("Enter your Age");
int age = scanner.nextInt();
System.out.println("Your age is: " + age);
}
Run Code Online (Sandbox Code Playgroud) 当我Socket.getInputStream()直接使用数据(没有像Scanner这样的接口)时,它不会阻塞.但是,当我尝试使用扫描仪(类似于我们收到字符串的方式System.in)时,确实如此.我想知道这个的原因,以及连接的Socket提供给你的InputStream是如何与InputStream inin 不同的System.
用于测试的客户端(用于两个服务器)
挂起的代码:
public class Server {
public static void main(String[] args) {
try {
ServerSocket ss = new ServerSocket(15180);
Socket socket = ss.accept();
Scanner scanner = new Scanner(socket.getInputStream());
//read data from client
while(true) {
String data = scanner.nextLine();
System.out.println("Received data!");
}
}catch(IOException e) {
e.printStackTrace();
}
}
}
Run Code Online (Sandbox Code Playgroud)
不阻止的代码:
public class Server {
public static void main(String[] args) {
try {
ServerSocket ss = new ServerSocket(15180);
Socket socket = …Run Code Online (Sandbox Code Playgroud) 输入格式
读取一些未知的n行输入,stdin(System.in)直到你达到EOF; 每行输入都包含一个非空字符串.
输出格式
对于每一行,打印行号,后跟一个空格,然后输入作为输入的行内容:
样本输出
Hello world
I am a file
Read me until end-of-file.
Run Code Online (Sandbox Code Playgroud)
这是我的解决方案.问题是我无法继续进行EOF.但输出只是:
Hello world
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
public class Solution {
public static void main(String[] args) {
check(1); // call check method
}
static void check(int count) {
Scanner s = new Scanner(System.in);
if(s.hasNext() == true) {
String ns = s.nextLine();
System.out.println(count + " " + ns);
count++;
check(count);
}
}
}
Run Code Online (Sandbox Code Playgroud) java ×10
input ×3
binding ×1
exception ×1
guice ×1
inputstream ×1
memory-leaks ×1
methods ×1
sockets ×1