reading next lines from a file after particular pattern [closed] - java

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have to read a file if some pattern occurs that file i have to read next lines of the text file in java
while (sc1.hasNextLine()){
String a = sc1.nextLine();
String b = sc1.nextLine();
if(a.contains("pattern")){
//read next lines
}
}

To simply read the file to find the line you want, just use a loop until you reach the end or the pattern your want like this :
while(sc1.hastNextLine() && !sc.nextLine().contains("pattern")){
//skip
}
then you just have to check if there is still a line to read
if (sc1.hastNextLine()){
//read the line
}

Related

last two occurance of semicolon(;) in given String using Java [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
How do I get the after last two semicolon's occurrences of a string ?
Example:
Mobiles;Students;Test;Yes;1234
The output should be
Yes;1234
Using a regex replacement, we can try:
String input = "Mobiles;Students;Test;Yes;1234";
String output = input.replaceAll("^.*;([^;]+;[^;]+)$", "$1");
System.out.println(output); // Yes;1234

How do you read a integer and a string seperatly from a single line in file and store them to variables? [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 2 years ago.
Improve this question
I have a text file that looks something like this:
1 Song one
2 Song two
3 Song three
...
How do I read a line by line and extract number and String separately from a single line, and let's say print them to the console with:
String title; // extracted from current line
int num; // extracted from current line
System.out.println("Number: " + num + "Title: " + title);
How do I read a line by line
Use a BufferedReader and its readLine() method.
How do I extract number and String separately
Use line.indexOf(' ') then substring(...) to get the 2 parts.
Then use Integer.parseInt(...) on the first part to get number.

Assigning a file name to a variable in java [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 5 years ago.
Improve this question
I want to simply assign the file name "database.txt" to a string type variable say "fname".
What is the code for it for java.
I want to use relative file destinations.
try something like this :
File file = new File("your/path");
String str = file.getName();

Write java array to file.txt in one line [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
What should I change in my code to write my array into text file in a single line instead of a different line per vale of the array?
PrintWriter zapis = new PrintWriter("wyniki.txt");
for (int g=1; g<801; g++)
{
zapis.println(g+":"+wynik[g]+" ");
}
zapis.close();
As the documentation states, println "Prints a String and then terminates the line."
If you don't want to terminate the line, use print instead.
Change the line inside for loop like this :
zapis.print(g+":"+wynik[g]+" ");

Changing the Lengths of Words in a String - Java [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I am currently writing a game in Java, where words in a string will have to be all changed to equal 5 characters a word.
eg. I am writing in Java
Iamwr iting inJav a
I wonder if anyone knows how I would do this?
First remove spaces between words.
Then split that resulting String to length with 5.
Add a space after every 5 character.

Categories

Resources