Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Java
im trying to read the strings from a txt file starting at char 7 and storing it in an array but am having some issues here is the code
public String[] readNames(String filename)
	{
String[] names = new String[5];
String currentLine = "";
int index = 0;
try
{
    //make a new scanner object that will read from the file
    Scanner in = new Scanner(new File(filename));
    
    //loop as long as the scanner still has contents (use the hasNext method)
    while(in.hasNext())
    {
        //if the current line has "Name" in it, add the name (starts at the 
        //7th character) to the array and increase the index
        if(in.hasNext("name"))
        {
            names[index] =in.next().charAt(6) ;
            index++;
        }
        //read the next line from the file
        currentLine = in.nextLine();
    }
Posted 30-Nov-12 14:36pm
Edited 1-Dec-12 5:54am
v7
Comments
Richard MacCutchan at 1-Dec-12 6:47am
   
Is this all your code? It looks like quite a lot is missing. I am also not sure that the Scanner class is what you are looking for, since it breaks the input into tokens.
diego14567 at 1-Dec-12 8:57am
   
no this is a method from the code but um having trouble with reading a a certain character from a text i already have the part that puts it into the array .for example i tried doing
names[index]= in.charAt(6) to start reading after the word "name" and store it all in the array then continue doing that line by line
Richard MacCutchan at 1-Dec-12 9:05am
   
That does not make much sense I am afraid. Are you trying to split each text line into its component words and store all words in your array? Or, are you trying to store the text from character 7 onwards, for all lines that start with "name"? There is a big difference between the two.
CPallini at 1-Dec-12 9:32am
   
It looks the class FileReader is more appropriate then the Scanner one for what you are trying to do.
diego14567 at 1-Dec-12 10:51am
   
okay lets say i have a txt file with the following input
name: joe
name: bill
name: jane
what im trying to do is start the read at char 7 if the line has "name" in it so for the first line it would begin at b and go to the end storing the name into the array then doing the same if the next line has "name" so when the program terminates i would have the following
joe
bill
jane
TorstenH. at 3-Dec-12 2:20am
   
You need to read the complete line and THEN split it.
You can use String.split(String) for that, which is much safer than to persist on a certian character count.

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

public String[] readNames(String filename)
{
    String[] names = new String[5];
    String currentLine = "", line = "";
    int index = 0;
	try
	{
            //make a new scanner object that will read from the file
            Scanner in = new Scanner(new File(filename));
    
            //loop as long as the scanner still has contents (use the hasNext method)
            while(in.hasNext())
            {
		//Read the line from file and store it in line variable
		line = in.nextLine();
		
		//Check whether line contains name in start
		if ( line.startsWith("name"))
                {
		
		//Extract string after word "name : " and store it in names array
			names[index] = line.substring(7);
			index++;
		}
            }
	}
	catch(IOException ex)
        {
		ex.printStackTrace();
        }
	
    return names;
}
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Suvendu Shekhar Giri 285
1 Sergey Alexandrovich Kryukov 245
2 OriginalGriff 210
3 PIEBALDconsult 180
4 Peter Leow 125
0 Sergey Alexandrovich Kryukov 6,425
1 OriginalGriff 5,918
2 Peter Leow 2,514
3 Maciej Los 2,263
4 Abhinav S 2,249


Advertise | Privacy | Mobile
Web04 | 2.8.150414.1 | Last Updated 8 Dec 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100