问题在阅读来自用户和手写输入Java中的文件

阿布舍克·库马尔:

这是基类Employee代码

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)  

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

来自用户在 PyTelegramBotApi、Telegram 中的输入

Python - 来自用户的输入

计算来自用户输入(Python 2.7)的txt文件中特殊字符出现的行数

手写识别Java

将来自用户输入的特定数量的字符存储在变量#java中

手写笔中的多个文件的编译

模拟手写笔输入

为什么来自用户的输入值在批处理文件中的代码中不起作用?

在 html(来自用户)中输入日期和时间并在 Javascript 中接收的最佳方法是什么?

在Swift 4中阅读来自Facebook Graph Request的回复

在 C# 中阅读来自 blogspot 的最新帖子

来自用户输入的角度获取请求

来自用户处理的 javascript 输入

Tricking Scanner 输入来自用户

android - 来自用户输入的 URL 编码

验证来自用户输入的浮动值

显示来自用户输入的字典键

来自用户的矩阵耦合输入

来自用户列表理解的输入

来自用户输入的 Python 饼图

使手写文本变暗 - Pdf 文件

手写笔/用户 Javascript 和 CSS 或类似的浏览器扩展

如何使用我自己的.styl文件从bootstrap手写笔文件夹中自定义Bootstrap手写笔变量?

如何解决Safari中的手写html dingbat字体大小问题

如何使用来自用户窗体的数据输入隐藏和取消隐藏工作表

让表单控件的值既来自props也来自用户输入

手写动作

如何使用函数使用来自用户输入的变量计算复杂问题并在 javascript 中显示结果

Azure Computer Vision API中从本地存储的图像文件中的手写识别