所以在我的代码中,我正在尝试读取类似的文件:
100
22
123;22
123 342;432
但输出时会包含&#34 ;;" (例如,100,22,123; 22,123,342; 432})。 我试图将文件变成一个数组(例如{100,22,123,22,123 ...})。 有没有办法读取文件,但忽略分号? 谢谢!
public static void main(String args [])
{
String[] inFile = readFiles("ElevatorConfig.txt");
for ( int i = 0; i <inFile.length; i = i + 1)
{
System.out.println(inFile[i]);
}
System.out.println(Arrays.toString(inFile));
}
public static String[] readFiles(String file)
{
int ctr = 0;
try{
Scanner s1 = new Scanner(new File(file));
while (s1.hasNextLine()){
ctr = ctr + 1;
s1.next();
}
String[] words = new String[ctr];
Scanner s2 = new Scanner(new File(file));
for ( int i = 0 ; i < ctr ; i = i + 1){
words[i] = s2.next();
}
return words;
}
catch(FileNotFoundException e)
{
return null;
}
}
答案 0 :(得分:0)
public static String[] readFiles(String file)
{
int ctr = 0;
try{
Scanner s1 = new Scanner(new File(file));
while (s1.hasNextLine()){
ctr = ctr + 1;
s1.next();
}
String[] words = new String[ctr];
Scanner s2 = new Scanner(new File(file));
for ( int i = 0 ; i < ctr ; i = i + 1){
words[i] = s2.next();
}
return words;
}
catch(FileNotFoundException e)
{
return null;
}
}
将其替换为
public static String [] readFiles(String file){
List<String> retList = new ArrayList<String>();
Scanner s2 = new Scanner(new File(file));
for ( int i = 0 ; i < ctr ; i = i + 1){
String temp = s2.next();
String[] tempArr = se.split(";");
for(int k=0;k<tempArr.length;k++) {
retList.add(tempArr[k]);
}
}
return (String[]) retList.toArray();
}
答案 1 :(得分:0)
使用正则表达式。将整个文件读入String (将每个标记作为字符串读取,并在字符串中的每个标记后面附加空格),然后将其拆分为空格和半冒号。
String x <--- contains all contents of the file
String[] words = x.split("[\\s\\;]+");
words[]
的内容是:
"100", "22", "123", "22", "123", "342", "432"
在使用数字之前,请务必将它们解析为int
。
答案 2 :(得分:0)
使用BufferedReader的简单方法逐行读取然后拆分;
public static String[] readFiles(String file)
{
BufferedReader br = new BufferedReader(new FileReader(file)))
StringBuilder sb = new StringBuilder();
String line = br.readLine();
while (line != null) {
sb.append(line);
sb.append(System.lineSeparator());
line = br.readLine();
}
String allfilestring = sb.toString();
String[] array = allfilestring.split(";");
return array;
}
答案 3 :(得分:0)
您可以使用<?php
$con=mysqli_connect("localhost","root","mypassword","myDB");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
echo "<h3 style='color: darkgreen;'> Connected to database. </h3> <br />";
$result = mysqli_query($con,"SELECT * FROM Inventory");
echo "<table border='1'>
<tr>
<th>Item ID</th>
<th>Item Name</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td class='item-id'>" . $row['itemID'] . "</td>";
echo "<td class='item-name'>" . $row['itemName'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
<script src="https://code.jquery.com/jquery-1.12.4.min.js" integrity="sha256-ZosEbRLbNQzLpnKIkEdrPv7lOy9C27hHQ+Xp8a4MxAQ=" crossorigin="anonymous"></script>
<script type="text/javascript">
$("tr").click(function() {
var id = $(this).find('.item-id').text();
var name = $(this).find('.item-name').text();
alert("Your data is: " + $.trim(id) + " , " + $.trim(name));
});
</script>
根据您的要求使用正则表达式将字符串拆分为数组。
split()
希望有所帮助
答案 4 :(得分:0)
保留用于计算数组大小的代码。 我只想改变你输入你的价值观的方式。
for (int i = 0; i < ctr; i++) {
words[i] = "" + s1.nextInt();
}
答案 5 :(得分:0)
另一个选项是用空格替换整个文件字符串中的所有非数字字符。这样就可以忽略任何非数字字符。
BufferedReader br = new BufferedReader(new FileReader(file)))
StringBuilder sb = new StringBuilder();
String line = br.readLine();
while (line != null) {
sb.append(line);
line = br.readLine();
}
String str = sb.toString();
str = str.replaceAll("\\D+"," ");
现在你有一个字符串,数字用空格分隔,我们可以将它们标记为数字字符串。
String[] final = str.split("\\s+");
然后转换为int数据类型。