我从.txt文件中获取输入并将其存储到ArrayList中。我需要摆脱所有空格,以便将void btnEvent_Click(object sender, EventArgs e)
{
var button = sender as Button;
//Searching...
int x,y;
for(x = 0; x < btn.GetLength(0); x++)
{
bool found = false;
for(y = 0; y < btn.GetLength(1); y++)
{
if(btn[x,y] == button ) {
found = true;
break;
}
}
if(found) break;
}
//Now x,y have the correct coordinates.
}
解析为ArrayList
。我将发布注释掉的部分,以便您可以看到我尝试使用它来执行此操作。谢谢。如果你能给我一个看起来很棒的地方。
<Integer>
这是我正在阅读的.txt文件,import java.io.*;
import java.util.*;
public class LabWriterReadre {
public static void main(String [] args){
BufferedReader br = null;
BufferedWriter bw = null;
// fileWriter = new FileWriter("output.txt",false);
ArrayList<String> storage = new ArrayList<String>();
ArrayList<String> convertToInt = new ArrayList<String>();
ArrayList<Integer> matrix = new ArrayList<Integer>();
String strLine = "";
int row1,col1,row2,col2;
try {
br = new BufferedReader( new FileReader("C:/Users/jeremiahlukus/Desktop/New folder/jj.txt"));
while( (strLine = br.readLine()) != null)
{
System.out.println(strLine);
storage.add(strLine);
}
} catch (FileNotFoundException e) {
System.err.println("Unable to find the file: fileName");
} catch (IOException e) {
System.err.println("Unable to read the file: fileName");
}
/*
String[] trimmedArray = new String[string.size()];
for (int i = 0; i < string.size(); i++)
{
trimmedArray[i] = string[i].trim();
string.removeAll(Arrays.asList(null,""));
}
*/
//string.removeAll(Collections.singleton(""));
// string.removeAll(Arrays.asList(null,"")
for (String str : storage)
{
if (//str != " ")
!str.isEmpty())
{
convertToInt.add(str);
//convertToInt.trim();
}
}
System.out.println(convertToInt);
}
/*
row1 = Integer.parseInt(convertToInt.get(0));
col1 = Integer.parseInt(convertToInt.get(1));
row2 = Integer.parseInt(convertToInt.get(2));
col2 = Integer.parseInt(convertToInt.get(3));
int[][] matrix1=new int[row1][col1];
int[][] matrix2=new int[row2][col2];
*/
// System.out.println(row1);
/*
表示空格
_
这是打印出的ArrayList
3___________3
3_4
1 ______2 3
4_5 _6
7 _8 _9
1 _2_ 3_ 4
5 _6 _7 _8
9_ 10_ 11_ 12
这就是我想要发生的事情
[3______3,_3 _4,__ 1_______2_ 3 , ___4 _5 _6, 7_ 8 _9, 1_ 2_ 3_ 4, 5 _6_ 7_ 8, 9 _10_ 11_ 12]
答案 0 :(得分:1)
添加线时,您可以直接替换(&#34;&#34;,&#34;&#34;)。此外,这个代码可以更有效。
答案 1 :(得分:1)
每一行都可以使用像这样的正则表达式进行解析
List<String> sequence = new ArrayList<String>();
Patter noSpacesPattern = Pattern.compile("(\\S+)");
Matcher matches;
// reading your line from file here, now for each line do this
matches = noSpacesPattern.matcher(strLine);
while (matches.find())
sequence.add(matches.group());
这将消除不必要的空间并使代码保持可读性。
答案 2 :(得分:0)
尝试在数组中读取文件时使用replaceAll()
删除所有空格。
while( (strLine = br.readLine()) != null)
{
System.out.println(strLine);
storage.add(strLine.replaceAll("\\s+",""));
}
答案 3 :(得分:0)
我可能错了,但看起来你的代码可以用
之类的东西来解决和简化Scanner input = new Scanner(new File("C:/Users/jeremiahlukus/Desktop/New folder/jj.txt"));
List<Integer> numbers = new ArrayList<>();
while(input.hasNextInt()){
numbers.add(input.nextInt());
}
我在这里假设你的文件只包含用空格分隔的数字(也包括行分隔符)。
答案 4 :(得分:0)
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Test {
public static void main(String[] args)
{
String total="";
List list=new ArrayList();
list.add("3______3");
list.add("_3 _4");
list.add("__ 1_______2_ 3");
list.add("3 ");
list.add(" ___4 _5 _6");
list.add(" 7_ 8 _9");
list.add(" 1_ 2_ 3_ 4");
list.add(" 5 _6_ 7_ 8");
list.add(" 9 _10_ 11_ 12");
String firstname1 = list.toString();
String regex = "[0-9]+";
firstname1 = firstname1.replaceAll("_", ",");
firstname1 = firstname1.replace(",", ",");
String str = firstname1.toString().replaceAll("\\s+", ",").replace("[", "").replace("]", "");
String arr[] = str.split(",");
for (int i = 0; i < arr.length; i++) {
if (arr[i].matches(regex)) {
total += arr[i] + ",";
}
}
total = total.substring(0, total.length() - 1);
List finalList = Arrays.asList(total);
System.out.println(finalList);
//output-->> [3,3,3,4,1,2,3,3,4,5,6,7,8,9,1,2,3,4,5,6,7,8,9,10,11,12]
}
}
答案 5 :(得分:0)
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Test {
public static void main(String[] args) throws FileNotFoundException, IOException
{
String total="";
List list=new ArrayList();
try {
BufferedReader br = new BufferedReader( new FileReader("filename.txt"));
while( (total = br.readLine()) != null)
{
list.add(total);
}
br.close();
} catch (FileNotFoundException e) {
System.err.println("Unable to find the file: fileName");
}
String firstname1 = list.toString();
String regex = "[0-9]+";
firstname1 = firstname1.replaceAll("_", ",");
firstname1 = firstname1.replace(",", ",");
String str = firstname1.toString().replaceAll("\\s+", ",").replace("[", "").replace("]", "");
String arr[] = str.split(",");
String sum="";
for (int i = 0; i < arr.length; i++) {
if (arr[i].matches(regex)) {
sum += arr[i] + ",";
}
}
sum = sum.substring(0, sum.length() - 1);
List finalList = Arrays.asList(sum);
System.out.println("finallist-->>>"+finalList);
//output-->> [3,3,3,4,1,2,3,3,4,5,6,7,8,9,1,2,3,4,5,6,7,8,9,10,11,12]
}
}