まず、これは宿題の問題です。プログラムを終了しましたが、問題なく動作します。しかし、私には少し問題があります。このプログラムは、最初に txt ファイルをオンラインで読み取り、50 州すべてとその略語を収集することになっています。次に、有効な州の略語を入力するようにユーザーに依頼する必要があります。彼らがそれをしたら、オンラインで別の txt ファイルを読み込んで、2 人の議員の状態を返さなければなりません。
私が抱えている問題は、ユーザーに状態を入力するように求めるスキャナーをループしたいということです。彼らが間違った状態に入った場合、私は彼らに再質問したい. 現在、正当な状態に入ると、プログラムは正常に動作します。間違った状態に入ると、プログラムがループし、別の状態に入るように再度要求されます。間違った状態に入ってから正しい状態に入ると、プログラムを続行するのではなく、別の状態に入るように求められます。何か助けてください。
問題は、プログラムがループするときに読み取れないことだと思います。
if(stateAbbreviation.equals(abbreviation))
{
state = splitData[0];
abbrev = stateAbbreviation;
found = true;
break;
}
繰り返しますが、found を常に = false にします。
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Scanner;
public class Senator
{
static String abbrev;
static String state;
public static void main(String[] args) throws IOException
{
// define our URL
URL serviceURL = new URL("http://i5.nyu.edu/~cmk380/cs101/USStates.txt");
// connect and obtain data from this URL
Scanner input = new Scanner( serviceURL.openStream() );
Scanner input1 = new Scanner(System.in);
while(true)
{
boolean found = false;
//Ask for state
System.out.print("Enter state (abbreviation): ");
String stateAbbreviation = input1.next();
// read in our data
while ( input.hasNext() )
{
// grab a line
String data = input.nextLine();
// split up the line based on the position of the commas
String[] splitData = data.split(",");
// grab out the abbreviation
String abbreviation = splitData[1];
if(stateAbbreviation.equals(abbreviation))
{
state = splitData[0];
abbrev = stateAbbreviation;
found = true;
break;
}
}
if (found == true)
{
// close our URL
input.close();
break;
}
}
double numLines = 0;
// define our URL
URL senatorURL = new URL("http://www.contactingthecongress.org/downloadsContactingCongress.db.txt");
// connect and obtain data from this URL
Scanner input2 = new Scanner( senatorURL.openStream() );
// read in our data
while ( input2.hasNext() )
{
// grab a line
String data = input2.nextLine();
if (numLines != 0)
{
// split up the line based on the position of the commas
String[] splitData = data.split("\t");
if(splitData[0].equals(abbrev+"SR"))
{
System.out.println("");
System.out.println("State: " + state);
System.out.println("");
System.out.println("Senior Senator");
System.out.println("Name: " + splitData[1]);
//Try catch their information (exception raised if not found)
try
{
System.out.println("Address: " + splitData[3]);
}
catch (Exception e)
{
System.out.println("Addrress unavailable.");
}
try
{
System.out.println("Phone: " + splitData[4]);
}
catch (Exception e)
{
System.out.println("Phone unavailable.");
}
try
{
System.out.println("Website: " + splitData[7]);
}
catch (Exception e)
{
System.out.println("Website unavailable.");
}
System.out.println("");
}
if(splitData[0].equals(abbrev+"JR"))
{
System.out.println("Junior Senator");
System.out.println("Name: " + splitData[1]);
//Try catch their information (exception raised if not found)
try
{
System.out.println("Address: " + splitData[3]);
}
catch (Exception e)
{
System.out.println("Addrress unavailable.");
}
try
{
System.out.println("Phone: " + splitData[4]);
}
catch (Exception e)
{
System.out.println("Phone unavailable.");
}
try
{
System.out.println("Website: " + splitData[7]);
}
catch (Exception e)
{
System.out.println("Website unavailable.");
}
System.out.println("");
}
}
numLines ++;
}
// close our URL
input2.close();
}
}