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 8411057
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T00:20:39+00:00 2026-06-10T00:20:39+00:00

Im working on the question below and am quite close but in line 19

  • 0

Im working on the question below and am quite close but in line 19 and 32 I get the following error and cant figure it out.

foreach not applicable to expression type
for (String place: s)

Question:

Tax inspectors have available to them two text files, called unemployed.txt and taxpayers.txt, respectively. Each file contains a collection of names, one name per line. The inspectors regard anyone who occurs in both files as a dodgy character. Write a program which prints the names of the dodgy characters. Make good use of Java’s support for sets.

My code:

class Dodgy {

public static void main(String[] args) {

    HashSet<String> hs = new HashSet<String>();

    Scanner sc1 = null;
try {sc1 = new Scanner(new File("taxpayers.txt"));} 
        catch(FileNotFoundException e){};
while (sc1.hasNextLine()) {
    String line = sc1.nextLine();
    String s = line;
    for (String place: s) {
        if((hs.contains(place))==true){
            System.out.println(place + " is a dodgy character.");
    hs.add(place);}
    }   
}

    Scanner sc2 = null;
try {sc2 = new Scanner(new File("unemployed.txt"));} 
        catch(FileNotFoundException e){};
while (sc2.hasNextLine()) {
    String line = sc2.nextLine();
    String s = line;
    for (String place: s) {
        if((hs.contains(place))==true){
            System.out.println(place + " is a dodgy character.");
    hs.add(place);}
}
}
}
}
  • 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-10T00:20:40+00:00Added an answer on June 10, 2026 at 12:20 am

    You’re trying to iterate over “each string within a string” – what does that even mean?

    It feels like you only need to iterate over each line in each file… you don’t need to iterate within a line.

    Secondly – in your first loop, you’re only looking at the first file, so how could you possibly detect dodgy characters?

    I would consider abstracting the problem to:

    • Write a method to read a file and populate a hash set.
    • Call that method twice to create two sets, then find the intersection.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

UPDATE I managed to get it working although I'm not quite sure why ;)
Problem solved, see below Question I'm working in Flex Builder 3 and I have
Have been working on this question for a couple hours and have come close
Followed this question about delayed_job and monit Its working on my development machine. But
this may be a stupid question, but I've been doing Android Development for quite
I'm not quite sure where to start with all of this, but im assuming
While working on this question , I noticed that GCC (v4.7)'s implementation of std::function
Referring to question Spring Autowiring stopped working on GAE I ask: why doesn't AppEngine
In working through the issues with another question, I've found text files with embedded
I got question about log4net is it working on IIS 7,5? I read lot

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.