0

I am trying to create a system which allows the hiring of bikes. I have managed to get the other parts of the system to work, but what I am having trouble with is trying to get the hiring part of the system in place. This is my first main programming project and would like to see the system fully running. All help would be appreciated.

    Exception in thread "main" java.util.NoSuchElementException: No line found
at java.util.Scanner.nextLine(Scanner.java:1540)
at bikehire.textdao.TextBikeDAO.loadBikes(TextBikeDAO.java:46)
at bikehire.BikeHireIncrement1.loadCars(BikeHireIncrement1.java:122)
at bikehire.BikeHireIncrement1.main(BikeHireIncrement1.java:35)

This is the error I am getting when trying to run the main class for the bike hire function. Below is the TextBikeDAO and BikeHireIncrement1.

    @Override
public List<Bike> loadBikes(Path path) {
    List<Bike> bikes = new ArrayList<>(); 
    try (Scanner s = new Scanner (new BufferedReader(new FileReader(path.toString())))) {
        s.useDelimiter(Character.toString(DELIMITER));
        Bike b;
        int bikeId;
        String bikeBrand, bikeType;
        float dailyCost = 0;
        while (s.hasNext()) {
            if (s.hasNextInt()) {
                bikeId = s.nextInt();
            }
            else {
                bikeId = 0;
            }
            bikeBrand = s.next();
            bikeType = s.nextLine();
            b = new Bike(bikeId, bikeBrand, bikeType, dailyCost);
            bikes.add(b);
    }
        s.close();
        } catch (FileNotFoundException ex) {
                Logger.getLogger(TextBikeDAO.class.getName()).log(Level.SEVERE, null, ex);
                }

    return bikes;
}

\ 's.nextline();' is the code being highlighted for the error.

The other errors are my main class BikeHireIncrement1.

public static void main(String[] args) {

    Scanner userInput = new Scanner (System.in);
    char choice;

    List<Customer> customers=loadCustomers(); 
    List<Bike> bikes=loadBikes();

The List bikes=loadBikes(); is being identified as a fault.

    public static List<Bike> loadBikes() {
    List<Bike> bikess;
    Path path=Paths.get("bikes.txt");

    TextDAOFactory daoFactory = (TextDAOFactory) DAOFactory.getDAOFactory(1);
    TextBikeDAO dao = (TextBikeDAO) daoFactory.getBikeDAO();
    bikes = dao.loadbikes(path);  
    return bikes;
}    

and the bikes = dao.loadbikes(path); is also being highlighted.

Again, any help at all would be very much appreciated as this is my first attempt at completing a programming application. I am really lost with regards to what I have done wrong as everything has compiled without any faults.

Thanks!

EDIT*** I have ensure that the s.nextline is going for another value but still seem to get the same error.

heath123
  • 1
  • 2
  • **Also I can appreciate that more than 1 fault may occur outside of the question which I am asking, but to get the system running is my main priority. Thanks again!** – heath123 Aug 16 '16 at 18:48
  • You need to look at the input file your scanner is processing. The exception is telling you there is no further input at the point you are calling nextLine (see: https://docs.oracle.com/javase/7/docs/api/java/util/Scanner.html#nextLine()). Try breaking this question down to just the first question and post the input file you are processing. – Malcolm Smith Aug 16 '16 at 22:52

0 Answers0