Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/390.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JAVA当文件中的字符串打印在屏幕上时,null也会在末尾与它一起打印_Java - Fatal编程技术网

JAVA当文件中的字符串打印在屏幕上时,null也会在末尾与它一起打印

JAVA当文件中的字符串打印在屏幕上时,null也会在末尾与它一起打印,java,Java,所以我的程序正在运行…但只有一个问题…null也正在与文件中的内容或字符串一起打印。。。是否有任何方法可以阻止null打印。读取的代码要求用户提供文件名.txt,然后要求用户输入他/她想要搜索的字符,程序在字符串中搜索指定的字符,打印文本文件的内容,并告诉用户找到该文件的次数。问题是在它打印出文本文件(字符串)的内容之后,它还打印出单词null,但是null不在txt文件中 import java.util.Scanner; import java.io.*; public class fil

所以我的程序正在运行…但只有一个问题…null也正在与文件中的内容或字符串一起打印。。。是否有任何方法可以阻止null打印。读取的代码要求用户提供文件名.txt,然后要求用户输入他/她想要搜索的字符,程序在字符串中搜索指定的字符,打印文本文件的内容,并告诉用户找到该文件的次数。问题是在它打印出文本文件(字符串)的内容之后,它还打印出单词null,但是null不在txt文件中

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

public class filefinder
 {
   public static void main(String[] args) throws IOException
   {


         //needed for scanner class
          Scanner kb = new Scanner(System.in);

          int charCount = 0;
            String filename = "";
            String str = "";
            String line = "";
            boolean isString = false;
            boolean fileFound = false;
            FileReader freader;

          // get users string
            while (!fileFound)
            {
          while (!isString)
            {   
            try { Thread.currentThread().sleep(500); }
                catch ( Exception e ) { }

            System.out.println("Please enter a filename: ");
            System.out.println("");
          filename = kb.nextLine();
            if (filename != null)
                   isString = true;
           }//end inner while loop

            // open file
            try 
            {
                freader = new FileReader(filename);     

            fileFound = true;


            Thread.currentThread().sleep(1500);
            }
            catch (Exception e)
            {

            System.out.println("");
            System.out.println("The system cannot find the file specified.");
            System.out.println("");
            isString = false;


            }//end try-catch
            }//end outer while loop

            try { Thread.currentThread().sleep(1000); }
          catch ( Exception e ) { }

           freader = new FileReader(filename);
            BufferedReader inputFile = new BufferedReader(freader);

            // Read first line from file
            while (line !=null)
            {

            line = inputFile.readLine();

            if (line != null)


            try { Thread.currentThread().sleep(1000); }
          catch ( Exception e ) { }

                 str = str+ "\n" + line; 
            }//end while

            System.out.println(str);

            inputFile.close();

          // get users character
            System.out.println("");
          System.out.println("Please enter a character you want to find: ");
            System.out.println("");
          char userChar = kb.nextLine().charAt(0);






                    while(str.length()>0){
                    for(int i= 0;i<str.length();i++){
                                if(str.charAt(i)==userChar)
                        charCount++;

                }


          System.out.println("\n The entered character " +"\"" + userChar +
                              "\"  inside  " + filename +
                              "was found " + charCount +   " times.\n");         
                                      break;  

        }
    }
    }
