Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8964135
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:31:05+00:00 2026-06-15T16:31:05+00:00

First of all this is a homework problem. I finished the program, and it

  • 0

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();
  }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T16:31:06+00:00Added an answer on June 15, 2026 at 4:31 pm

    You have two while and then you try to use a break to get out of it, but you’re just getting out of the inner while.

    To get out of both whiles, you need to change your program logic or use labels (yes, Java can use labels, and to break from an inner loop and of the outter loop at once is one of it’s uses)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First of all, this is not homework! :) Say I have an NSString: the?or?
First of all this is not 'homework', its a problem from Thinking in C++
First of all, this is a homework assignment. I'm trying to make some bubbleSort
First of all: This question is not directly programming related. However, the problem only
Maybe it's a similar beginning, but it's true. first of all sorry if this
First of all there is probably a question like this already but i couldn't
First of all this is not homework, I'm in a desperate need for a
First of all, this is not homework, so please don't tag it as homewrok
This is for uni homework, but I reckon it is more a generic problem
XXX----Yep, its a Homework problem, but yea I'm stuck. Why doesn't this print out

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.