Parse string in Java [duplicate] - java

This question already has answers here:
string parsing in java
(4 answers)
Closed 7 years ago.
I have a string in below format
// JCSDL_MASTER b04591342ee71a2baa468d9d2a340ec8 AND
// JCSDL_VERSION 1.0
// JCSDL_START 0980a5f2ef935c4ed153bf975879eac0 twitter.text,contains_any,27-52
twitter.text contains_any "obama, santorum, gingrich, romney, ronpaul, ron paul"
// JCSDL_END
AND
// JCSDL_START f7c18a6fedd90c6b4d77acc14a3a8e5c interaction.type,in,21-29
interaction.type in "twitter,facebook,digg,youtube"
// JCSDL_END
// JCSDL_MASTER_END
I suppose it include newline character at the end, i need to just get only those line which is not being started by // how to get only those lines?

Pretty simply, just split up the string into individual lines (note: \n is an escape character for a line break), then only use each line if it does not start with //
String[] lines = string.split("\\n");
for (String line : lines)
{
if (!line.startsWith("//"))
{
//use the line and do your thing
}
}

Use Scanner.nextLine() to read each line and then Ignore the ones which starts with // like following:
String str = "hi \n//this is kuleep\nthis is a test class";
Scanner sc = new Scanner(str);
while (sc.hasNextLine()) {
String line = sc.nextLine();
if(!line.startsWith("//"))
{
System.out.println(line);
}
}

Related

java scanner next line throws but only with print statement [duplicate]

This question already has answers here:
using hasNextLine() but still getting java.util.NoSuchElementException: No line found
(5 answers)
Closed 5 years ago.
When I try to use java scanner it works and in my list I get all the text file content as a list.
But when I try to print within the while loop, it throws
java.util.NoSuchElementException: No line found
exception at the last line. Why would that be the case, wouldn't mylist also thrown had it been out of bound?
try {
Scanner myscanner = new Scanner(new FileReader(myfilepath));
while(myscanner.hasNextLine()){
//System.out.println(myscanner.nextLine() );
mylist.add(myscanner.nextLine());
numline += 1;
}
myscanner.close();
}
catch (Exception ex) {
ex.printStackTrace();
}
You check that you have next line and then when you print you read 2 lines.
You should change it to:
String line = myscanner.nextLine();
// print and add to list using line variable
To avoid that you should create variable to store the result fo myscanner.nextLine() then print it and add it to the list. e.g
while(myscanner.hasNextLine()){
String temp = myscanner.nextLine();
System.out.println(temp);
mylist.add(temp);
numline += 1;
}

Reading From File With Comma Delimeter Error

Here's the .txt file i'm trying to read from
20,Dan,09/05/1990,3,Here
5,Danezo,04/09/1990,99,There
And here's how I'm doing it.. Whenever the .txt file has only one line, it seems to be reading from file fine. Whenever more than one line is being read, I get this error
Exception in thread "main" java.lang.NumberFormatException: For input string: "Danezo"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:580)
at java.lang.Integer.parseInt(Integer.java:615)
at AttackMonitor.readFromFile(AttackMonitor.java:137)
at AttackMonitor.monitor(AttackMonitor.java:57)
at MonsterAttackDriver.main(MonsterAttackDriver.java:14)
Java Result: 1
Here's the readfromfile code.
private void readFromFile() throws FileNotFoundException, IOException
{
monsterAttacks.clear();
Scanner read = new Scanner(new File("Attacks.txt"));
read.useDelimiter(",");
String fullDateIn = "";
int attackIdIn = 0;
int attackVictimsIn = 0;
String monsterNameIn= "";
String attackLocationIn= "";
while (read.hasNext())
{
attackIdIn = Integer.parseInt(read.next());
monsterNameIn = read.next();
fullDateIn = read.next();
attackVictimsIn = Integer.parseInt(read.next());
attackLocationIn = read.next();
monsterAttacks.add(new MonsterAttack(fullDateIn, attackIdIn, attackVictimsIn, monsterNameIn, attackLocationIn));
}
read.close();
}
What is happening is that at the end of each line there is a newline character, which is currently not a delimiter. So your code is attempting to read it as the first integer of the next line, which it is not. This is causing the parse exception.
To remedy this, you can try adding newline to the list of delimiters for which to scan:
Scanner read = new Scanner(new File("Attacks.txt"));
read.useDelimiter("[,\r\n]+"); // use just \n on Linux
An alternative to this would be to just read in each entire line from the file and split on comma:
String[] parts = read.nextLine().split(",");
attackIdIn = Integer.parseInt(parts[0]);
monsterNameIn = parts[1];
fullDateIn = parts[2];
attackVictimsIn = Integer.parseInt(parts[3]);
attackLocationIn = parts[4];
You can use the Biegeleisen suggestion. Or else you can do as follows.
In your while loop you are using hasNext as condition. Instead of that you can use while (read.hasNextLine()) and get the nextLine inside the loop and then split it by your delimiter and do the processing. That would be a more appropriate approach.
e.g
while (read.hasNextLine()) {
String[] values = scanner.nextLine().split(".");
// do your rest of the logic
}
Put the while loop content in a try catch, and catch for NumberFormatException. So whenever it falls to catch code, you can understand you tried to convert a string to int.
Could help more if your business is explained.
attackLocationIn = read.next(); This value takes as "Here\n 5" because there is no comma between Here and 5 and it has new line character.
so 2nd iteration attackIdIn = Integer.parseInt(read.next()); here read.next() value is "Danezo" it is String and you are trying parse to Integer. That's why you are getting this exception.
What I suggest is use BufferReader to read line by line and split each line with comma. It will be fast also.
Or another solution Add comma at end of each line and use read.next().trim() in your code. That's it it will work with minimal changes to your current code.

