java - How do I add two string elements in vector?
问题描述
I am new to Java and learning through a task.
I have tried to create a program but when I input my name it is raising a InputMismatchException
exception.
Here is my code:
Name.java
package LearnJava;
public class Name {
String firstName,surName;
public Name(String fName, String sName) {
super();
this.firstName = fName;
this.surName = sName;
}
public String getName() {
return firstName;
}
public void setName(String fName) {
this.firstName = fName;
}
public String getSname() {
return surName;
}
public void setSname(String sName) {
this.surName = sName;
}
}
解决方案
If I see it correctly and you are just entering your name the problem is the scanning for int to get n. n is an int and the scanner will get your name which is a string which will result in the mismatch exception. Also v1 is unnecessary vector of objects as you know you are only giving it names, you should write the type name so you could later use the properties of Name.
推荐阅读
- c# - 使用 WriteAllText 如何将 DataTable 列名写入 CSV 文件
- reactjs - useCallback 和 useMemo react hook 的缓存大小是多少,为什么?
- phaser-framework - Phaser 3:为什么我的 this.cameras.main.fadeOut 效果没有立即启动?
- amazon-web-services - AWS MediaConvert CMAF 格式 HLS 不起作用
- terraform - 控制 Compute Engine 实例名称
- java - 如何使用 JMeter JDBC Request 在 OracleDB 中测试这个存储函数?
- excel - Excel 宏在 Excel 2016 中不起作用 - Selection.cut
- ssl - 在 jmeter 中启用详细的服务器证书身份验证
- python - 如何查找一个值是否按顺序或时间顺序出现?
- azure-devops - 为什么在合并拉取请求以进行开发时 GitVersion 会增加我的版本号的主版本?