Hi need some help to separate my string.

I'm reading my text from file. The format is "name,1,2,3"

What i need to do is to seperate out the entire string to:

String name = "name";
String one = "1";
String two = "2";
String three = "3";

The commas are the seperator to the string.

I've tried to write a for loop to capture the text. but no good results so far.

Can anyone give me a heads up please?

Recommended Answers

All 2 Replies

read it using scanner
Scanner in = new Scanner(new File(fileName));
String name = in.next;
String one = in.next;
String two = in.next; .....

Thanks wildplace. I got the codes written and it works! Thank you so much!

import java.util.Scanner;
import java.io.*;
import java.io.File;
import java.io.FileNotFoundException;


    public class scanner
    {
        public static void main(String[] args) throws IOException
        {

			File key = new File("keyRing.txt");

			//System.out.println(test);
            Scanner scanner =  new Scanner(key).useDelimiter(",");

		//	String name = scanner.next();
		//	System.out.println(name);
			String name,p,g,u,temp;
			String nameToFind="ivan";
            while (scanner.hasNextLine()) {
               //int num = scanner.nextInt();

			//System.out.println(scanner.next());
			temp = scanner.next();
			if(nameToFind.equalsIgnoreCase(temp))
			{
				p = scanner.next();
				g = scanner.next();
				u = scanner.next();
				System.out.println("p: "+p);
				System.out.println("g: "+g);
				System.out.println("u: "+u);
			}

         }




      }//end main
   }//end class
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.