Javaでファイルにユーザーや書き込みからの入力を読んで問題

アビシェーク・クマール:

これは、基本クラスの従業員コードです

public class Employee {
    private String name;
    private int age;
    private String address;
    private String EmpId;
    private double salary;

    public Employee(String name,int age,String address,String EmpId,double salary)  {
        this.name=name;
        this.age=age;
        this.address=address;
        this.EmpId=EmpId;
        this.salary=salary;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getEmpId() {
        return EmpId;
    }

    public void setEmpId(String empId) {
        EmpId = empId;
    }

    public double getSalary() {
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }
}

ここでは、ソフトウェアの従業員は、この基本クラスを拡張して

public class SoftwareEmployee extends Employee{
    private String skillSet;
    public SoftwareEmployee(String name,int age,String address,String EmpId,double salary,String 
    skillSet) {
        super(name,age,address,EmpId,salary);
        this.skillSet=skillSet;
    }

    public String getSkillSet() {
        return skillSet;
    }

    public void setSkillSet(String skillSet) {
        this.skillSet = skillSet;
    }

}

また、金融従業員は、コードがある基底クラスを拡張しています。

public class FinanceEmployee extends Employee {
    private String Degree;
    public FinanceEmployee(String name,int age,String address,String EmpId,double salary,String Degree) {
        super(name,age,address,EmpId,salary);
        this.Degree=Degree;
    }

    public String getDegree() {
        return Degree;
    }

    public void setDegree(String degree) {
        Degree = degree;
    }   
}

ここでは、外部ファイルに書き込むことはできません。私が最初にして、年齢の名前を挿入するためにも、コンソールが待機していません。それはちょうど、同時に再び二つのことを一度に名前と年齢を印刷しています。私はこれに非常に新しいです。

これは私のメインクラスです

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Scanner;


public class Main {

    public static void main(String[] args) throws IOException  {
        Scanner sc = new Scanner(System.in);
        //File file = new File("C:\\Users\\AK078479\\Documents\\newfile.txt");
        FileWriter fWriter = new FileWriter("C:\\Users\\AK078479\\Documents\\newfile.txt");
        //BufferedWriter writer = null;

        Employee employee=new Employee(null, 0, null, null, 0);
        SoftwareEmployee sftemp=new SoftwareEmployee(null, 0, null, null, 0, null);
        FinanceEmployee fncemp=new FinanceEmployee(null, 0, null, null, 0, null);
        System.out.println("You are "+"\n"+"1.Reviewer"+"\n"+"2.Data Entry Operator");
        int choice = sc.nextInt();
        switch(choice) {
            case(1): 
                System.out.println("Do you want to see a"+ "\n" + "1.Particular record"+"\n"+"2.All 
         records");
                int value=sc.nextInt();
                switch(value) {
                    case(1):
                }
                break;
            case(2):
                System.out.println("Employee is a "+"\n"+"1.Software Employee"+"\n"+"2.Finance 
      Employee");
                int num =sc.nextInt();
                switch(num) {
                    case(2):
                        try {

                            //String s="Finance Employee";
                            //writer.write(s);
                            //writer.newLine();
                            int n=2;
                            while(n>0) {

                                //writer = new BufferedWriter(fWriter);
                                System.out.println("Enter name");
                                String text = sc.next();
                                fncemp.setName(text);
                                fWriter.write(text);
                                //fWriter.newLine();
                                System.out.println("Enter age");
                                int age=sc.nextInt();
                                fncemp.setAge(age);
                                fWriter.write(age);
                                //writer.newLine();
                                System.out.println("Enter address");
                                String add = sc.nextLine();
                                fncemp.setAddress(add);
                                fWriter.write(add);
                                //fwriter.newLine();
                                System.out.println("Enter EmpId");
                                String emplId = sc.nextLine();
                                fncemp.setEmpId(emplId);
                                fWriter.write(emplId);
                                //writer.newLine();
                                System.out.println("Enter salary");
                                double sal = sc.nextDouble();
                                fncemp.setSalary(sal);
                                fWriter.write((int) sal);
                                //writer.newLine();
                                System.out.println("Enter degree");
                                String degree = sc.nextLine();
                                fncemp.setDegree(degree);
                                fWriter.write(degree);
                                //writer.newLine();  
                                n--;
                            }      
                        } catch (Exception e) {
                            System.out.println("Error!");
                        }
                    break;
                    case(1):
                        try {

                            //String st="Software Employee";
                            //writer.write(st);
                           // writer.newLine();
                            int n=2;
                            while(n>0) {

                                //fWriter = new FileWriter(file);
                                //writer = new BufferedWriter(fWriter);
                                System.out.println("Enter name");
                                //String text = sc.nextLine();
                                sftemp.setName(sc.nextLine());
                                fWriter.write(sftemp.getName());
                                //fWriter.newLine();
                                System.out.println("Enter age");
                                int age=sc.nextInt();
                                sftemp.setAge(age);
                                fWriter.write(age);
                                //fWriter.newLine();
                                System.out.println("Enter address");
                                String add = sc.nextLine();
                                sftemp.setAddress(add);
                                fWriter.write(add);
                                //writer.newLine();
                                System.out.println("Enter EmpId");
                                String emplId = sc.nextLine();
                                sftemp.setEmpId(emplId);
                                fWriter.write(emplId);
                                //writer.newLine();
                                System.out.println("Enter salary");
                                double sal = sc.nextDouble();
                                sftemp.setSalary(sal);
                                fWriter.write((int) sal);
                                //writer.newLine();
                                System.out.println("Enter skill Set");
                                String skill = sc.nextLine();
                                sftemp.setSkillSet(skill);
                                fWriter.write(skill);
                                //writer.newLine();  
                                n--;
                            }      
                        } catch (Exception e) {
                            System.out.println("Error!");
                        }
                    break;
                    default:
                        System.out.println("Try again");

                }
                break;
                default:
                    System.out.println("Try again");

        }
    }
}
アレックス:

あなたはスキャナメソッドを使用した後nextInt()、あなたが呼び出す必要がありnextLine()ますが、文字列を読むことができる前に、もう一回。あなたが呼び出した後、その理由はされていることをnextInt、入力ストリームで自分の位置を追跡するスキャナ内部カーソルが整数を渡しますが、右側の改行文字の前で停止します。あなたが呼び出すとnextLine()スキャナはただ、次の行に空の文字列と動きを返しますので、唯一の次nextLine()の呼び出しでは、数字の後に入力された非空の文字列を返します。したがって、ここでの例:

System.out.println("You are "+"\n"+"1.Reviewer"+"\n"+"2.Data Entry Operator");
int choice = sc.nextInt();
switch(choice) 

あなたが追加する必要があります。

System.out.println("You are "+"\n"+"1.Reviewer"+"\n"+"2.Data Entry Operator");
int choice = sc.nextInt();
sc.nextLine();   // Skip the end of line and prepare to read something from the next line
switch(choice)  

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=284303&siteId=1