Read a string from a file, skip the rest of the line after a certain character [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I am currently learning how to use Java. I am trying to read a file using the scanner class and I want to read the file but ignore the rest of the line after a certain character eg #. say the file reads
P5 #ignorethis
#ignorealso
123 123 123 #thisneedstogo
355 255 345 #alsothis goes
the file I am trying to read has comments after the symbol '#' and they last till the end of the line. I want to read the strings of the file, whilst ignoring '#' and everything after that.
Any help would be much appreciated, thanks :)
Read the file one line at a time and then consider using the replaceAll(String string) method which takes a regular expression on the line you have just read. You would then use something like so: #.*$ to replace the # character and whatever follows till the end of the string with an empty string.
You could then write the string back to some other file or console once that you are done.
From the Scanner's class doc:
A Scanner breaks its input into tokens using a delimiter pattern,
which by default matches whitespace.
You can do it using useDelimiter method and regular expressions
As an example:
public static void main(String[] args) {
String s = " P5 #ignorethis\n" +
" #ignorealso\n" +
" 123 123 123 #thisneedstogo\n" +
" 355 255 345 #alsothis goes";
Scanner scanner = new Scanner(s).useDelimiter("#.*");
while (scanner.hasNext()){
System.out.print(scanner.next());
}
}
You can write something like this:
Scanner scanner = new Scanner(new FileInputStream(new File("input.txt")));
while(scanner.hasNextLine()){
String str = scanner.nextLine();
System.out.println(str.substring(0, str.indexOf("#")));
}
scanner.close;
Hope this helps.
You can also read whole line, and then use just one part of it, like this:
public void readFile(File file){
String line = "";
try{
scanner = new Scanner(file);
}catch (FileNotFoundException ex){
ex.printStackTrace();
System.out.println("File not found");
}
while (scanner.hasNext()){
line = scanner.nextLine();
String parts[] = line.split("#");
System.out.println(parts[0]);
}
}
This method read new line to String, split a String in a place of "#", and use part before "#" occurrence. Here is output:
P5
123 123 123
355 255 345
BufferedReader br = new BufferedReader(new FileReader("name_file"))) {
String line;
String result = "";
while ((line = br.readLine()) != null) {
if(line.trim().startsWith("#"))
{
System.out.println("next line");
}
else
{
int index = line.indexOf("#");
if(index != -1){
String split = line.substring(0,index);
String[] sLine = line.trim().split("\t");
result = result + " " +split;
}
else
{
String[] sLine = line.trim().split("\t");
result = result + " " +line;
}
}
}
br.close();
System.out.println(result);

A simple string.split() gone horribly wrong [duplicate]

This question already has answers here:
Split string with | separator in java
(12 answers)
Closed 10 years ago.
This code seems to be broken but I can't tell why:
System.out.println(line);
// prints: Some Name;1|IN03PLF;IN02SDI;IN03MAP;IN02SDA;IN01ARC
String args[] = line.split("|");
String candidatArgs[] = args[0].split(";");
String inscrieriString[] = args[1].split(";");
System.out.println(args[0]);
System.out.println(args[1]);
System.out.println(candidatArgs);
System.out.println("[0]:" + candidatArgs[0]);
System.out.println("[1]:" + candidatArgs[1]);
// prints: S
// [Ljava.lang.String;#4f77e2b6
// [0]:
I have no idea why that happens. By my logic:
String args[] = line.split("|");
[0]: Some Name;1
[1]: IN02SDI;IN03MAP;IN02SDA;IN01ARC
Instead of:
[0]: S
In case you'd like more code: This should compile even if it doesn't do much (removed as much un-necessary code as I could)
Main:
Have a file: Candidati.txt
containing: Some Name;1|IN03PLF;IN02SDI;IN03MAP;IN02SDA;IN01ARC
import java.util.ArrayList;
Repository repository = new Repository ("Candidati.txt"); // file name
ArrayList<Candidat> candidati = repository.getCandidati();
System.out.println(candidati);
Repository
import java.util.ArrayList;
public class Repository {
private String fisierCuCandidati;
private ArrayList<Candidat> listaCandidati;
public Repository (String fisierCuCandidati) {
this.fisierCuCandidati = fisierCuCandidati; // file name
this.listaCandidati = new ArrayList<Candidat>();
this.incarcaCandidati();
}
public void incarcaCandidati() {
FileReader in = null;
BufferedReader input = null;
//try {
in = new FileReader (this.fisierCuCandidati);
input = new BufferedReader (in);
String line;
while ((line = input.readLine()) != null) {
System.out.println(line);
String args[] = line.split("|");
String candidatArgs[] = args[0].split(";");
String inscrieriString[] = args[1].split(";");
System.out.println(args[0]);
System.out.println(args[1]);
System.out.println(candidatArgs);
System.out.println("[0]:" + candidatArgs[0]);
System.out.println("[1]:" + candidatArgs[1]);
}
}
Candidat
public class Candidat {
public Candidat (String nume) {
}
public Candidat (String nume, int id) {
}
String.split uses a regular expression so you need to escape the pipe |, which is a special character (meaning OR):
String args[] = line.split("\\|");
Also to print the String array output rather the Object.toString representation, you will need:
System.out.println(Arrays.toString(candidatArgs));
You either need to escape the pipe or use it inside a character class in your split, as String#split takes a regex, and | is a meta character in regex. So, use this instead:
String args[] = line.split("\\|");
or:
String args[] = line.split("[|]");
The reason a character class works is because, inside a character class, te meta-characters have no special meaning. So, a pipe is just a pipe, and not an alternation character.
In addition to that, you should use Arrays#toString method to print your array.
change to:
String args[] = line.split("\\|");
your | won't work because the parameter of split is a regex, | has special meaning(or) in regex.
| is a special character in java regexes, so you need to escape it like \\|. I generally do
line.split(Pattern.quote(separator))
where Pattern is http://docs.oracle.com/javase/1.4.2/docs/api/java/util/regex/Pattern.html, and separator is whatever separator you use. This automatically takes care of escaping special characters.

Splitting String in case of a new line [duplicate]

This question already has answers here:
Split Java String by New Line
(21 answers)
Closed 6 years ago.
I have a method in java which returns the updated lines of a flat file as a "String". I am receiving the String as a bunch of lines, what i want to do is to separate the String line by line. How can i do that in java?????
I will suggest make use of system property line.separator for this splitting to make your code work on any platform eg: *nix, Windows, Mac etc. Consider code like this:
String str = "line1\nline2\nline3";
String eol = System.getProperty("line.separator");
System.out.printf("After Split - %s%n", Arrays.toString(str.split(eol)));
String lines[] = fileString.split("\\r?\\n"); //should handle unix or win newlines
I am not sure about with single string separator .
but i roughly write a method that may be relevant to you .
public void splitter (DocumentEvent e) {
String split[], docStr = null;
Document textAreaDoc = (Document)e.getDocument();
try {
docStr = textAreaDoc.getText(textAreaDoc.getStartPosition().getOffset(), textAreaDoc.getEndPosition().getOffset());
} catch (BadLocationException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
split = docStr.split("\\n");
}

Categories