Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/119.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中,如何将数据从getter传递到main方法?_Java - Fatal编程技术网

在java中,如何将数据从getter传递到main方法?

在java中,如何将数据从getter传递到main方法?,java,Java,我已经用getter和setter编写了一个java类。我想把数据存储在一个文本文件中,所以我用main方法创建了另一个java类。 但我得到的输入是空的。有人能认出我的错误并重做吗 这是我的代码 package Test; import static java.lang.System.out; public class NewClass2 { private String[] today = new String[4]; private int[] time = new int[4

我已经用getter和setter编写了一个java类。我想把数据存储在一个文本文件中,所以我用main方法创建了另一个java类。 但我得到的输入是空的。有人能认出我的错误并重做吗

这是我的代码

 package Test;
 import static java.lang.System.out;
 public class NewClass2 {

private  String[] today = new String[4];
private  int[] time = new int[4];


NewClass2() {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

public  int[] gettime(){
    return time;
}
public void settime(int[] time){
    this.time = time;
}
public  String[] gettoday(){
    return today;
}
public void settoday(String[] today){
    this.today = today;
}
public void printData(){
    out.printf("%s", (Object) today);
    out.printf("%d", (Object) time);

}
}


package Test;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
public class NewClass4 {

 public static void main(String args[])throws IOException {
    FileWriter fw = new FileWriter("google1.txt");
    BufferedWriter bw = new BufferedWriter(fw);
    NewClass2 mynewclass=new NewClass2();
    String[] todaynew=mynewclass.gettoday();
    int[] timenew=mynewclass.gettime();
     for (int i=0; i <4; i++) {
         try{
        bw.write(todaynew[i]+"  ");
        bw.write(timenew[i]);
        bw.write("hello");
        bw.newLine();
         }
         catch(IOException e){

    }
 }


try
{
    if ( bw != null)
    bw.close( );
    fw.close( );
}
catch ( IOException e)
{
}
}
封装测试;
导入静态java.lang.System.out;
公共类NewClass2{
私有字符串[]今天=新字符串[4];
私有整数[]时间=新整数[4];
新类别2(){
抛出新的UnsupportedOperationException(“尚未受支持”);//若要更改生成的方法体,请选择“工具”“模板”。
}
public int[]gettime(){
返回时间;
}
公共无效设置时间(int[]时间){
这个时间=时间;
}
公共字符串[]gettoday(){
今天回来;
}
今日公开(字符串[]今日){
this.today=今天;
}
public void printData(){
out.printf(“%s”,(对象)今天);
out.printf(“%d”,(对象)时间);
}
}
包装试验;
导入java.io.BufferedWriter;
导入java.io.FileWriter;
导入java.io.IOException;
公共类NewClass4{
公共静态void main(字符串args[])引发IOException{
FileWriter fw=新的FileWriter(“google1.txt”);
BufferedWriter bw=新的BufferedWriter(fw);
NewClass2 mynewclass=newnewclass2();
字符串[]todaynew=mynewclass.gettoday();
int[]timenew=mynewclass.gettime();

对于(inti=0;i3),这里有几个问题

  • 构造函数不应引发异常

  • 应该正确命名getter和setter

  • 你不应该吞下/隐藏例外情况

  • NewClass2的命名应该与if实际表示的内容相匹配

  • NewClass2有两个从不包含任何值的数组,您只需初始化该数组。如果您为每个数组索引分配值,则应该开始工作


  • 这是一个非常快速的答案,但我希望它能引导您走向正确的方向:

    数据类

        public class Data {
    
        private String date;
        private int time;
    
        public Data(String date, int time) {
            super();
            this.date = date;
            this.time = time;
        }
        public String getDate() {
            return date;
        }
    
        public int getTime() {
            return time;
        }
    }
    
    public class WriteToFile {
    
        public static Data data;
    
        public void writeToFileExample() {
    
            BufferedWriter bw = null;
            try {
                File file = new File("google1.txt");
                FileWriter fw = new FileWriter(file);
                bw = new BufferedWriter(fw);
                bw.write(data.getDate() + " " + data.getTime());
            }
            catch(Exception e) {
                System.out.println(e);
            }
            finally {
                try {
                    if(bw != null) 
                        bw.flush();
                        bw.close();
                } catch(IOException ex) {
                    ex.printStackTrace();
                }
            }
             System.out.println("\n Written into file succesfully");       
        }
    
        public static void main(String args[]) throws IOException {
    
            data = new Data("Tuesday", 12);
            WriteToFile wtf = new WriteToFile();
            wtf.writeToFileExample();
        }
    }
    
    写入文件类

        public class Data {
    
        private String date;
        private int time;
    
        public Data(String date, int time) {
            super();
            this.date = date;
            this.time = time;
        }
        public String getDate() {
            return date;
        }
    
        public int getTime() {
            return time;
        }
    }
    
    public class WriteToFile {
    
        public static Data data;
    
        public void writeToFileExample() {
    
            BufferedWriter bw = null;
            try {
                File file = new File("google1.txt");
                FileWriter fw = new FileWriter(file);
                bw = new BufferedWriter(fw);
                bw.write(data.getDate() + " " + data.getTime());
            }
            catch(Exception e) {
                System.out.println(e);
            }
            finally {
                try {
                    if(bw != null) 
                        bw.flush();
                        bw.close();
                } catch(IOException ex) {
                    ex.printStackTrace();
                }
            }
             System.out.println("\n Written into file succesfully");       
        }
    
        public static void main(String args[]) throws IOException {
    
            data = new Data("Tuesday", 12);
            WriteToFile wtf = new WriteToFile();
            wtf.writeToFileExample();
        }
    }
    

    另外,getter和setter应该遵循camelCase惯例,在你提问时总是缩进你的代码。你可以通过选择你的代码并按CTRL+K来做到这一点。

    你的
    NewClass2
    构造函数到底发生了什么?也永远不要默默地吞下异常-总是打印出来是的,它正在编译,它正在获取g打印“hello”和“Null”注意Java命名约定。get和set后的第一个字符必须是大写,但为什么要从构造函数中抛出UnsupportedOperationException?你会犯我代码中的这些错误吗?因为我不知道如何做谢谢你的回答,但我想传递数组