import java.util.Scanner;
导入java.io.*;
公共类文件查找器
{
公共静态void main(字符串[]args)引发IOException
{
//扫描器类需要
扫描仪kb=新扫描仪(System.in);
int charCount=0;
字符串filename=“”;
字符串str=“”;
字符串行=”;
布尔isString=false;
布尔fileFound=false;
文件阅读器;
//获取用户字符串
而(!filefind)
{
而(!isString)
{   
试试{Thread.currentThread().sleep(500);}
捕获(例外e){}
System.out.println(“请输入文件名:”);
System.out.println(“”);
filename=kb.nextLine();
如果(文件名!=null)
isString=true;
}//结束内部while循环
//打开文件
尝试
{
freader=新文件读取器(文件名);
fileFound=true;
Thread.currentThread().sleep(1500);
}
捕获(例外e)
{
System.out.println(“”);
System.out.println(“系统找不到指定的文件。”);
System.out.println(“”);
isString=false;
}//结束试接
}//结束外while循环
试试{Thread.currentThread().sleep(1000);}
捕获(例外e){}
freader=新文件读取器(文件名);
BufferedReader inputFile=新的BufferedReader(freader);
//从文件中读取第一行
while(行!=null)
{
line=inputFile.readLine();
如果(行!=null)
试试{Thread.currentThread().sleep(1000);}
捕获(例外e){}
str=str+“\n”+行;
}//结束时
系统输出打印项次(str);
inputFile.close();
//获取用户角色
System.out.println(“”);
System.out.println(“请输入要查找的字符:”);
System.out.println(“”);
char userChar=kb.nextLine().charAt(0);
而(str.length()>0){
对于(int i=0;i基本循环为

String str = "";
while (line != null) {
    line = inputFile.readLine();
    str = str + "\n" + line;
}// end while

System.out.println(str);
这将读取所有行,直到最后一行。但是由于文件结尾的检查(即
行==null
)是在while条件内进行的,因此
null
行也将附加到
str
变量。

基本循环是

String str = "";
while (line != null) {
    line = inputFile.readLine();
    str = str + "\n" + line;
}// end while

System.out.println(str);
这将读取所有行,直到最后一行。但由于文件结尾检查(即,
line==null
)是在while条件内进行的,因此
null
行也将附加到
str
变量中。

这就是问题所在:

  while (line !=null)
  {

  line = inputFile.readLine();

  if (line != null)


  try { Thread.currentThread().sleep(1000); }
catch ( Exception e ) { }

       str = str+ "\n" + line; 
  }
很难准确说出你认为这意味着什么,因为压痕很差,但它相当于:

while (line !=null)
{  
    line = inputFile.readLine();
    if (line != null)
    {
        try { Thread.currentThread().sleep(1000); }
        catch ( Exception e ) { }
    }
    str = str+ "\n" + line; 
}
换句话说,不管它是否为null,您仍然在循环中连接
;如果它为null,您就不会睡觉。我怀疑您希望它是这样的:

while (line !=null)
{  
    line = inputFile.readLine();
    if (line != null)
    {
        try { Thread.currentThread().sleep(1000); }
        catch ( Exception e ) { }
        str = str+ "\n" + line; 
    }
}
我强烈建议:

  • 您可以让IDE为您执行缩进,这样您就可以清楚地看到
    if
    块等所包含的内容
  • 你总是在while/if/etc中使用大括号,以使事情更清楚
  • 您无法捕获裸露的
    异常
    (捕获更具体的异常)
  • 在不记录异常的情况下,不能接受异常
  • 没有什么特别的原因,你一秒钟也睡不着
  • 在循环()中不使用字符串连接
  • 您可以将代码分解为更小的方法
  • 您可以在第一次使用时声明局部变量,而不是在方法的顶部声明所有变量
  • 您可以关闭
    finally
    块中的流和其他类似资源,以避免在引发异常时泄漏资源
    • 这就是问题所在:

        while (line !=null)
        {
      
        line = inputFile.readLine();
      
        if (line != null)
      
      
        try { Thread.currentThread().sleep(1000); }
      catch ( Exception e ) { }
      
             str = str+ "\n" + line; 
        }
      
      很难准确说出你认为这意味着什么,因为压痕很差,但它相当于:

      while (line !=null)
      {  
          line = inputFile.readLine();
          if (line != null)
          {
              try { Thread.currentThread().sleep(1000); }
              catch ( Exception e ) { }
          }
          str = str+ "\n" + line; 
      }
      
      换句话说,不管它是否为null,您仍然在循环中连接
      ;如果它为null,您就不会睡觉。我怀疑您希望它是这样的:

      while (line !=null)
      {  
          line = inputFile.readLine();
          if (line != null)
          {
              try { Thread.currentThread().sleep(1000); }
              catch ( Exception e ) { }
              str = str+ "\n" + line; 
          }
      }
      
      我强烈建议:

      • 您可以让IDE为您执行缩进,这样您就可以清楚地看到
        if
        块等所包含的内容
      • 你总是在while/if/etc中使用大括号,以使事情更清楚
      • 您无法捕获裸露的
        异常
        (捕获更具体的异常)
      • 在不记录异常的情况下,不能接受异常
      • 没有什么特别的原因,你一秒钟也睡不着
      • 在循环()中不使用字符串连接
      • 您可以将代码分解为更小的方法
      • 您可以在第一次使用时声明局部变量,而不是在方法的顶部声明所有变量
      • 您可以关闭
        finally
        块中的流和其他类似资源,以避免在引发异常时泄漏资源

      所以我应该把它从while criteration中删除?你可以按照乔恩在回答中的建议去做。所以我应该把它从while criteration中删除