Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Nancy Melucci
Courses Plus Student 36,659 PointsSearching text file for target name, trying to avoid an infinite loop and print out integers.
I am working on an assignment where the user inputs a target name and Java searches a text file for the line that starts with that name and then prints out the rankings for a century of name popularity.
What I have posted here is an intermediate step, so I haven't dealt with the Scanner for user input. I am just trying to search the file for the name and print out the integers, and then skip the rest of the file.
I am still too new at this to figure out how to do that without triggering an infinite loop.
I am posting my code and a small portion of the text file. I am going to replace the awkward, non-DRY integer variables with a for-loop. But I need to fry this fish first.
Thanks for any guidance you can give me. I have been doing well in this course. This will be my first late assignment if I can't figure out how not to be such an idiot about while loops soon...><
/**
*
* @author Nancy
*/
import java.io.*;//file processing
import java.util.*;
//import java.util.Scanner;
public class FileInfo {
public static void main(String[] args)
throws FileNotFoundException, IOException {
Scanner baby = new Scanner(new File("babynames.txt"));
int decadeOne = 0, decadeTwo = 0;
String target = "Aaron";
while (baby.hasNext()){
if (!baby.hasNext(target)){
baby.next();
}
else if (baby.hasNextInt()){
// the String to int conversion happens here
decadeOne = baby.nextInt();
decadeTwo = baby.nextInt();
int decadeThree = baby.nextInt();
int decadeFour = baby.nextInt();
int decadeFive = baby.nextInt();
int decadeSix = baby.nextInt();
int decadeSeven = baby.nextInt();
int decadeEight = baby.nextInt();
int decadeNine = baby.nextInt();
int decadeTen = baby.nextInt();
int decadeEleven = baby.nextInt();
} if (baby.hasNext("\n"))
break;
System.out.println(decadeOne);
System.out.println(decadeTwo);
} System.out.println("End outer loop");
}
}
A 83 140 228 286 426 612 486 577 836 0 0
Aaliyah 0 0 0 0 0 0 0 0 0 380 215
Aaron 193 208 218 274 279 232 132 36 32 31 41
Abagail 0 0 0 0 0 0 0 0 0 0 958
Abbey 0 0 0 0 0 0 0 0 537 451 428
Abbie 431 552 742 924 0 0 0 0 752 644 601
Abbigail 0 0 0 0 0 0 0 0 0 953 562
Abby 0 0 0 0 0 906 782 548 233 211 209
Abdiel 0 0 0 0 0 0 0 0 0 925 721
3 Answers
Simon Coates
28,695 PointsMight try something like:
import java.io.*;//file processing
import java.util.*;
//import java.util.Scanner;
public class Reader {
public static void main(String[] args)
throws FileNotFoundException, IOException
{
Scanner baby = new Scanner(new File("babynames.txt"));
String target = "Aaron";
while (baby.hasNextLine()){
String ln = baby.nextLine();
System.out.println(ln);
String[] lineSegs = ln.split(" ");
if(lineSegs[0].equals(target)) {
System.out.println("Found");
//use indices to get ints.
break;
}
}
}
}
I think you're supposed to close a scanner at the end.
Nancy Melucci
Courses Plus Student 36,659 PointsBeautiful. Thank you so much. I may make it after all.
Jeremy Hill
29,567 PointsYou're welcome :)
Jeremy Hill
29,567 PointsJeremy Hill
29,567 PointsI would add another String declaration in there to put each word in that the Scanner reads. Like: