NathanH
NathanH

Reputation: 17

Error using Substring : String Index out of range : -1 *Java*

In this program I am just getting input from a file and trying to get the boys name and the girls name out of it, and also put them in separate files. I have done everything just as the book has stated. And I've also searched everywhere online for help with this but cant seem to find anyone with the same problem. Ive seen problems where its not -1 but a positive number because they went to far out of the string calling a substring over the strings length. But cant seem to figure out this giving me -1 since i's value is 1.

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
import java.util.ArrayList;


public class Homework_11_1 {

public static void main(String[] args)throws FileNotFoundException 
{
   File inputFile = new File("babynames.txt");
   Scanner in = new Scanner(inputFile);
   PrintWriter outBoys = new PrintWriter("boys.txt");
   PrintWriter outGirls = new PrintWriter("girls.txt");


   while (in.hasNextLine()){
    String line = in.nextLine();
    int i = 0;
    int b = 0;
    int g = 0;
    while(!Character.isWhitespace(line.charAt(i))){ i++; }
    while(Character.isLetter(line.charAt(b))){ b++; }
    while(Character.isLetter(line.charAt(g))){ g++; }
    String rank = line.substring(i);
    String boysNames = line.substring(i, b);
    String girlsNames = line.substring(b, g);
    outBoys.println(boysNames);
    outGirls.println(girlsNames);
    }

    in.close();
    outBoys.close();
    outGirls.close();
    System.out.println("Done");
}


}

Here is the txt file

    1 Jacob Sophia
    2 Mason Emma
    3 Ethan Isabella
    4 Noah Olivia
    5 William Ava
    6 Liam Emily
    7 Jayden Abigail
    8 Michael Mia
    9 Alexander Madison
    10 Aiden Elizabeth

Upvotes: 0

Views: 675

Answers (2)

Jakkra
Jakkra

Reputation: 651

I would have written it an other way, using split.

public static void main(String[] args)throws FileNotFoundException 
    {
       File inputFile = new File("babynames.txt");
       Scanner in = new Scanner(inputFile);
       PrintWriter outBoys = new PrintWriter("boys.txt");
       PrintWriter outGirls = new PrintWriter("girls.txt");


       while (in.hasNextLine()){
        String line = in.nextLine();
        String[] names = line.split(" "); // wile give you [nbr][boyName][GirlName]

        String boysNames = names[1];
        String girlsNames = names[2];
        outBoys.println(boysNames);
        outGirls.println(girlsNames);
        }

        in.close();
        outBoys.close();
        outGirls.close();
        System.out.println("Done");
    }

Upvotes: 1

mattarod
mattarod

Reputation: 380

Rather than fuss with loops and substring(), I'd just use String.split(" "). Of course, the assignment may not permit you to do this.

But anyway, without giving you the answer to the assignment, I can tell you that your logic is wrong. Walk through it and find out why. If you try running this code on just the first line of the input file, you'll get these values: i=1, b=0, and g=0. Calling line.substring(1,0) is obviously not going to work.

Upvotes: 0

Related Questions