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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T14:25:58+00:00 2026-06-15T14:25:58+00:00

I have been going over this thing all day, line by line and I

  • 0

I have been going over this thing all day, line by line and I cannot figure out why I’m getting an empty String error. The program reads a text file and the output from the first part is correct, however, the second part gives me the empty String error.
Output is supposed to look like this –

COMMERICAL

FARM

LAND

RESIDENTIAL

101 600000.00

105 30000.00

106 200000.00

107 1040000.00

110 250000.00

Mine looks like this –

COMMERCIAL

FARM

LAND

RESIDENTIAL

java.lang.NumberFormatException: empty String
    at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1011)
    at java.lang.Double.parseDouble(Double.java:540)
    at my.report.agentReport.agentValue(agentReport.java:84)
    at my.report.agentReport.main(agentReport.java:41)

That block of errors repeats itself 7 times which happens to be the number of lines in the text file.
This is what the text file looks like –

110001 commercial 500000.00 101

110223 residential 100000.00 101

110020 commercial 1000000.00 107

110333 land 30000.00 105

110442 farm 200000.00 106

110421 land 40000.00 107

112352 residential 250000.00 110

I have tried more things than I can count in the past 2 days to no avail.
I’m running out of options. I’d really appreciate any help that anyone could give me.

Finally, here’s the code…

        package my.report;

import java.io.*;
import java.util.*;


public class agentReport {

public static void main(String[] args)
        throws FileNotFoundException {

    // get input from user (file name)
    Scanner console = new Scanner(System.in);
    System.out.print("Name of file to process: ");
    String inputFile = console.next();
    BufferedWriter pwfo = null;
    try {
        pwfo = new BufferedWriter(new FileWriter("C:\\agentReport.txt", true));
    } catch (IOException e) {
        e.printStackTrace();
    }
    PrintWriter pwo = new PrintWriter(pwfo);

    //Construct treeSet (property type)
    Set<String> propertyType = pType(inputFile);

    // Print property types 
    for (String type : propertyType) {
        System.out.println(type);
        pwo.println(type);
    }

    //Construct treeSet (agent IDs and values) 
    Set<String> agentReport = agentValue(inputFile);

    // Print agent IDs and values 
    for (String tail : agentReport) {
        {
            System.out.println(tail);
            pwo.println(tail);
        }
    }
    pwo.flush();
    pwo.close();
}

// read input and alphabetized property types in uppercase
public static Set<String> pType(String inputFile) throws FileNotFoundException //Construct treeSet to return property types
{
    Set<String> type = new TreeSet<>();
    Scanner in = new Scanner(new File(inputFile));

    // use while loop and delimiter to select specific characters for set
    in.useDelimiter("[1234567890. ]");

    while (in.hasNext()) {
        type.add(in.next().toUpperCase());
    }
    in.close();
    return type;
}

//  read file and print out agent ID's and property values
public static Set<String> agentValue(String inputFile)
        throws FileNotFoundException {

    TreeSet<String> tail = new TreeSet<>();
    SortedMap<String, Number> agentValue = new TreeMap<>();
    Scanner in = new Scanner(new File(inputFile));
    String line;

    while (in.hasNextLine()) {
        try {
            line = in.nextLine();
            String[] fields = line.split("[\\s+]");
            String agentId = (fields[3]);
            Double pValue = Double.parseDouble(fields[2]);

            if (agentValue.containsKey(agentId)) {
                pValue += agentValue.get(agentId).doubleValue();
            }
            agentValue.put(agentId, pValue);

            // Create keyMap with keys and values

        } catch (Exception e) {
            e.printStackTrace();
        }
        Set<String> keySet = agentValue.keySet();
        for (String key : keySet) {
            Number value = agentValue.get(key);
            System.out.println(key + ":" + value);
            tail.add(key + ":" + value);
        }
    }
    return tail;
}
}
  • 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-15T14:25:59+00:00Added an answer on June 15, 2026 at 2:25 pm

    In your pType method, you have used certain delimiters for scanner: –

    in.useDelimiter("[1234567890. ]");
    

    Now, when you read this line: –

    110001 commercial 500000.00 101
    

    You will get an empty string between the each numeric value. Which will give you some problems, if you don’t pre-check it.

    See this sample code for e.g: –

    String str = "110001 commercial 500000.00 101";
    Scanner scanner = new Scanner(str);
    scanner.useDelimiter("[0-9. ]");
    
    while (scanner.hasNext()) {
        System.out.print("*" + scanner.next() + "*-");
    }
    

    Output: –

    **-**-**-**-**-**-*commercial*-**-**-**-**-**-**-**-**-**-**-**-**-**-
    

    As you can see those pairs of **, which denotes the empty string read between each numeric values.

    You can add a test for empty string inside the while loop, to avoid adding it: –

    String line = "";
    while (in.hasNext()) {
        if (!(line = in.next()).isEmpty()) {
            type.add(line.toUpperCase());
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been going over this for hours and cannot find the problem. I
I have been going mad trying to figure out why my scripts weren't working,
I've been going around in circles for hours with this. I have a UserProfile
I have been tweaking my program all day and I am having a problem
This question is long winded because I have been updating the question over a
I have been pulling my hair out trying to solve this. What I am
I've been searching all over the place and even if I have found the
I have been going crazy over a problem with my tab control. It has
Quick question about WordPress. I've been Googling all over the place and cannot find
I have been going through the core jQuery code and had a few why

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.