简体   繁体   English

String.charAt()工作扫描器错误

[英]Wrong work Scanner with String.charAt()

I have a problem when I'm using Scanner in my project. 我在项目中使用扫描仪时遇到问题。 I have a file with string like that: 我有一个像这样的字符串文件:

  • Name = Jhon 名字= Jhon
  • count = 100 计数= 100
  • 1ip = 127.0.0.7 1ip = 127.0.0.7
  • end = file 结束=文件

All this string I'm adding in array and this array add in two ArrayList's. 我在数组中添加的所有此字符串,在此数组中添加了两个ArrayList。 I don't need a line witch starts with a number. 我不需要以数字开头的女巫。 Like "1ip". 就像“ 1ip”。 So I try to skip it. 所以我尝试跳过它。

And that is my code of method: 那就是我的方法代码:

    public void scan_file() throws IOException{
      Scanner sc = null;         
      String [] array_string;
      String not_sring;

      try{  
              File out = new File("file.txt");          
          sc = new Scanner(out);
          while(sc.hasNextLine()){
          not_sring=sc.nextLine();
           if(not_sring.charAt(0)>='0' && not_sring.charAt(0)<='9'){
                array_string = sc.nextLine().split("=");
            }
           else{
               array_string=sc.nextLine().split("=");
               for (int i=0; i<array_string.length; i++)
                for(int j=1; j<array_string.length; j++){
                           list_id.add(array_string[i]);
                           list_value.add(array_string[j]);     
                     }
               }
        }
      }

         catch(FileNotFoundException e) {
                 //e.printStackTrace(System.out);
                 System.out.println("File not found");
                 scan_file();
         } 
        sc.close();}

And that all isn't working. 而且这一切都不起作用。 If someone has understood my english and my Task. 如果有人了解我的英语和我的任务。

您在循环中两次调用nextLine() ,这当然是您的问题之一。

If you want to skip a line just continue to the next loop iteration with : 如果要跳过一行,请继续执行下一个循环迭代:

 if(not_sring.charAt(0)>='0' && not_sring.charAt(0)<='9'){
     continue; // This will skip to the next while iteration begining with the conditional check
 }

And if your format is id=value than use : 并且如果您的格式为id=value不是use:

    array_string=not_sring.split("="); // No need to use nextLine again as it will overwrite the current line read into not_sring
    list_id.add(array_string[0]); 
    list_value.add(array_string[1]);

This is assuming the file format is correct as described. 这是假设文件格式如上所述正确。 And there is no need for the else anymore between those blocks. 在这些模块之间不再需要else模块。

try{  
          File out = new File("file.txt");          
          sc = new Scanner(out);
          while(sc.hasNextLine()){
              not_sring=sc.nextLine();
              if(!Character.isDigit(not_sring.charAt(0))){
                array_string = not_sring.split("=");
                list_id.add(array_string[0]);
                list_value.add(array_string[1]);
              }
          }

}

Check this, you don't need for loops and you need one if block or otherwise the string will be discarded. 选中此选项,则不需要循环,如果需要阻塞,则需要一个循环,否则将丢弃该字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM