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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:04:46+00:00 2026-05-28T05:04:46+00:00

I am doing a homework assignment that deals with classes and objects. In it,

  • 0

I am doing a homework assignment that deals with classes and objects. In it, I have an Address class, Letter class, and PostOffice class that when calling PostOffice, takes an input file and scans through it, printing basically everything in the input file the way it should appear on a letter (to: bla, from: bla, postage amount, to and from address, etc.)

i get an error that reads:

Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Scanner.java:1516)
at PostOffice.readLetters(PostOffice.java:33)
at PostOffice.main(PostOffice.java:14)

and I dont really understand why….

here is my post office class:

import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

public class PostOffice {

private final int MAX = 1000;
private Letter [] letterArray = new Letter[MAX];
private int count;

public static void main(String [] args) {
    PostOffice postOffice = new PostOffice();
    postOffice.readLetters("letters.in");
    postOffice.sortLetters();
    postOffice.printLetters();
}

public PostOffice() {
    Letter [] myLetters = letterArray;
    this.count = 0;
}

public void readLetters(String filename) {
    String toName, toStreet, toCity, toState, toZip;
    String fromName, fromStreet, fromCity, fromState, fromZip, temp; //, weight;
    double weight;
    int index;
    Scanner s = new Scanner(filename);
    if (s != null) {
        while(s.hasNext()){
            toName = s.nextLine();
            toStreet = s.nextLine();
            temp = s.nextLine();
            index = temp.indexOf(",");
            toCity = temp.substring (0, index);
            index = index + 2;
            toState = temp.substring (index, index + 2);
            toZip = temp.substring (index);
            fromName = s.nextLine();
            fromStreet = s.nextLine();
            temp = s.nextLine();
            index = temp.indexOf(",");
            fromCity = temp.substring (0, index);
            index = index + 2;
            fromState = temp.substring (index, index + 2);
            fromZip = temp.substring (index);
            String var = s.nextLine();
            weight = Double.parseDouble(var);
            //weight = s.nextLine();
            Letter l = new Letter(toName, toStreet, toCity, toState, toZip, fromName, fromStreet, fromCity, fromState, fromZip, weight);
            this.count += 1;
            this.letterArray[count - 1] = l;
        }
    }
    s.close();
}

public static void sortLetters() {
//call sortSearchUtil This method should call the compareTo method provided by the Letter class to sort.  
//You may use any sorting routine you wish (see SortSearchUtil.java)
}

public static void printLetters() {
//call tostring of letter class. print the count of Letters followed by the total postage followed 
//by each Letter (make sure you use the toString method provided by the Address and Letter classes for this)
}

}

my letter class:

    public class Letter extends PostOffice implements Comparable<Letter> {
private static final double POSTAGE_RATE = 0.46;
private String fromName;
private Address fromAddr;
private String toName;
private Address toAddr;
private double weight;


    public Letter (String fromName, String fromStreet, String fromCity, String fromState,       String fromZip, String toName, 
String toStreet, String toCity, String toState, String toZip, double weight) {
this.fromName = fromName;
this.fromAddr = new Address(fromStreet, fromCity, fromState, fromZip);
this.toName = toName;
this.toAddr = new Address(toStreet, toCity, toState, toZip);
this.weight = weight;   
}

public String toString() {
String result;
result = String.format("from: %s\t\t\t%5.2f\n%s", fromName, getPostage(weight), fromAddr);
result = result + String.format("\t\t To: %s\n\t\t%s", toName, toAddr);
return result;
}

    public int compareTo(Letter that) {
int value;
value = this.toAddr.getZip().compareTo(that.toAddr.getZip());
return value;
}


public static double getPostage(double weight) {
double workWeight;
workWeight = weight + 0.999;
workWeight = (int)workWeight;   
return workWeight * POSTAGE_RATE;
    } 
}

and my address class:

import java.awt.*;
import java.util.*;

public class Address {
private String street;
private String city;
private String state;
private String zip;

    public Address (String street, String city, String state, String zip) {
    this.street = street;
    this.city = city;
    this.state = state;
    this.zip = zip;
    }

    public String getStreet() {
    return street;
    }

    public void setStreet(String street) {
    this.street = street;
    }

    public String getCity() {
    return city;
    }

    public void setCity(String city) {
    this.city = city;
    }

    public String getState() {
    return state;
    }

    public void setState(String state) {
    this.state = state;
    }

    public String getZip() {
    return zip;
    }

    public void setZip(String zip) {
    this.zip = zip;
    }

    public String toString() {
    String result;
    result = String.format("%s\n%s, %s %s", street, city, state, zip);
    return result;  
    }
}

and this is the content of the text file

Stu Steiner (NEW LINE)
123 Slacker Lane (NEW LINE)
Slackerville, IL  09035 (NEW LINE)
Tom Capaul(NEW LINE)
999 Computer Nerd Court (NEW LINE)
Dweebsville, NC  28804-1359 (NEW LINE)
0.50 (NEW LINE)
Tom Capaul (NEW LINE)
999 Computer Nerd Court (NEW LINE)
Dweebsville, NC  28804-1359 (NEW LINE)
Chris Peters (NEW LINE)
123 Some St. (NEW LINE)
Anytown, CA  92111-0389 (NEW LINE)
1.55 (NEW LINE)

Everything compiles, I just need it to output like this:

----------------------------------------------------------------------------

From: From value                                              Postage value
From Address value (be sure and use Address toString)

                    To: To value
                    To Address value (be sure and use Address toString)

----------------------------------------------------------------------------
  • 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-05-28T05:04:47+00:00Added an answer on May 28, 2026 at 5:04 am

    You do s.readline() many times inside 1 iteration of while loop. This is why you’re getting the error.

    You need to call readLine() only once inside your while loop

    Example in your code:

    while(s.hasNext()){
            toName = s.nextLine();
            toStreet = s.nextLine();
            temp = s.nextLine();
            // ...
        }
    

    These are 3 calls to nextLine, how are you sure there are still lines?

    SOLUTION:

    put an if statement before every s.nextLine() except the first one.
    Example:

    while(s.hasNext()){
         toName = s.nextLine();
         if (s.hasNext()) {
             toStreet = s.nextLine();
         }
         if (s.hasNext()) {
             temp = s.nextLine();
         }
         // ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm doing a homework assignment in C. I have to build a calculator that
I'm doing a basic homework assignment which looks like this: While input <> -1
I am doing a college assignment in Java that deals with currency. For that
This is a homework problem that I have. I have been doing some research
I am doing a homework assignment that reads in a book. First, a line
I am doing a homework assignment that involves writing a program to draw shapes
So in my java class we have a homework assignment to use System.currentTimeMillis to
I'm doing some homework and while I have some experience with SML, Haskell has
I'm doing a homework project that requires this: Below you will find the code
I'm doing a homework assignment in which I need to print out to the

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.