Tag: java.util.scanner

扫描仪读取大文件

我正在玩Scanner类用于学习目的,我用它来读取一个非常大的文件(60.000行aprox),而不使用Reader类,它在大约400行后停止读取。 我是否必须在Scanner的构造函数中使用Bufferedreader,否则问题是什么? 我想知道为什么会这样。 谢谢。 我的代码是输出所有行的常用代码。 File file1 = new File(“file1”); Scanner in= new Scanner(file1); while (scan.hasNextLine() ) { String str = scan.nextLine(); System.out.println(str); }

如何坚持用户输入是一个int?

这里的基本问题..我将首先要求你不要回复任何代码,因为这可能只会让我更加困惑(编程菜鸟)。 我正在寻找一个关于如何解决这个问题的明确解释。 我有一个扫描仪,可以读取用户的输入。 提示用户输入1到150之间的int值(仅限整数)。 我获得如下值: Scanner scan = new Scanner(System.in); int input = scan.nextInt(); 继续我的程序,一切正常。 不幸的是,代码并不完全是防弹的,因为任何非整数的输入都可以破坏它(字母,符号等)。 如何使代码更健壮,哪里可以validation只输入了一个int? 这些是我希望的结果: 让我们说输入是: 23 -> valid fx -> display an error message, ask the user for input again (a while loop would do..) 7w -> error, again 3.7 -> error $$ -> error etc

扫描仪不会读完整个句子

我正在编写一个程序,允许用户输入他的数据然后输出它。 它的3/4是正确的,但是当它到达输出地址时它只打印一个字,只能说’Archbishop’来自’Archbishop Street’。 我该如何解决? import java.util.*; class MyStudentDetails{ public static void main (String args[]){ Scanner s = new Scanner(System.in); System.out.println(“Enter Your Name: “); String name = s.next(); System.out.println(“Enter Your Age: “); int age = s.nextInt(); System.out.println(“Enter Your E-mail: “); String email = s.next(); System.out.println(“Enter Your Address: “); String address = s.next(); System.out.println(“Name: “+name); System.out.println(“Age: “+age); […]

读取多个扫描仪输入

我想要做的是有多个输入都有不同的变量。 每个变量都是不同方程的一部分。 我正在寻找一种方法来做到这一点,我想我有一个想法。 我只是想知道这是否合法,如果可能有更好的方法来做到这一点。 import java.util.*; public class Example{ public static void main(String args[]){ Scanner dd = new Scanner(System.in); System.out.println(“Enter number.”); int a = dd.nextInt(); System.out.println(“Enter number.”); int b = dd.nextInt(); System.out.println(“Enter number.”); int c = dd.nextInt(); } }

Java – 在扫描程序中使用多个分隔符

我正在使用扫描仪来获取输入,并希望将其拆分为块。 我希望它使用全字分隔符将其拆分。 所以现在我有: Scanner scanner = new Scanner(“1 imported bottle of perfume at 27.99”); scanner.useDelimiter(“\\sdelimitOne\\s”); 所以输入“word word delimitOne word word delimitTwo word word”我得到输出: word word word word delimitTwo word word 我希望 scanner.useDelimiter(“\\sdelimitOne\\s\\sdelimitTwo\\s”); 可能会奏效,但不是。 如何实现以下输出: word word word word word word ?

将用户的字符串输入限制为字母和数字值

基本上,我的情况要求我检查键盘用户输入定义的字符串是否仅在一种情况下是字母字符而在另一种情况下只是数字。 这是用Java编写的。 我目前的代码: switch (studentMenu) { case 1: // Change all four fields System.out.println(“Please enter in a first name: “); String firstNameIntermediate = scan.next(); firstName = firstNameIntermediate.substring(0,1).toUpperCase() + firstNameIntermediate.substring(1); System.out.println(“Please enter in a middle name”); middleName = scan.next(); System.out.println(“Please enter in a last name”); lastName = scan.next(); System.out.println(“Please enter in an eight digit student ID […]

Eclipse字符编码

我使用Scanner扫描Java中的.txt文档。 但是,当我在Eclipse中打开.txt文档时,我注意到某些字符未被识别,并且它们被替换为如下所示: 这些字符甚至不允许我扫描文件 while(scan.hasNext) 自动返回false(如果这些字符不存在,那么我可以很好地扫描文档)。 那么,我如何让Eclipse识别这些字符以便我可以扫描? 我无法手动删除它们,因为文档非常大。 谢谢。

Java:CSV文件读写

我正在阅读2个csv文件: store_inventory和new_acquisitions 。 我希望能够将store_inventory csv文件与new_acquisitions进行比较。 1)如果项目名称匹配,则只更新store_inventory中的数量。 2)如果new_acquisitions有一个store_inventory中不存在的新项目,则将其添加到store_inventory 。 这是我到目前为止所做的,但不是很好。 我添加了评论,我需要添加1和2 。 任何做上述任务的建议或代码都会很棒! 谢谢。 File new_acq = new File(“/src/test/new_acquisitions.csv”); Scanner acq_scan = null; try { acq_scan = new Scanner(new_acq); } catch (FileNotFoundException ex) { Logger.getLogger(mainpage.class.getName()).log(Level.SEVERE, null, ex); } String itemName; int quantity; Double cost; Double price; File store_inv = new File(“/src/test/store_inventory.csv”); Scanner invscan = null; try […]

Java Scanner(文件)行为不当,但Scanner(FIleInputStream)始终使用相同的文件

我对Scanner有一种奇怪的行为。 当我使用Scanner(FileInputStream)构造函数时,它将与我正在使用的一组特定文件一起使用,但它不会与Scanner(File)构造函数一起使用。 案例1: Scanner(File) Scanner s = new Scanner(new File(“file”)); while(s.hasNextLine()) { System.out.println(s.nextLine()); } 结果:没有输出 案例2: Scanner(FileInputStream) Scanner s = new Scanner(new FileInputStream(new File(“file”))); while(s.hasNextLine()) { System.out.println(s.nextLine()); } 结果:文件内容输出到控制台。 输入文件是包含单个类的java文件。 我以编程方式(在Java中)仔细检查: 该文件存在, 是可读的, 并且具有非零文件大小。 在这种情况下, Scanner(File)通常适用于我,我不知道为什么现在没有。

Java Scanner问题

如何将扫描仪的分隔符设置为; 还是新线? 我试过: Scanner.useDelimiter(Pattern.compile(“(\n)|;”)); 但它不起作用。