Click here to Skip to main content
15,907,497 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Java
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
Updated 1-Dec-12 4:54am
v7
Comments
Richard MacCutchan 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 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 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 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 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

1 solution

Java
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;
}
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900