使用java将文本文件修剪到第一列

使用java将文本文件修剪到第一列,java,string,char,substring,trim,Java,String,Char,Substring,Trim,嗯,我有一张IP的清单 193.137.150.5 368 ms DANIEL-PC 193.137.150.7 95 ms N/A 193.137.150.13 37 ms N/A 193.137.150.17

嗯,我有一张IP的清单

193.137.150.5              368 ms                DANIEL-PC             
193.137.150.7              95 ms                 N/A                   
193.137.150.13             37 ms                 N/A                   
193.137.150.17             33 ms                 N/A                   
193.137.150.24             238 ms                N/A                   
193.137.150.38             74 ms                 DUARTE-PC             
193.137.150.41             26 ms                 N/A                   
193.137.150.52             176 ms                N/A   
我想用java只从列表中删除IP

这是我的密码:

import java.io.*;
import java.util.*;

class trim{

    public static void main(String[] args) throws Exception{
        String s;
        char c;
        int i = 0;
        Scanner in = new Scanner(System.in);

        while (in.hasNextLine()){
            s = in.nextLine();
            c = s.charAt(i);
            while (c != ' '){
                System.out.print(c);
                i++;
                c = s.charAt(i);
            }
            System.out.println();
        }
    }
}

我做错了什么?

在你的循环中,你永远不会将
I
归零。这意味着在第一行之后,每行的偏移量都是错误的

    while (in.hasNextLine()){
        s = in.nextLine();
        c = s.charAt(i);
        while (c != ' '){
            System.out.print(c);
            i++;
            c = s.charAt(i);
        }
        System.out.println();
        i = 0; // Finished with this line
    }

在您的循环中,您永远不会将
i
归零。这意味着在第一行之后,每行的偏移量都是错误的

    while (in.hasNextLine()){
        s = in.nextLine();
        c = s.charAt(i);
        while (c != ' '){
            System.out.print(c);
            i++;
            c = s.charAt(i);
        }
        System.out.println();
        i = 0; // Finished with this line
    }

您可以简单地拆分字符串一次,然后打印第二部分

while (in.hasNextLine()){
    s = in.nextLine();
    System.out.println(s.split(" ", 1)[1]);
}

您可以简单地拆分字符串一次,然后打印第二部分

while (in.hasNextLine()){
    s = in.nextLine();
    System.out.println(s.split(" ", 1)[1]);
}

你不是在重新初始化我

    while (in.hasNextLine()){
        s = in.nextLine();
        // reset i so you start reading at line begin
        i = 0;
        c = s.charAt(i);
        while (c != ' '){
            System.out.print(c);
            i++;
            c = s.charAt(i);
        }
        System.out.println();
    }

也就是说,有更简单的方法,例如使用
String.split()
您不需要重新初始化i

    while (in.hasNextLine()){
        s = in.nextLine();
        // reset i so you start reading at line begin
        i = 0;
        c = s.charAt(i);
        while (c != ' '){
            System.out.print(c);
            i++;
            c = s.charAt(i);
        }
        System.out.println();
    }
也就是说,有更简单的方法,例如使用
String.split()
您需要重新设置i=0。 你可以在下一个电话后再做

    s = in.nextLine();
    i = 0;  
您需要重新设置i=0。 你可以在下一个电话后再做

    s = in.nextLine();
    i = 0;  

你们有什么产出?你们有什么产出?非常感谢。事实上,我记得这么做了,但在两者之间的某个地方完全忘记了。非常感谢。事实上,我记得这样做,但在两者之间的某个地方完全忘记了。