Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/373.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
在.txt文件中保存整数输入(Java)_Java_File_Input_Output_File Handling - Fatal编程技术网

在.txt文件中保存整数输入(Java)

在.txt文件中保存整数输入(Java),java,file,input,output,file-handling,Java,File,Input,Output,File Handling,我试图将输入的整数写入文本文件,但不管我如何编辑代码,我的记事本都会把我的整数当成废话。例如: 输入的整数:java.util.Scanner[delimiters=\p{javaWhitespace}+][position=1][match valid=true][need input=false][source closed=false][skipped=false] 我相信我的问题出在我用“*”标记的那一行。我该如何着手解决这个问题?我相信这与“String.valueOf(input)”

我试图将输入的整数写入文本文件,但不管我如何编辑代码,我的记事本都会把我的整数当成废话。例如:

输入的整数:java.util.Scanner[delimiters=\p{javaWhitespace}+][position=1][match valid=true][need input=false][source closed=false][skipped=false]

我相信我的问题出在我用“*”标记的那一行。我该如何着手解决这个问题?我相信这与“String.valueOf(input)”行有关。完整代码链接如下

for (int i = 0 ; i < 10 ; i++) {
             System.out.printf("Please enter integer %d: ", i+1);
             numbers[i] = input.nextInt();

             {
                 try
                 {
                    *output.format("Integer inputted: %s%n", String.valueOf(input));
                 }
                 catch (FormatterClosedException formatterClosedexception)
                 {
                     System.err.println("Error writing to the file. Terminating.");
                     break;
                 }
                 catch (NoSuchElementException elementException)
                 {
                     System.err.println("Invalid input. Please try again.");
                     input.nextLine();
                 }
for(int i=0;i<10;i++){
System.out.printf(“请输入整数%d:,i+1”);
数字[i]=input.nextInt();
{
尝试
{
*output.format(“输入的整数:%s%n”,String.valueOf(input));
}
catch(FormatterClosedException FormatterClosedException)
{
System.err.println(“写入文件时出错。正在终止”);
打破
}
catch(NoSuchElementException元素异常)
{
System.err.println(“输入无效,请重试”);
input.nextLine();
}

您可以使用此选项写入文件:

PrintWriter writer = new PrintWriter(fileName);
writer.println("1");
writer.println("2");
writer.close();
要从该文件中读取,请执行以下操作:

  String line, newLine;         // Variable to store a line and to check for a new line
  String[] splited;             // Variable to split text
  int lines = 0;                // Variable to check how many lines in a file

  Scanner myReader = new Scanner(new File(fileName));
  BufferedReader bufferedReader = new BufferedReader(new FileReader(fileName));

  Numbers[] numbers = new numbers[2];

  while ((line = bufferedReader.readLine()) != null) {
     lines++;
     for (int i = 1 ; i < 2 ; i++) {
        while (myReader.hasNextLine()) {
           newLine = myReader.nextLine();
           splited = newLine.split(",");
           numbers[i] = Integer.parseInt(splited[i]);
       }
     }
  }

  myReader.close();
字符串行,换行;//用于存储行和检查新行的变量
字符串[]已拆分;//用于拆分文本的变量
int lines=0;//用于检查文件中有多少行的变量
Scanner myReader=新扫描仪(新文件(文件名));
BufferedReader BufferedReader=新的BufferedReader(新文件读取器(文件名));
编号[]编号=新编号[2];
而((line=bufferedReader.readLine())!=null){
行++;
对于(int i=1;i<2;i++){
while(myReader.hasNextLine()){
newLine=myReader.nextLine();
拆分=换行。拆分(“,”);
数字[i]=整数.parseInt(拆分[i]);
}
}
}
myReader.close();

我刚刚在这里写了这篇文章,但根据我的记忆,我就是这么写的。

这看起来对吗?它仍然给了我一个空白文档。好吧,我犯了一个多么愚蠢的错误。现在一切都很好,但我似乎无法将整数打印到文件中,但我可以打印任何其他内容。这是我正在使用的行:“writer.printf(输入的整数是%d\n,input);”但是我得到了这个错误:线程“main”java.util.IllegalFormatConversionException:d!=java.util.Formatter$FormatSpecifier.failConversion处的java.util.Scanner.failConversion(未知源代码)不是这样写的,而是这样写的:writer.printf(“Text:+number[I]”);好的,到目前为止还不错。现在你不认为我应该实现你发布的整个第二个代码,对吗?因为我已经有了for循环。在这种情况下,我会把它放在另一个方法中吗?最初我用两个不同的方法将它们分开,然后在主方法中调用它们,所以是的,我建议你把它们都放在不同的方法中。