私が取り組んでいるプロジェクト用にこのJavaコードを書きました。できるところまで行ったのですが、まだうまくいかないので、ここで試してみようと思いました。
このプログラムは、 a を使用しJOptionPane
て、どの都市からどの都市までの走行距離を表示したいかをユーザーに尋ねることになっています。これらの数値は .txt ファイルから取得されます。
「j」が見つからないというエラーが常に発生します。
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
import javax.swing.JOptionPane;
public class Mileage
{
public static void main(String[] paramArrayOfString)
throws IOException
{
String str3 = "";
Scanner localScanner = new Scanner(new File(JOptionPane.showInputDialog("Enter the name of the data file.")));
String str1 = localScanner.nextLine() + " Cities:\n\n";
int i = localScanner.nextInt();
ArrayList localArrayList = new ArrayList(i);
int[][] arrayOfInt = new int[i][i];
for (int j = 0; j < i; j++)
{
localScanner.nextLine();
localArrayList.add(localScanner.nextLine());
for (int k = 0; k < i; k++)
{
arrayOfInt[j][k] = localScanner.nextInt();
}
}
j = 1 ;
for (String str4 : localArrayList)
{
str1 = str1 + j++ + ". " + str4 + "\n"; str1 = str1 + "\nEnter from and to cities for mileage, \"Quit\" or \"quit\" to quit.";
String str2;
}
do
{
str2 = JOptionPane.showInputDialog(str3 + str1);
if (str2.equalsIgnoreCase("quit"))
{
continue;
}
localScanner = new Scanner(str2);
int m = localScanner.nextInt();
int n = localScanner.nextInt();
if ((m < 1) || (m > i) || (n < 1) || (n > i))
str3 = "Invalid values, try again.\n\n";
else {
str3 = "The distance between " + (String)localArrayList.get(m - 1) + " and " + (String)localArrayList.get(n - 1) + " is " + arrayOfInt[(m - 1)][(n - 1)] + " miles.\n\n";
}
}
while (!str2.equalsIgnoreCase("quit"));
}
}
ここに私が試した別のフォームがありますが、ほとんど違いはありませんが、両方とも同じエラーがあります:
import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Scanner;
import javax.swing.JOptionPane;
public class MileageTest
{
public static void main(String[] paramArrayOfString)
throws IOException
{
String str5 = "";
String str1 = JOptionPane.showInputDialog("Enter the name of the data file.");
Scanner localScanner = new Scanner(new File(str1));
String str2 = localScanner.nextLine();
int i = localScanner.nextInt();
ArrayList localArrayList = new ArrayList(i);
int[][] arrayOfInt = new int[i][i];
for (int j = 0; j < i; j++)
{
localScanner.nextLine();
localArrayList.add(localScanner.nextLine());
for (int k = 0; k < i; k++)
{
arrayOfInt[j][k] = localScanner.nextInt();
}
}
String str3 = str2 + " Cities:\n\n";
j = 1;
for (String str6 : localArrayList)
str3 = str3 + j++ + ". " + str6 + "\n";
str3 = str3 + "\nEnter from and to cities for mileage, \"Quit\" or \"quit\" to quit.";
String str4;
do {
str4 = JOptionPane.showInputDialog(str5 + str3);
if (str4.equalsIgnoreCase("quit"))
continue;
localScanner = new Scanner(str4);
int m = localScanner.nextInt();
int n = localScanner.nextInt();
if ((m < 1) || (m > i) || (n < 1) || (n > i))
str5 = "Invalid values, try again.\n\n";
else
{
str5 = "The distance between " + (String)localArrayList.get(m - 1) + " and " + (String)localArrayList.get(n - 1) + " is " + arrayOfInt[(m - 1)][(n - 1)] + " miles.\n\n";
}
}
while (!str4.equalsIgnoreCase("quit"));
test();
}
private static void test()
{
System.out.println("Testing...");
}
}