Java i/o reading a text while hiding specific words - java

How would you read the following text while hiding the word SECRET each times it appears ?
here is the text :
this line has a secret word.
this line does not have a one.
this line has two secret words.
this line does not have any.
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
public class BufferedReadertest {
public static void main(String[] args) {
ArrayList <String>list = new ArrayList<>();
BufferedReader br = null;
try {
String sCurrentLine;
br = new BufferedReader(new FileReader("secretwords.txt"));
while ((sCurrentLine = br.readLine()) != null) {
list.add(sCurrentLine);
}
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}

Do you mean like
System.out.println(list.get(i).replaceAll("SECRET", "******");

I'm n seeing the need to use an ArrayList. Replace SECRET and print the message when iterating the buffered reader.
public static void main(String[] args) {
BufferedReader br = null;
try {
String sCurrentLine;
br = new BufferedReader(new FileReader("secretwords.txt"));
while ((sCurrentLine = br.readLine()) != null) {
System.out.println(sCurrentLine.replaceAll("SECRET", ""));
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}

if(line.indexOf("SECRET") > -1){
System.out.println("Has Secret");
continue;
}

Related

Why am I receiving this error 'illegal start of type'?

I am getting the error in the part where it says (final (String[] args)). It would be great if somebody knows why this error is occurring. Thanks in advance! I have included the code.
public class PartOne {
private static Object BufferedReader;
public static <String> void PartOne (final (String[] args))
{
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader("words-sowpods.txt"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
StringBuilder sb = new StringBuilder();
String line = br.readLine();
while (line != null) {
sb.append(line);
sb.append(System.lineSeparator());
line = br.readLine();
}
String everything = sb.toString();
} catch (IOException e) {
e.printStackTrace();
} finally {
br.close();
}
}
There are couple of things wrong here-
public static void PartOne (final (String[] args))
why u have making generic type as String even though is void
param is incorrect
Please find the correct implementation :
public static void PartOne(String[] args) {
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader("words-sowpods.txt"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
StringBuilder sb = new StringBuilder();
String line = br.readLine();
while (line != null) {
sb.append(line);
sb.append(System.lineSeparator());
line = br.readLine();
}
String everything = sb.toString();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

I am making the bible app and I want to print a particular chapter and highlight a particular line

I am trying to do same in Eclipse to print a text file and highlight a particular line, but am only able to read text file and not the line in it. Following is my code:
import java.io.*;
public class Bible {
public static void main(String[] args) {
try {
FileReader reader = new FileReader("temp.txt");
int character;
while ((character = reader.read()) != -1) {
System.out.print((char) character);
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Correct code to read a file line by line is
public static void main(String[] args) {
BufferedReader br = null;
FileReader fr = null;
try {
//br = new BufferedReader(new FileReader(FILENAME));
fr = new FileReader(FILENAME);
br = new BufferedReader(fr);
String sCurrentLine;
while ((sCurrentLine = br.readLine()) != null) {
System.out.println(sCurrentLine);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)
br.close();
if (fr != null)
fr.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
Now comes the code to highlight.
There are multiple options to do it.
Use html codes in file e.g.
origString = origString.replaceAll(textToHighlight,"<font color='red'>"+textToHighlight+"</font>");
Textview.setText(Html.fromHtml(origString));
Use spannable texts
String text = "Test";
Spannable spanText = Spannable.Factory.getInstance().newSpannable(text);
spanText.setSpan(new BackgroundColorSpan(0xFFFFFF00), 14, 19, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(spanText);
Use some third party library
EmphasisTextView and
Android TextView Link Builder

how can i put a filter on a CSV File stored in an Arraylist...Using Java

Iam reading a Csv file and want to put a filter on that arraylist in which the whole Csvfile is stored...
I'm new to Java Can anyone Correct me where m going wrong...
public static void main(String[] args) throws IOException {
String csvFile = "File.csv";
BufferedReader br = null;
String line ="";
ArrayList<CSVRead> alist=new ArrayList<CSVRead>();
try {
br = new BufferedReader(new FileReader(csvFile));
while((line = br.readLine()) != null)
{
String StoringArray[] = line.split(",");
CSVRead Cs1 = new CSVRead((StoringArray[0]),(StoringArray[1]),(StoringArray[2]),(StoringArray[3]),(StoringArray[4]),(StoringArray[5]), (StoringArray[6]), (StoringArray[7]),(StoringArray[8]),(StoringArray[9]),(StoringArray[10]),(StoringArray[11]),(StoringArray[12]),(StoringArray[13]), (StoringArray[14]),(StoringArray[15]),(StoringArray[16]),(StoringArray[17]));
alist.add(Cs1);
}
alist.forEach(Cs1 -> System.out.println("\t" + Cs1));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
catch(IOException e){
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}}
Try it
public static void main(String[] args) throws IOException {
String csvFile = "File.csv";
BufferedReader br = null;
String line ="";
ArrayList<String> alist=new ArrayList<String>();
try {
br = new BufferedReader(new FileReader(csvFile));
while((line = br.readLine()) != null)
{
String StoringArray[] = line.split(",");
for (String i : StoringArray){
alist.add(i);
}
}
System.out.println(alist); // print all list values
} catch (FileNotFoundException e) {
e.printStackTrace();
}
catch(IOException e){
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}}

How to read a txt file as my system.in using eclipse

I'm working through the problems on code chef. I'm stuck with a problem and all its says is that I have the wrong answer. I want to test my program to see its output but it reads input from a text file and I can't figure out how to do that with eclipse, my code is below:
import java.io.*;
class Holes {
public static void main(String[] args) throws IOException{
// TODO Auto-generated method stub
BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
int testCases = Integer.parseInt(r.readLine());
for (int i =0; i<testCases; i++)
{
int holes = 0;
String s = r.readLine();
for (int j= 0; j< s.length(); j++)
{
char c = s.charAt(j);
if (c == 'B')
holes += 2;
else if (c== 'A' || c== 'D' ||c== 'O' ||c== 'P' ||c== 'Q' ||c== 'R' )
{
holes +=1;
}
System.out.println(holes);
}
}
}
}
add folder to your eclipse project in that folder add your input file and then read it using BufferReader as follows
BufferedReader br = null;
try {
String sCurrentLine;
br = new BufferedReader(new FileReader("yourFolder/theinputfile.txt"));
while ((sCurrentLine = br.readLine()) != null) {
System.out.println(sCurrentLine);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
this is one way the other way is to pass the path as argument to your program
as it shown bellow
try {
String sCurrentLine;
br = new BufferedReader(new FileReader(args[0]));
while ((sCurrentLine = br.readLine()) != null) {
System.out.println(sCurrentLine);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
how to do that when your run the application do run configuration and there you will find args you can add what ever path in it for example c:\myinput.txt
hopefully this help
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class BufferedReaderExample {
public static void main(String[] args) {
BufferedReader br = null;
try {
String sCurrentLine;
br = new BufferedReader(new FileReader("C:\\testing.txt"));
while ((sCurrentLine = br.readLine()) != null) {
System.out.println(sCurrentLine);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}

How to check for valid sentence using index values in a file in Java

I have a following Begin and End indexes of sentences in a file.
Begin:1583 End:1631
Begin:2284 End:2324
Now i have to check whether these indexes are existed in a valid sentence by reading a particular file.I have tried with BreakIterator.But i did not understand how can i check the above indexes are existed in a valid sentence.
The code i have tried is
public class Breakiterator {
public static void main(String[] args) {
BufferedReader br = null;
String sCurrentLine = null;
String l=null;
try {
br = new BufferedReader(new FileReader("C:/test.txt "));
while ((sCurrentLine = br.readLine()) != null) {
BreakIterator i=BreakIterator.getSentenceInstance(Locale.GERMANY);
i.setText(sCurrentLine);
for(int s=i.first(), e=i.next(); e>=0; s=e, e= i.next())
{
System.out.println("Sentence: from "+s+" to "+e+" \""+sCurrentLine.substring(s, e)+'"');
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
Try this. Modify your regular expression as per your requirement
public static final String VALID_LINE_REG_EXP = "\\w+:\\d+.\\w+:\\d+";
public static void main(String args[]) {
BufferedReader br = null;
String sCurrentLine = null;
String l = null;
boolean isValid;
try {
br = new BufferedReader(new FileReader("D:/Shamse.txt "));
while ((sCurrentLine = br.readLine()) != null) {
System.out.println(sCurrentLine.trim().matches(VALID_LINE_REG_EXP));
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null) {
br.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
I have written this code assuming that your file will contains values in TEXT:NUMBER TEXT:NUMBER you can modify this regular expression as per your requirement

Categories