我在使用以下代碼時遇到問題。
public class ReadFromFile {
public static void main(String[] args) {
Scanner scan=null;
try {
scan=new Scanner(new File("AllAccInfo.txt"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
String input=scan.nextLine();
while(input!=null) {
String[] data=input.split("[\\: \t]");
try {
input=scan.nextLine();
}
catch(NoSuchElementException e) {
break;
}
}
}
}
這是我的文本檔案
SavingAccount: Sourav Roy Komol 25478963 79863 77010.0 5000.0 45000.0
CurrentAccount: Shami Kaiser 1234789 22167 88000.0 5000.0
代碼的輸出是
Roy
Kaiser
我想將整個名稱選擇Sourav Roy Komol為Shami Kaisar一個字串變數。這樣我的第一個程式第一個讀取第一行并選擇“Sourav Roy Komol”將選擇“String x”。與“Shami Kaisar”相同的程序。如何在忽略空格的情況下選擇這些值?這里 1234789 和 22167 也是一個字串變數。
uj5u.com熱心網友回復:
由于輸入資料是制表符分隔的(我復制并粘貼了您的輸入資料,其中每個欄位由制表符分隔),您可以使用StringTokenizer. 這是一個簡單的示例,展示了它是如何作業的,使用輸入示例的第一行,并在運行時顯示輸出。
String one = "SavingAccount: Sourav Roy Komol\t25478963\t79863\t77010.0\t5000.0\t45000.0";
StringTokenizer tokenizer = new StringTokenizer(one, "\t");
while (tokenizer.hasMoreTokens()) {
System.out.println("token: " tokenizer.nextToken());
}
這里的輸出:
token: SavingAccount: Sourav Roy Komol
token: 25478963
token: 79863
token: 77010.0
token: 5000.0
token: 45000.0
這應該足以讓您繼續作業。您可能希望從第一個標記中排除“SavingAccount:”部分,只留下名稱(“Sourav Roy Komol”)。有幾個選項可以讓你做到這一點,但這里的代碼應該足以解決你的決議問題。
uj5u.com熱心網友回復:
由于沒有完整的代碼,所以我假設您正在尋找拆分字串,以便名稱將顯示為一個字串。
在這種情況下,您可以在 split & later trim() 中使用以下正則運算式來洗掉尾隨空格:
String[] data=input.split("[\\^0-9]");
String name = data[0].trim(); //this will trim trailing whitespace of your returned string.
如果您name在控制臺中列印 String,您將看到洗掉了尾隨空格的全名。如果您想保留尾隨空格,您可以忽略trim()對回傳字串的操作。
此建議假定您的輸入檔案將始終具有名稱。
uj5u.com熱心網友回復:
您的正則運算式只會拆分您指定的三個字符之一。您應該做的是使用捕獲組以冒號或一組空格后跟一個或多個數字分隔。這將為您提供:冒號前的第一個字串,名稱,然后是每個數字。
public class Main {
public static void main(String[] args) {
Scanner scan = null;
try {
scan = new Scanner(new File("AllAccInfo.txt"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
String[] data;
String input = scan.nextLine();
while (input != null) {
data = input.split("(:|\\s (?=\\d ))");
try {
System.out.println(data[1].trim());
input = scan.nextLine();
} catch (NoSuchElementException e) {
break;
}
}
}
}
輸出
Sourav Roy Komol
Shami Kaiser
uj5u.com熱心網友回復:
方法 1 假設您有一個計劃 txt 作為您的資料庫,并且知道如何從 txt 檔案中只獲取名稱。然后這樣做
- 用下劃線替換空格
st = st.replaceAll("\\s ","_").這讓所有名稱都有一個像這樣的下劃線 Sourav_Roy_Komol - 然后在將其渲染到螢屏時執行此操作
st = st.replaceAll("_"," "),這會將下劃線替換為空格。
方法二 將 split("[\: \t]") 替換為 split("[\s ]");
希望夠清楚嗎?
根據您對 JSON 方法的請求:見下文 第 1 步:添加依賴項
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>
第 2 步:匯入庫
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
第 3 步:創建 JsonObject。
JSONObject employeeDetails1 = new JSONObject();
employeeDetails.put("AccountType", "SavingAccount");
employeeDetails.put("Fullname", "Sourav Roy Komol");
employeeDetails.put("AccountNumber", "25478963");
JSONObject employeeObject = new JSONObject();
employeeObject.put("employee", employeeDetails1);
為其他用戶靜態重復上述代碼或使用回圈填充它們。第 4 步:將 employeesObj 添加到串列
JSONArray employeeList = new JSONArray();
employeeList.add(employeeObject);
第 5 步:寫入檔案
try (FileWriter file = new FileWriter("employees.json")) {
//We can write any JSONArray or JSONObject instance to the file
file.write(employeeList.toJSONString());
file.flush();
} catch (IOException e) {
e.printStackTrace();
}
現在!!。上面的代碼僅以 JSON FORMAT 將資料寫入檔案 (employees.json)。 讀取資料
第 6 步:
//JSON決議器物件決議讀取檔案
JSONParser jsonParser = new JSONParser();
try (FileReader reader = new FileReader("employees.json"))
{
//Read JSON file
Object obj = jsonParser.parse(reader);
JSONArray employeeList = (JSONArray) obj;
System.out.println(employeeList);//you can delete this line
//Iterate over employee array
employeeList.forEach( emp -> parseEmployeeObject( (JSONObject) emp ) );
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
第 7 步:創建決議物件的函式
private static void parseEmployeeObject(JSONObject employee)
{
//Get employee object in list
JSONObject employeeObject = (JSONObject) employee.get("employee");
//Get employee fullname
String fullname= (String) employeeObject.get("fullname");
System.out.println(fullname);
//here you can format the output......
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/482186.html
上一篇:為什么我的變數在javascript中未定義?[復制]
下一篇:陣列中所有物件的1個物件的值變化
