First of all this is a homework problem. I finished the program, and it runs fine; but I have a little side problem. The program is supposed to first read a txt file online, and gather all 50 states and their abbreviations. Next, I have to ask the user to enter in a valid state abbreviation. Once they do that, I have to read another txt file online, and return the two senators for their state.
The problem I'm having is that I want to loop the scanner asking the user to enter a state. If they enter an incorrect state, then I want to re-ask them. Right now, if I enter in a legit state, the program works fine. If I enter in an incorrect state, the program loops, and re-asks me to enter in another state. If I enter in an incorrect state, and then enter in a correct state, the program asks me to enter in another state, rather than continuing with the program. Any help please?
I think the problem is that when the program loops, it doesn't read:
if(stateAbbreviation.equals(abbreviation))
{
state = splitData[0];
abbrev = stateAbbreviation;
found = true;
break;
}
Again, making found always = 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();
}
}