0

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
4
  • To increase i, you will need while(Character.isWhitespace... instead of while(!Character.isWhitespace... Commented May 1, 2014 at 21:23
  • Still giving me the same error with that changed. Commented May 1, 2014 at 21:25
  • That's probably because b and g are never increased. Add System.out.println(b); etc to see whats happening. Or better yet - use a debugger. Commented May 1, 2014 at 21:27
  • Use system.out.print statements in your loops to see how your code reacts. Commented May 1, 2014 at 21:32

2 Answers 2

1

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");
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for showing me this. Helped me better understand using the split method! Got some good notes on it now.
0

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.

1 Comment

Thanks for all the help, I ended up figuring it out. I guess I was just not in the right mind set at the time!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.