想要解析以下文本文件:
示例文本文件:
<2008-10-07>text entered by user<Ted Parlor><2008-11-26>additional text entered by user<Ted Parlor>
Run Code Online (Sandbox Code Playgroud)
我想解析上面的文本,以便我可以有三个变量:
v1 = 2008-10-07
v2 = text entered by user
v3 = Ted Parlor
v1 = 2008-11-26
v2 = additional text entered by user
v3 = Ted Parlor
Run Code Online (Sandbox Code Playgroud)
我试图使用扫描仪和useDelimiter,但是,我有问题如何设置它以获得如上所述的结果.这是我的第一次尝试:
import java.io.*;
import java.util.Scanner;
public class ScanNotes {
public static void main(String[] args) throws IOException {
Scanner s = null;
try {
//String regex = "(?<=\\<)([^\\>>*)(?=\\>)";
s = new Scanner(new BufferedReader(new FileReader("cur_notes.txt")));
s.useDelimiter("[<]+");
while (s.hasNext()) {
String v1 = …Run Code Online (Sandbox Code Playgroud) 我正在阅读2个csv文件:store_inventory&new_acquisitions.
我希望能够比较store_inventorycsv文件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 {
invscan = new Scanner(store_inv);
} catch (FileNotFoundException ex) {
Logger.getLogger(mainpage.class.getName()).log(Level.SEVERE, null, ex);
}
String itemNameInv;
int quantityInv; …Run Code Online (Sandbox Code Playgroud) 我想要做的是有多个输入都有不同的变量.每个变量都是不同方程的一部分.我正在寻找一种方法来做到这一点,我想我有一个想法.我只是想知道这是否合法,如果可能有更好的方法来做到这一点.
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();
}
}
Run Code Online (Sandbox Code Playgroud) 我经常使用Scanner类来读取文件,因为它非常方便.
String inputFileName;
Scanner fileScanner;
inputFileName = "input.txt";
fileScanner = new Scanner (new File(inputFileName));
Run Code Online (Sandbox Code Playgroud)
我的问题是,上面的语句是否一次将整个文件加载到内存中?或者对fileScanner进行后续调用
fileScanner.nextLine();
Run Code Online (Sandbox Code Playgroud)
从文件中读取(即从外部存储器而不是从内存中读取)?我问,因为我担心如果文件太大而无法一次性读入内存会发生什么.谢谢.
我需要从文件的内容中解析整数.
为了测试我的算法,当我从声明的字符串中提供文件的内容时
String test = "15 kuru?";
Run Code Online (Sandbox Code Playgroud)
Integer.parseInt工作正常.但是,当我从UTF-8文件中读取Scanner类时,它不起作用并给出异常
java.lang.NumberFormatException:对于输入字符串:"15"
注意:我将字符串拆分为"15"和"kuruş",因此parseInt方法仅将"15"作为参数.
示例代码:
satir = satir.trim();//15 kuru?
StringTokenizer tokenizer = new StringTokenizer(satir," ");
System.out.println(tokenizer.countTokens());//2
String s = tokenizer.nextToken();
int deger = Integer.parseInt(s);//where the exception was throwed
Run Code Online (Sandbox Code Playgroud) 我使用的是Windows 7机器,其"控制面板\时钟,语言和地区"是"丹麦"
根据Scanner的文档:
扫描程序的初始语言环境是Locale.getDefault()方法返回的值;
但是当我运行代码时:
System.out.println(Locale.getDefault());
Scanner sc = new Scanner("1.0");
sc.nextDouble();
Run Code Online (Sandbox Code Playgroud)
它输出"en_US"然后在sc.nextDouble()处抛出java.util.InputMismatchException.扫描仪初始化为"1,0"时有效
但是,如果我明确设置了Locale:
Locale.setDefault(Locale.US);
System.out.println(Locale.getDefault());
Scanner sc = new Scanner("1.0");
sc.nextDouble();
Run Code Online (Sandbox Code Playgroud)
它输出"en_US"然后解析双精度就好了.我错过了什么,或扫描仪的文档是错误的?
编辑根据@Perception的建议,我在第一个例子中查看了sc.locale().它打印"da_DK".那么为什么它不是"en_US",那么Locale.getDefault()方法返回的是什么?
我正在尝试通过ScannerObject 读取命令.检查我使用的输入语法sc.hasNext()(对于缺少命令的情况).它已经很好地适用于许多情况,但现在我已经在JavaAPI中描述为"可以阻塞并等待输入"的情况.
该hasNext()方法何时阻止,我该如何控制它?有趣的事情是它在块之前的3个案例中完美地工作.此外,JavaAPI描述hasNext()了检查是否存在另一个Input的正确方法,以便Method next()不生成Exception.
这是我到目前为止所做的代码:
if (sc.hasNext() && sc.next().equals("create")) {
if (sc.hasNextInt()) {
width = sc.nextInt();
if (width > 0) {
if (sc.hasNextInt()) {
heigth = sc.nextInt();
if (heigth > 0) {
if (sc.hasNext()) { //At this point the hasNext Statement blocks for //no reason till an Input is made.
charset = sc.next();
Image = new AsciiImage(width, heigth,charset);
} else {
ret = false;
System.out.println("INPUT MISMATCH");
}
} ...//and …Run Code Online (Sandbox Code Playgroud) 如何修改我的主类,以便我可以在不丢失空格的情况下接收值?
这是我主要课程的一部分:
StringBuilder sb = new StringBuilder();
Scanner s = new Scanner(new FileReader(new File("C:/User/waithaka/Documents//duka.json")));
while (s.hasNext()) {
sb.append(s.next());
}
s.close();
GsonBuilder gsonBuilder = new GsonBuilder();
Gson gson = gsonBuilder.create();
String jstring = sb.toString();
Result results = gson.fromJson(jstring, Result.class);
List<data> dat = response.getData();
for (data data : dat) {
System.out.println("The item is " + data.getItem());
}
Run Code Online (Sandbox Code Playgroud)
最后一行打印没有空格的值.例如,带有字符串的值"Black and yellow"将更改为"Blackandyellow".
我在尝试从txt文件中读取String和Double时遇到了一些麻烦.这是我的txt文件:
Mike 300.50
John 260
Lisa 425.33
Run Code Online (Sandbox Code Playgroud)
以下是我用来阅读它们的代码:
reader = new Scanner();
while(reader.hasNext()){
name= reader.next();
salary = reader.nextDouble();
System.out.println(name + " " + salary + "\r\n");
}
Run Code Online (Sandbox Code Playgroud)
每当我运行此代码时,Exception in thread "main" java.util.InputMismatchException似乎告诉我问题所在nextDouble().
有人知道如何解决这个问题吗?
在HACKERRANK中,这一行代码经常发生。我认为这是跳过空格,但那是什么"\r\u2028\u2029\u0085"意思
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
Run Code Online (Sandbox Code Playgroud)