[英]How to make integer value auto-increment in java?
I have a text file which has records like this: 我有一个文本文件,其中包含以下记录:
1 Hamada PEPSI
2 Johny PEPSI
the format of these records is like this: 这些记录的格式如下:
int id, String name, String drink
I wrote a small method to add records to this text file but id must be unique for every record 我写了一个小方法将记录添加到此文本文件,但是id对于每个记录都必须是唯一的
for example: these records are not acceptable: 例如:这些记录是不可接受的:
1 Hamada PEPSI
2 Johny PEPSI
1 Terry Milk
here is my code: 这是我的代码:
public void addProduct(int id, String name, String drink)
{
Formatter x = null;
try{
FileWriter f = new FileWriter("C:\\Users\\فاطمة\\Downloads\\products.txt", true);
x = new Formatter(f);
x.format("%d %s %s %s%n",id,name,drink);
x.close();
}
catch(Exception e)
{
System.out.println("NO Database");
}
}
How to make id be auto-increment when entering new record? 输入新记录时如何使id自动递增?
for example: 例如:
1 Ahmed PEPSI
2 Hamada PEPSI
3 Johny Milk
4 Terry Milk
5 Jack Miranda
6 Sarah Juice
Ugly code. 丑陋的代码。 You're a beginner, so you need to know that readability matters.
您是一个初学者,因此您需要知道可读性很重要。 Pay attention to format.
注意格式。
Don't print message to System.out. 不要将消息打印到System.out。 Always print a stack trace at minimum in a catch block.
始终至少在catch块中打印堆栈跟踪。
private static int AUTO_INCREMENT_ID = 1;
public void addProduct(String name, String drink) {
Formatter x = null;
try {
FileWriter f = new FileWriter("C:\\Users\\فاطمة\\Downloads\\products.txt", true);
x = new Formatter(f);
x.format("%d %s %s %s%n",AUTO_INCREMENT_ID++,name,drink);
x.close();
} catch(Exception e) {
e.printStackTrace();
}
}
More bad code: Can't change file; 更糟糕的代码:无法更改文件; don't close resources.
不要关闭资源。
Finally i found an answer for my question, hope this be useful for other programmers. 最后,我找到了问题的答案,希望这对其他程序员有用。
this is the code: 这是代码:
public void addProduct(String name, String drink)
{
int max = 0;
Scanner y = null;
try{
y = new Scanner(new File("C:\\Users\\فاطمة\\Downloads\\products.txt"));
while(y.hasNext())
{
int a = y.nextInt(); // id
String b = y.next(); // name
String c = y.next(); // drink
max = a;
}
y.close();
}
catch(Exception e)
{
e.printStackTrace();
}
Formatter x = null;
try{
FileWriter f = new FileWriter("C:\\Users\\فاطمة\\Downloads\\products.txt", true);
x = new Formatter(f);
x.format("%d %s %s%n",++max,name,drink);
x.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
Explanation: we create a variable called max and initialize it to zero .. well ... now if it was the first time to create file and adding records to it, first id will be 1 for the first record... 说明:我们创建了一个名为max的变量,并将其初始化为零..好吧……现在,如果这是第一次创建文件并向其中添加记录,则第一个记录的第一个id将为1 ...
if text file is already exists... then the program will search for max id and increment it .... for example: 如果文本文件已经存在...则程序将搜索最大id并将其递增..例如:
1 Hamada PEPSI
2 Johny MILK
then when adding new record it will have the id = 3 然后在添加新记录时,其id = 3
if there is any errors, please tell me guys :) 如果有任何错误,请告诉我:)
thanks to all of you :) 感谢大家 :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.