我经常遇到这个问题.当我多次使用扫描仪时,它不会从用户那里获得输入.
Scanner scan = new Scanner(System.in);
System.out.println("1---");
int try1 = scan.nextInt();
System.out.println("2---");
int try2 = scan.nextInt();
System.out.println("3---");
String try3 = scan.nextLine();
System.out.println("4---");
String try4 = scan.nextLine();
Run Code Online (Sandbox Code Playgroud)
当我运行此代码时,结果:
1 ---
12
2 ---
321
3 ---
4 ---
AA
如您所见,它跳过了第3个输入.为什么会这样?我使用新的扫描仪解决了这个问题,有时我有5-6种不同的扫描仪,看起来很复杂.另一个问题是:存在错误"资源泄漏:扫描永远不会关闭".我真的很困惑.
我知道我在这里冒险,但我似乎无法理解为什么我们不能只创建 Scanner 类的实例两次。我会添加一个例子以防万一。
import java.util.Scanner;
public class Nope
{
public static void main(String[] args)
{
System.out.println("What's your name?");
Scanner scanner = new Scanner(System.in);
String name = scanner.nextLine();
System.out.println("Welcome " + name + "!");
scanner.close();
// Now
System.out.println("where you do live?");
Scanner sc = new Scanner(System.in);
String country = sc.nextLine();
System.out.println("That's a lovely place");
sc.close();
}
}
Run Code Online (Sandbox Code Playgroud)
我得到一个运行时错误,看起来像这样
What's your name?
Kate
Welcome Kate!
Exception in thread "main" where you do live?
java.util.NoSuchElementException: No line found
at java.base/java.util.Scanner.nextLine(Scanner.java:1651)
at Nope.main(Nope.java:17)
Run Code Online (Sandbox Code Playgroud)
我知道再次创建同一个类的新对象是没有意义的,鼓励冗余。但我只是认为如果我知道为什么,我的头脑就会清醒,你不也这么认为吗? …
我在java中遇到了一个异常,我无法解决这个问题.
在我的主要课程中,我有以下代码:
(import java.util.*;)
...
Scanner input = new Scanner(System.in);
String name;
int i = 1;
System.out.println("How many names do you want to enter?");
int iteratorCount = input.nextInt();
iteratorCount++;
while (i < iteratorCount){
System.out.println("Ener name number "+i);
name = input.nextLine();
System.out.println(name);
i++;
}
Run Code Online (Sandbox Code Playgroud)
但是在while循环的第一次出现期间,我不会被要求写一个名字.但是,确实显示"输入名称编号1"文本.这是输出:
How many names do you want to enter?
>>5
Ener name number 1
Ener name number 2
>>Jack
Jack
Ener name number 3
>>John
John
Ener name number 4
>>Lisa
Lisa
Ener name number 5
>>Paul …Run Code Online (Sandbox Code Playgroud) 编辑:我的问题已经部分修复.现在我可以输入文本,但输入'shiphalf'值后没有任何内容.我是否错误地构造了if else语句?
我试图让人们输入优惠券代码,但我无法弄清楚如何让控制台知道用户正在输入文本.我觉得错误在这里的某个地方:
System.out.println("Enter a Coupon Code: ");
String ship = input.nextLine();
Run Code Online (Sandbox Code Playgroud)
但我不完全确定.以下是完整的源代码:
package pseudoPackage;
import java.util.Scanner;
public class PseudoCode {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int ship1 = 0;
int ship2 = 6;
int ship3 = 2;
String shiphalf = null;
System.out.print("How much will shipping cost you?");
System.out.println();
System.out.print("Enter your textbook cost in dollars without the $: ");
double cost = input.nextDouble();
if ( cost >= 100 ) {
System.out.println("Your shipping costs …Run Code Online (Sandbox Code Playgroud) import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String s=new String();
int x=sc.nextInt();
double y=sc.nextDouble();
s = sc.next();
System.out.println("String:"+s);
System.out.println("Double: "+y);
System.out.println("Int: "+x);
}
}
Run Code Online (Sandbox Code Playgroud)
它只扫描一个单词,请提出任何建议......
Scanner i=new Scanner(System.in);
System.out.println("Enter an integer: ");
int in=i.nextInt();
System.out.println("Enter an floating point number: ");
double d=i.nextDouble();
System.out.println("Enter a string: ");
String str=i.next();
System.out.printf("%s%n,Sum of%2d and %.2f is %.2f%n",str,in ,d,in+d);
Run Code Online (Sandbox Code Playgroud)
我的问题是格式化通过扫描仪输入的字符串。我试图输入“Result is”,但 printf() 似乎只看到字符串的“Result”部分,那么空格的命令是什么?谢谢
到目前为止,我一直在使用Scanners 从文本文件中读取数据。例子:
File file = new File("path\\to\\file") ;
Scanner scan = new Scanner(file) ;
System.out.println(scan.nextLine()) ;
Run Code Online (Sandbox Code Playgroud)
并使用FileWriters 将数据写入文本文件。像这样:
try
{
FileWriter writer = new FileWriter("Foo.txt") ;
writer.write("hello there!") ;
writer.close()
}
catch(IOException ex)
{
ex.printStackTrace() ;
}
Run Code Online (Sandbox Code Playgroud)
几天前,我和我的导师开会。当我检查他的代码,我注意到他曾使用BufferedReader和BufferedWriter-的读取和写入文件,我以前没有使用过的方法。然后我问他使用 aBufferedReader和 aScanner从文件中读取数据有什么区别。他无法向我解释。
所以我做了一些研究,发现隐藏在引擎盖下执行这些操作的类是InputStream和OutputStream。这些类都有各自喜欢的子类FileInputStream,FileOutputStream等等。
在我的进一步研究中,我遇到了用于从文件读取数据和将数据写入文件的Reader和Writer类。同样,与InputStreamand 一样OutputStream,这些类都是abstract super类,并且有自己的子类来执行读写操作。
我对此并不感到困惑,但是......为什么?我的意思是,为什么有不同的方法来做同样的事情?有什么意义?哪种方法是处理文件输入和输出的最有效方法?
我试着做一个学生注册计划.所以我也写了一个类文件.使用该类文件,我将值放到ArrayList中.但问题是,如果我输入有关3名学生的详细信息,最后它只会打印最后一名学生的详细信息两次.我是个乞丐,请帮我解决这个问题.
这是我写的主要方法;
Student obj = new Student();
Scanner in = new Scanner(System.in);
ArrayList<Student> List = new ArrayList<Student>();
for (;;) {
System.out.print("Enter the Student ID: ");
obj.setStudentID(in.next());
System.out.print("Enter the first name: ");
obj.setFirstName(in.next());
System.out.print("Enter the last name: ");
obj.setLastName(in.next());
System.out.print("Enter the Course Work 1 marks: ");
obj.setCwk01(in.nextInt());
System.out.print("Enter the Course Work 2 marks: ");
obj.setCwk02(in.nextInt());
System.out.print("Enter the final exam marks: ");
obj.setExam(in.nextInt());
List.add(obj);
System.out.print("Enter q to stop (or any other to continue): ");
String str = in.next();
if (str.equals("q") || str.equals("Q")) …Run Code Online (Sandbox Code Playgroud) 我正在尝试制作一个"for"循环,它要求用户输入10个数字,然后只打印正数.
无法控制输入量.在我添加负数之前,我一直在获得无限输入.
import java.util.Scanner;
public class ej1 {
public static void main(String args[]) {
int x;
for (x = 1; x >= 0; ) {
Scanner input = new Scanner(System.in);
System.out.print("Type a number: ");
x = input.nextInt();
}
}
}
Run Code Online (Sandbox Code Playgroud) 我有以下代码,称之为代码#1
Scanner keyboard = new Scanner(System.in);
String s = keyboard.nextLine();
int x = keyboard.nextInt();
double y = keyboard.nextDouble();
System.out.println("String: "+s);
System.out.println("Double: "+y);
System.out.println("Int: "+x);
Run Code Online (Sandbox Code Playgroud)
如果我输入
Hello World
12.1
12
Run Code Online (Sandbox Code Playgroud)
输出将是
String: Hello World
Double: 12.1
Int: 12
Run Code Online (Sandbox Code Playgroud)
但是,如果我重新排列我的代码,请将其称为 code#2,如
Scanner keyboard = new Scanner(System.in);
int x = keyboard.nextInt();
double y = keyboard.nextDouble();
String s = keyboard.nextLine();
System.out.println("String: "+s);
System.out.println("Double: "+y);
System.out.println("Int: "+x);
Run Code Online (Sandbox Code Playgroud)
我输入
12
12.1
Run Code Online (Sandbox Code Playgroud)
编译器跳过字符串输入和输出
String:
Double: 12.1
Int: 12
Run Code Online (Sandbox Code Playgroud)
这对我来说很奇怪。我被告知编译器总是从上到下阅读。我想象编译器将 code#2 读取为
int x = keyboard.nextInt();
Run Code Online (Sandbox Code Playgroud)
首先等待用户输入一个整数,以便将其分配给 x。然后读 …