如何在java中增加id并进行冗长的描述
我制作了一个Note类,我想在其中增加id。它不会增加。我还从控制台获取输入描述。如何从用户那里接受像“这是Hello World”这样的java冗长描述。请提供帮助如何在java中增加id并进行冗长的描述,java,arrays,arraylist,collections,Java,Arrays,Arraylist,Collections,我制作了一个Note类,我想在其中增加id。它不会增加。我还从控制台获取输入描述。如何从用户那里接受像“这是Hello World”这样的java冗长描述。请提供帮助 public class Note { private String title; private static int id; private static int count = 0; private String description; public static int getId()
public class Note {
private String title;
private static int id;
private static int count = 0;
private String description;
public static int getId() {
return id = ++count;
}
public String getTitle() {
id++;
return title;
}
public String getDescription(){
return description;
}
public void setDescription(String description){
this.description=description;
}
public String toString() {
return ("Id : " + id + "\n Title :" + title + "\n Description :"+ description);
}
}
注意接受用户输入的控制台类。它接受1。在我希望接受用户正确描述的位置添加注释。第二个视图注释,在toString方法的帮助下,我打印输出。三是退出
import java.util.ArrayList;
import java.util.Scanner;
public class NoteConsole {
public static void NoteConsole() {
final int ADD_NOTE = 1;
final int VIEW_NOTE = 2;
final int EXIT = 3;
boolean loop = false;
NoteConsole nc = new NoteConsole();
Note note = new Note();
ArrayList<Note> notes = new ArrayList<Note>();
NoteServiceSerialize service1 = new NoteServiceSerialize();
System.out.println("Write to the Console 1.AddNote, 2. ToView 3. Exit");
Scanner in = new Scanner(System.in);
int choice = in.nextInt();
while (!loop) {
switch (choice) {
case ADD_NOTE: {
System.out.println("Enter the title");
String title = in.next();
note.setTitle(title);
System.out.println("Enter the description");
String description = in.next();
note.setDescription(description);
notes.add(note);
service1.noteSerialize(notes);
break;
}
case VIEW_NOTE: {
for (Note note1 : notes) {
System.out.println(note1);
}
break;
}
case EXIT: {
//code
}
}
}
}
public static void main(String[] args) {
NoteConsole();
}
import java.util.ArrayList;
导入java.util.Scanner;
公共类记事本控制台{
公共静态void NoteConsole(){
最终整数加注释=1;
最终int视图_注释=2;
最终int出口=3;
布尔循环=假;
NoteConsole nc=新的NoteConsole();
注释=新注释();
ArrayList notes=新的ArrayList();
NoteServiceSerialize service1=新的NoteServiceSerialize();
System.out.println(“写入控制台1.AddNote,2.ToView 3.Exit”);
扫描仪输入=新扫描仪(系统输入);
int choice=in.nextInt();
while(!loop){
开关(选择){
案例补充说明:{
System.out.println(“输入标题”);
字符串title=in.next();
注:设置标题(标题);
System.out.println(“输入说明”);
String description=in.next();
注:设置说明(说明);
注:增加(注);
服务1.注释序列化(注释);
打破
}
案例视图\u注:{
用于(注1:注){
系统输出打印项次(注1);
}
打破
}
案件出口:{
//代码
}
}
}
}
公共静态void main(字符串[]args){
NoteConsole();
}
}
NoteServiceSerialize类-
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
public class NoteServiceSerialize {
public void noteSerialize (ArrayList<Note> list){
try{
FileOutputStream file = new FileOutputStream("D:\\serializable_notes.txt");
ObjectOutputStream obj = new ObjectOutputStream(file);
obj.writeObject(list);
file.close();
obj.close();
}
catch(Exception e){
e.printStackTrace();
}
}
}
import java.io.FileOutputStream;
导入java.io.ObjectOutputStream;
导入java.util.ArrayList;
公共类NoteServiceSerialize{
公共void noteSerialize(ArrayList列表){
试一试{
FileOutputStream文件=新的FileOutputStream(“D:\\serializable_notes.txt”);
ObjectOutputStream obj=新的ObjectOutputStream(文件);
对象写入对象(列表);
file.close();
obj.close();
}
捕获(例外e){
e、 printStackTrace();
}
}
}
在类的构造函数中生成id
实例变量并增加count
,并将count
的当前值分配给id
public class Note {
private String title;
private int id;
private static int count = 0;
private String description;
public Note(){
count++;
this.id = count;
}
public int getId() {
return id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription(){
return description;
}
public void setDescription(String description){
this.description=description;
}
public String toString() {
return ("Id : " + id + "\n Title :" + title + "\n Description :"+ description);
}
}
现在,每个Note对象都将有单独的id
public class NoteConsole {
//NoteConsole() is not constructor, should avoid same method name and class name
public static void NoteConsole() {
final int ADD_NOTE = 1;
final int VIEW_NOTE = 2;
final int EXIT = 3;
boolean loop = true;
//NoteConsole nc = new NoteConsole();
Note note = new Note();
ArrayList<Note> notes = new ArrayList<Note>();
//NoteServiceSerialize service1 = new NoteServiceSerialize();
Scanner in = new Scanner(System.in);
while (loop) {
System.out.println("Write to the Console 1.AddNote, 2. ToView 3. Exit");
int choice = in.nextInt();
in.nextLine();
switch (choice) {
case ADD_NOTE: {
System.out.println("Enter the title");
//in.nextLine() will read complete line.
String title = in.nextLine();
note.setTitle(title);
System.out.println("Enter the description");
String description = in.nextLine();
note.setDescription(description);
notes.add(note);
//service1.noteSerialize(notes);
break;
}
case VIEW_NOTE: {
for (Note note1 : notes) {
System.out.println(note1);
}
break;
}
case EXIT: {
loop = false;
}
}
}
in.close();
}
public static void main(String[] args) {
NoteConsole();
}
公共类NoteConsole{
//NoteConsole()不是构造函数,应避免使用相同的方法名和类名
公共静态void NoteConsole(){
最终整数加注释=1;
最终int视图_注释=2;
最终int出口=3;
布尔循环=真;
//NoteConsole nc=新的NoteConsole();
注释=新注释();
ArrayList notes=新的ArrayList();
//NoteServiceSerialize service1=新的NoteServiceSerialize();
扫描仪输入=新扫描仪(系统输入);
while(循环){
System.out.println(“写入控制台1.AddNote,2.ToView 3.Exit”);
int choice=in.nextInt();
in.nextLine();
开关(选择){
案例补充说明:{
System.out.println(“输入标题”);
//in.nextLine()将读取完整的行。
字符串title=in.nextLine();
注:设置标题(标题);
System.out.println(“输入说明”);
String description=in.nextLine();
注:设置说明(说明);
注:增加(注);
//服务1.注释序列化(注释);
打破
}
案例视图\u注:{
用于(注1:注){
系统输出打印项次(注1);
}
打破
}
案件出口:{
循环=假;
}
}
}
in.close();
}
公共静态void main(字符串[]args){
NoteConsole();
}
id
将保持0(原语的默认值),除非调用其中一个方法:
public static int getId() {
return id = ++count;
}
public String getTitle() {
id++;
return title;
}
实际增加id
的值。可能类似于:
public String toString() {
return ("Id : " + getId() + "\n Title :" + title + "\n Description :"+ description);
}
还要确保您的计数已初始化为:
private static final int count = 0;
首先,还应该提供NoteServiceSerialize
类。
对于描述问题,应在.nextLine()中使用
。
关于增量id,在getTitle()
中这样做是不好的做法。
更好的选择是:
private static int lastId;
private int id = nextId();
private static int nextId() {
lastId = ++lastId;
return lastId;
}
而您的类也有一些其他问题,请用以下内容替换它们:
public class Note {
private String title;
private String description;
private static int lastId;
private int id = nextId();
private static int nextId() {
lastId = ++lastId;
return lastId;
}
public static int getLastId() {
return lastId;
}
public String getTitle() {
return title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public void setTitle(String description) {
this.description = description;
}
@Override
public String toString() {
return ("Id : " + id + "\nTitle :" + title + "\nDescription :" + description);
}
}
import java.util.ArrayList;
导入java.util.Scanner;
公共类记事本控制台{
最终静态ArrayList注释=新建ArrayList();
公共静态void NoteConsole(){
NoteServiceSerialize service1=新的NoteServiceSerialize();
NoteConsole nc=新的NoteConsole();
注释=新注释();
最终整数加注释=1;
最终int视图_注释=2;
最终int出口=3;
布尔循环=假;
扫描仪输入=新扫描仪(系统输入);
while(!loop){
System.out.println(“写入控制台1.AddNote 2.ToView 3.Exit”);
int choice=in.nextInt();
开关(选择){
案例补充说明:
System.out.println(“输入标题:”);
字符串title=in.next();
System.out.println(“输入描述:”);
//如果在.next()中删除描述文本有问题
in.next();
String description=in.nextLine();
注:设置标题(标题);
注:设置说明(说明);
注:增加(注);
服务1.注释序列化(注释);
import java.util.ArrayList;
import java.util.Scanner;
public class NoteConsole {
final static ArrayList<Note> notes = new ArrayList<Note>();
public static void NoteConsole() {
NoteServiceSerialize service1 = new NoteServiceSerialize();
NoteConsole nc = new NoteConsole();
Note note = new Note();
final int ADD_NOTE = 1;
final int VIEW_NOTE = 2;
final int EXIT = 3;
boolean loop = false;
Scanner in = new Scanner(System.in);
while (!loop) {
System.out.println("Write to the Console 1.AddNote 2. ToView 3. Exit");
int choice = in.nextInt();
switch (choice) {
case ADD_NOTE:
System.out.println("Enter the title :");
String title = in.next();
System.out.println("Enter the description :");
//if you had a problem with description text remove in.next()
in.next();
String description = in.nextLine();
note.setTitle(title);
note.setDescription(description);
notes.add(note);
service1.noteSerialize(notes);
System.out.println("----------------------");
break;
case VIEW_NOTE:
for (Note note1 : notes) {
System.out.println(note1);
System.out.println("----------------------");
}
break;
case EXIT:
loop = !loop;
break;
default:
System.out.println("Not a Valid Option !!!");
}
System.out.println();
}
}
public static void main(String[] args) {
NoteConsole();
}
}