Java 读取字符串IP地址以存储在2D字符串数组中
我有一个带有一组IP地址的文本文件,我用BufferedReader读取它,并将其存储在2D字符串数组中 这是我的文本文件:Java 读取字符串IP地址以存储在2D字符串数组中,java,arrays,Java,Arrays,我有一个带有一组IP地址的文本文件,我用BufferedReader读取它,并将其存储在2D字符串数组中 这是我的文本文件: 102.168.212.226、104.170.214.228、0 57.68.58.212, 59.70.60.214, 1 10.42.12.22, 12.44.14.24, 2 78.16.22.234, 80.18.24.236, 3 123.168.2.2125.170.4.4,4 这是我的密码: import java.io.; import java.uti
102.168.212.226、104.170.214.228、0
57.68.58.212, 59.70.60.214, 1
10.42.12.22, 12.44.14.24, 2
78.16.22.234, 80.18.24.236, 3
123.168.2.2125.170.4.4,4
这是我的密码:
import java.io.;
import java.util.;
public class IPAddressLookup
{
IPAddressLookup() //Constructor
{
int Width = 0, Height = 0;
try
{
File fileA = new File("ClassA.txt");
BufferedReader bra = new BufferedReader(new FileReader(fileA));
String line = "";
String[] str;
while((line = bra.readLine()) != null )
{
str = line.trim().split(", ");
Width = str.length;
Height++;
}
String [][] ClassATable = new String[Height][Width];
for(int i = 0; i < Height; i++)
{
if((line = bra.readLine()) != null )
{
str = line.trim().split(", ");
for(int j = 0; j < Width; j++)
ClassATable[i][j] = str[j];
}
}
for(int i = 0; i < Height; i++)
for(int j = 0; j < Width; j++)
System.out.println(ClassATable[i][j]);
System.out.println("The text file contains:");
System.out.println("Row : " +Height);
System.out.println("Column : " +Width);
}
catch(IOException e)
{
System.out.println("Error: File not found.");
}
}
public static void main(String args[])
{
IPAddressLookup acnl1 = new IPAddressLookup();
}
}
导入java.io。;
导入java.util。;
公共类IPAddressLookup
{
IPAddressLookup()//构造函数
{
整数宽度=0,高度=0;
尝试
{
File fileA=新文件(“ClassA.txt”);
BufferedReader bra=新BufferedReader(新文件读取器(fileA));
字符串行=”;
字符串[]str;
而((line=bra.readLine())!=null)
{
str=line.trim().split(“,”);
宽度=长度;
高度++;
}
字符串[][]ClassATable=新字符串[高度][宽度];
对于(int i=0;i
问题是当我试图打印字符串数组时,它在输出中显示“null”。
还有没有办法从文件中读取字符串IP地址并将其存储在整数2D数组中
我对Java有点陌生。
有人能帮我吗?看起来您正在读取文件两次,但您没有重置读卡器,以便在第二个循环中,从顶部重新开始 您需要在循环之间添加如下内容:
bra.getChannel().position(0)
(在此之后:String[][]ClassATable=newstring[Height][Width];
)。这将重置读卡器,使其可以从顶部再次启动。请尝试以下代码:
public class IPAddressLookup {
ArrayList<String[]> ip = new ArrayList<>();
IPAddressLookup() //Constructor
{
int Width = 0, Height = 0;
try
{
File fileA = new File("ClassA.txt");
BufferedReader bra = new BufferedReader(new FileReader(fileA));
String line = "";
String[] str;
while((line = bra.readLine()) != null )
{
str = line.trim().split(", ");
ip.add(str);
Width = str.length;
Height++;
}
String [][] ClassATable = new String[Height][Width];
for(int i=0 ; i<ip.size();i++){
String[] temp = ip.get(i);
for(int j=0;j<temp.length;j++){
ClassATable[i][j] = temp[j];
}
}
for(int i = 0; i < Height; i++)
for(int j = 0; j < Width; j++)
System.out.println(ClassATable[i][j]);
System.out.println("The text file contains:");
System.out.println("Row : " +Height);
System.out.println("Column : " +Width);
}
catch(IOException e)
{
System.out.println("Error: File not found.");
}
}
public static void main(String args[])
{
IPAddressLookup acnl1 = new IPAddressLookup();
}
公共类IPAddressLookup{
ArrayList ip=新的ArrayList();
IPAddressLookup()//构造函数
{
整数宽度=0,高度=0;
尝试
{
File fileA=新文件(“ClassA.txt”);
BufferedReader bra=新BufferedReader(新文件读取器(fileA));
字符串行=”;
字符串[]str;
而((line=bra.readLine())!=null)
{
str=line.trim().split(“,”);
ip.add(str);
宽度=长度;
高度++;
}
字符串[][]ClassATable=新字符串[高度][宽度];
对于(int i=0;代码甚至不会编译的iNote(ACNLab1()
不是构造函数,因为代码中不存在该类).Btw,为什么不使用列表而不是数组,至少在第1维中是这样?这样,您就不必首先获取行数,但可以在读取行数时不断添加行数。@Thomas+1此外,您似乎违反了一些命名约定,例如变量名应该跟在方法名后面。因此第一个字母必须是小写,并且名称中的每个单独的单词必须以大写字母开头。这完全不影响代码功能,它只是使代码更易于阅读。我很抱歉。构造函数名称实际上是IPAddressLookup。很抱歉!再次编辑帖子。@fillpant,感谢您提供有关命名约定的建议。谢谢!我我来看看。你能解释一下像ip.add(str)、ip.get(i)这样的行吗?我已经创建了一组字符串数组。ip.add(str)
是在arraylist.和ip.get(i)中添加一个字符串数组,例如{102.168.212.226、104.170.214.228、0}
从arraylist获取字符串数组。非常感谢!它正在工作。很抱歉问了一个愚蠢的问题。方法名称本身解释了这一点。欢迎使用