我不知道代码的错误,但我甚至不知道提交的基本知识。有人请帮忙。 它输入标记和注册号甚至标记但不显示标记 我该怎么办?
class Info
{
public void writeFile() throws IOException{
try{
FileWriter f = new FileWriter("D:\\Student.txt");
Scanner in=new Scanner(System.in);
PrintWriter fw = new PrintWriter(f) ;
System.out.println("Enter Student's Name");
String name=in.nextLine();
System.out.println("Enter Student's Reg.No");
String reg_no=in.nextLine();
Scanner inp=new Scanner(System.in);
System.out.println("Enter Student's marks");
int marks=inp.nextInt();
fw.write(name);
fw.write(reg_no);
fw.write(marks) ;
fw.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
public void readFile() throws IOException
{
try{
FileReader reader =new FileReader("D:\\Student.txt");
Scanner in=new Scanner(reader);
while(in.hasNext())
System.out.println(in.next());
reader.close();
}
catch(Exception e)
{
System.out.println(e);
}
}
}
答案 0 :(得分:1)
此行fw.write(marks);
之后int marks=inp.nextInt();
marks
int
write(int c)
PrintWriter
/**
* Writes a single character.
* @param c int specifying a character to be written.
*/
public void write(int c) {
try {
synchronized (lock) {
ensureOpen();
out.write(c);
}
}
...
...
}
int
/ p>
character
负责将您的write(int c)
编写为单StreamEncoder
而不是int值,如果我们更深入的话,如果您看到源代码,则调用public void write(int c) throws IOException {
char cbuf[] = new char[1];
cbuf[0] = (char) c;
write(cbuf, 0, 1);
}
类中的int
这种方法
char
它会将您的char
值转换为char
并存储到int
,这意味着它会为您的int marks=inp.nextInt();
存储等效的String marks=inp.nextLine();
值
如果您希望预期输出更改代码
@Component
public class AccountHandlerMethodArgumentResolver implements HandlerMethodArgumentResolver {
@Autowired
private UserService userService;
@Override
public Object resolveArgument(MethodParameter methodParameter, ModelAndViewContainer mvContainer,
NativeWebRequest request, WebDataBinderFactory webDataBinderFactory) throws Exception {
Account account = null;
Principal principal = request.getUserPrincipal();
if (principal != null) {
String email = principal.getName();
User user = userService.findByEmail(email);
if (user != null) {
account = user.getAccount();
}
}
return account;
}
@Override
public boolean supportsParameter(MethodParameter methodParameter) {
return methodParameter.getParameterType().equals(Account.class);
}
}
- > Account
答案 1 :(得分:0)
尝试在编写像
这样的代码时为String添加一些额外的空间System.out.println("Enter Student's Name");
String name=in.nextLine()+" ";
最后,您的字符串Ali28809
会像Ali 288 09
那样存储
在阅读您将获得的数据时
Ali
288
09
如果您希望输出与存储在文件中的输出相同,例如Ali 288 09
在readFile()
使用nexLine()
而不是next()
FileReader reader =new FileReader("D:\\Student.txt");
Scanner in=new Scanner(reader);
while(in.hasNext());
System.out.print(in.nextLine()); //here is the change
reader.close();
并且您的输出为Ali 288 09
希望这会对您有所帮助