Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.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 数据被保存,但当我加载变量时,它们显示为0,程序捕获我的加载异常_Java_Oop_Exception Handling_Text Files - Fatal编程技术网

Java 数据被保存,但当我加载变量时,它们显示为0,程序捕获我的加载异常

Java 数据被保存,但当我加载变量时,它们显示为0,程序捕获我的加载异常,java,oop,exception-handling,text-files,Java,Oop,Exception Handling,Text Files,你好,我正在尝试使用outfile和infle制作一个文本文件。似乎我的saveData方法工作正常。但是,我的loadData方法无法为我的RentalDays和RentalRate打印正确的数据。当我运行程序时,它会捕获我为加载而创建的异常,并创建一个名为sample.txt的文本文件。my sample.txt文件中的文本打印RentalDays 0和RentalRate 0。请帮忙。如果我不够清楚,可以提问 import java.io.*; import java.ut

你好,我正在尝试使用outfile和infle制作一个文本文件。似乎我的saveData方法工作正常。但是,我的loadData方法无法为我的RentalDays和RentalRate打印正确的数据。当我运行程序时,它会捕获我为加载而创建的异常,并创建一个名为sample.txt的文本文件。my sample.txt文件中的文本打印RentalDays 0和RentalRate 0。请帮忙。如果我不够清楚,可以提问

    import java.io.*;
    import java.util.*;
    import javax.swing.*;

    public class TractorText {
       private static int RentalRate;
       private static int RentalDays;

       public int setRentalRate(int RentalRate) {
            if (RentalRate <= 0 || RentalRate > 100000) 
            {
              return -1;
            } 
            else 
            {
               return RentalRate;

            }
         }


       public int setRentalDays(int RentalDay) {
            if (RentalDays <= 0 || RentalDays > 365) 
            {
              return -1;
            } 
            else 
            {

               return RentalDays;
            }
         }

          TractorText() {
      setRentalRate(20);
      setRentalDays(30);
   }

TractorText(int RentalRate, int RentalDays) {
          this.RentalRate = setRentalRate(RentalRate);
          this.RentalDays = setRentalDays(RentalDays);
       }
          public static void saveData(String filename) throws Exception {
                File outFile = new File(filename);
                FileOutputStream outFileStream = new FileOutputStream(outFile);
                PrintWriter outStream = new PrintWriter(outFileStream);

                //write values of primitive data types to the stream
                outStream.println("Rental Days " + RentalDays);
                outStream.println("Rental Rate" + RentalRate);

                outStream.close();

            }

            public static void loadData(String filename) throws Exception {

                File inFile = new File(filename);
                FileReader fileReader = new FileReader(inFile);
                BufferedReader bufReader = new BufferedReader(fileReader);
                String turn;
                turn = bufReader.readLine();
                int g = Integer.parseInt(turn);
                System.out.println(g);
                turn = bufReader.readLine();
                System.out.println(turn);

                bufReader.close();

               }

       public static void main(String[] args){
           try { saveData("sample.txt"); }
            catch ( Exception e) { System.out.println("Error saving data."); }


            try { loadData("sample.txt"); }
            catch ( Exception e) { System.out.println("Loading data..."); }


       }

    }
import java.io.*;
导入java.util.*;
导入javax.swing.*;
公共类TractorText{
私人静态租金;
私人租赁日;
公共国际租赁费率(国际租赁费率){
如果(租金100000)
{
返回-1;
} 
其他的
{
租金回报率;
}
}
公共国际租赁日(国际租赁日){
如果(租金365天)
{
返回-1;
} 
其他的
{
归还租金;
}
}
TractorText(){
地租率(20);
三天(30);
}
TractorText(国际租赁费率,国际租赁日){
this.RentalRate=setRentalRate(RentalRate);
this.RentalDays=setRentalDays(RentalDays);
}
公共静态void saveData(字符串文件名)引发异常{
文件输出文件=新文件(文件名);
FileOutputStream outFileStream=新FileOutputStream(outFile);
PrintWriter outStream=新的PrintWriter(outFileStream);
//将基本数据类型的值写入流
exptream.println(“租赁天数”+租赁天数);
超流打印(“租金率”+租金率);
exptream.close();
}
公共静态void loadData(字符串文件名)引发异常{
文件填充=新文件(文件名);
FileReader FileReader=新的FileReader(infle);
BufferedReader bufReader=新的BufferedReader(文件读取器);
串转;
turn=bufReader.readLine();
int g=整数.parseInt(回合);
系统输出打印ln(g);
turn=bufReader.readLine();
系统输出打印项次(圈);
bufReader.close();
}
公共静态void main(字符串[]args){
请尝试{saveData(“sample.txt”);}
catch(异常e){System.out.println(“保存数据时出错”);}
请尝试{loadData(“sample.txt”);}
catch(异常e){System.out.println(“加载数据…”);}
}
}
我改变了

try { loadData("sample.txt"); }
        catch ( Exception e) { System.out.println("Loading data..."); }

得到

java.lang.NumberFormatException:对于输入字符串:“租赁天数0”
正在加载数据。。。
位于java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
在java.lang.Integer.parseInt(Integer.java:580)处
在java.lang.Integer.parseInt(Integer.java:615)
在tractortext.tractortext.loadData(tractortext.java:70)
位于tractortext.tractortext.main(tractortext.java:84)

生成成功(总时间:0秒)

正在分析的输入字符串不是整数。
try { loadData("sample.txt"); }
        catch ( Exception e) { 
            e.printStackTrace();
            System.out.println("Loading data..."); }