あなたの死にました :
私の小さなプロジェクトではないが、スイッチに動的な配列のおかげに新しい番号を追加することについてです。私の問題は、私は、配列内に存在する文字列の値を検索する場合、それは場合にそれを完全に省略していることです。条件ならば問題は三番目のケースであります
package savearray;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Scanner;
public class SaveArray {
public static int op, xWhile, nArray, array[], accumulator;
public static String strArray, strSeeNumber, newStringArray[];
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
do {
System.out.println("-----------------------------");
System.out.println("1: Add numbers | 2: Read numbers | 3: See existing number");
System.out.println("-----------------------------");
op = in.nextInt();
switch (op) {
case 1:
xWhile = 1;
accumulator++;
System.out.println("Size of the array:");
nArray = in.nextInt();
array = new int[nArray];
try {
for (int i = 0; i < nArray; i++) {
System.out.println("Enter the desired number");
array[i] = in.nextInt();
}
// Accumulator for when the operation is done more than 1 time, the array will not be null at the beginning, and when it is run a second time or more, don't override the strArray and act like a database, saving values until the program ends.
if (accumulator == 1) {
strArray = "";
}
for (int i : array) {
strArray += "[" + i + "]";
}
//Save array
FileOutputStream saveFile = new FileOutputStream("DB.txt");
OutputStreamWriter outWrite = new OutputStreamWriter(saveFile);
outWrite.write(strArray);
outWrite.flush();
outWrite.close();
saveFile.close();
System.out.println("File saved.");
} catch (IOException e) {
}
break;
case 2:
xWhile = 1;
try {
//Open array
FileInputStream saveFile = new FileInputStream("DB.txt");
InputStreamReader inReader = new InputStreamReader(saveFile);
BufferedReader buffRead = new BufferedReader(inReader);
String strArray = buffRead.readLine();
buffRead.close();
inReader.close();
saveFile.close();
//Print array
newStringArray = strArray.split(",");
for (String i : newStringArray) {
System.out.println(i);
}
} catch (IOException e) {
}
break;
case 3:
xWhile = 1;
in.skip("\n");
System.out.println("Input the number:");
strSeeNumber = in.nextLine();
for (String newStringArray1 : newStringArray) {
//The problem is here:
if (newStringArray1.equals(strSeeNumber)) {
System.out.print("[" + newStringArray1 + "]");
} else {
System.out.println("Number not found, find an existing number again.");
}
}
break;
default:
xWhile = 0;
break;
}
} while (xWhile == 1);
}
}
私は私の問題は、私は、テキストファイル内の文字列値を比較していることだと思うので、私はそれを修正し、あなたはあなたが持っているパターンとMatcherの(または任意のソリューションを使用する必要があり検索したい正確な数を見つけるために、その音論理ハハハを願っています)経験:
package savearray;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Scanner;
public class SaveArray {
public static int op, xWhile, nArray, array[], accumulator;
public static String strArray, strSeeN, newStringArray[];
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
do {
System.out.println("-----------------------------");
System.out.println("1: Add numbers | 2: Read numbers | 3: See existing number");
System.out.println("-----------------------------");
op = in.nextInt();
switch (op) {
case 1:
xWhile = 1;
accumulator;++;
System.out.println("Size of the array");
nArray = in.nextInt();
array = new int[nArray];
try {
for (int i = 0; i < nArray; i++) {
System.out.println("Enter the desired number");
array[i] = in.nextInt();
}
// Accumulator for when the operation is done more than 1 time, the array will not be null at the beginning, and when it is run a second time or more, don't override the strArray and act like a database, saving values until the program ends.
if (accumulator; == 1) {
strArray = "";
}
for (int i : array) {
strArray += "[" + i + "]";
}
//Save array
FileOutputStream saveFile = new FileOutputStream("DB.txt");
OutputStreamWriter outWrite = new OutputStreamWriter(saveFile);
outWrite.write(strArray);
outWrite.flush();
outWrite.close();
saveFile.close();
System.out.println("File saved.");
} catch (IOException e) {
}
break;
case 2:
xWhile = 1;
try {
//Open array
FileInputStream saveFile = new FileInputStream("DB.txt");
InputStreamReader inReader = new InputStreamReader(saveFile);
BufferedReader buffRead = new BufferedReader(inReader);
String strArray = buffRead.readLine();
buffRead.close();
inReader.close();
saveFile.close();
//Print array
newStringArray = strArray.split(",");
for (String i : newStringArray) {
System.out.println(i);
}
} catch (IOException e) {
}
break;
case 3:
xWhile = 1;
in.skip("\n");
System.out.println("Enter the number:");
strSeeN = in.nextLine();
Pattern pattern = Pattern.compile(strSeeN);
Matcher matcher = pattern.matcher(strArray);
while (matcher.find()) {
System.out.println("Number found: " + matcher.group());
}
break;
default:
xWhile = 0;
break;
}
} while (xWhile == 1);
}
}
超クール。:
そのそこに要素ではないので、newStringArrayは、あなたがオプション3.Youを選択するには、直接選択オプション1と第2の時間であれば、それはあなたのヌルポインタ例外がスローされます忘れてしまった file.Tryから、あなたのコンテンツを読むために読んで、それをして比較します。そしてまた、ファイルへの書き込みが追加時にコンマをそれ以外の値に分割する文句を言わない仕事を。
int index=0;
for (int i : array) {
if(index==nArray-1)
strArray += "[" + i + "]";
else
strArray += "[" + i + "],";
index++;
}
あなたのケースで次のように変更します。3
System.out.println("Input the number:");
strSeeNumber = in.nextLine();
try{
ileInputStream saveFile = new FileInputStream("DB.txt");
InputStreamReader inReader = new InputStreamReader(saveFile);
BufferedReader buffRead = new BufferedReader(inReader);
String strArray = buffRead.readLine();
buffRead.close();
inReader.close();
saveFile.close();//Print array
System.out.println(strArray);
newStringArray = strArray.replaceAll("\\[", "").replaceAll("\\]", "").replaceAll("\\s", "").split(",");
for (String newStringArray1 : newStringArray) {
System.out.println(newStringArray1);
if (newStringArray1.equals(strSeeNumber)) {
System.out.println("Found Number -[" + newStringArray1 + "]");
} else {
System.out.println("Number not found, find an existing number again.");
}
}
ここにあなたのコードをテスト-その作業:https://onlinegdb.com/rJ5LhsC8L