Reputation: 147
I am trying to put a String into an array so I can print the tokens in a different order to how they are in the original file which I am reading from.
Below is the code I have so far, I have also included the input file I am reading from. What I would like to be able to do is print one word from the original file; system.out.println(tokens[4]); Which would print 'Species'
import java.util.Scanner;
public class inClassTest4Time {
public static void main(String[] args) {
Scanner scan = new
Scanner(inClassTest4Time.class.getResourcesAsStream("pet.txt"));
String line;
String[] tokens;
while (scan.hasNextLine())
{
line = (scan.nextLine());
tokens = line.split("//s");
for (int i = 0; i < tokens.length; i++) {
System.out.println(tokens[i]);
}
}
}
}
Input File:
Pet
===================
- species : String
+ isChipped : boolean
- name : String
- age : int
===================
+ Pet ( String name )
===================
Upvotes: 0
Views: 149
Reputation: 20129
I think you meant to put \\s
instead of //s
. //s
is actually splitting based on the literal string //s
(ie no escaping). Since none of your strings have that, there is no split. I suspect that if you do tokens[2]
you will get - species : String
.
Upvotes: 2