Remove all java keywords from a file - java

I am going through a project where I need to remove all java keywords from a java file. First I create a keyword.java file and store all java keywords into this file.Like abstract continue for new switch assert default goto package etc which I store keyword.java file. I have another file named newFile.java and I read all data from newFile.java as a String. I have to remove all java keywords from newFile.java file. As far I tried:
public void processFile() throws IOException {
String data = "";
data = new String(Files.readAllBytes(Paths.get("H:\\java\\Clone\\newFile.java"))).trim();
String rmvPunctuation = removePunctuation(data);
String newLineRemove = rmvPunctuation.replace("\n", "").replace("\r", "");
String spaceRemove = newLineRemove.replaceAll("( ){2,}", " ");
removeKeyword(spaceRemove);}
public void removeKeyword(String fileAsString) throws FileNotFoundException, IOException {
ArrayList<String> keyWordList = new ArrayList<>();
ArrayList<String> methodContentList = new ArrayList<>();
FileInputStream fis = new FileInputStream("H:\\java\\keyword.java");
byte[] b = new byte[fis.available()];
fis.read(b);
String[] keyword = new String(b).trim().split(" ");
String newString = "";
for (int i = 0; i < keyword.length; i++) {
keyWordList.add(keyword[i].trim());
}
String[] p = fileAsString.split(" ");
for (int i = 0; i < p.length; i++) {
if (!(keyWordList.contains(p[i].trim()))) {
newString = newString + p[i] + " ";
}
}
System.out.println("" + newString);
}
But I could not found my desired output. All the java keywords are not removed from newFile.java file. I think StackOverflow community help me to solve this. I am also a beginner.
I also tried:
public void removeKeyword(String fileAsString) throws IOException {
String keyWord = new String(Files.readAllBytes(Paths.get("H:\\java\\keyword.java"))).trim();
String text = fileAsString.trim();
ArrayList<String> wordList = new ArrayList<>();
ArrayList<String> keyWordList = new ArrayList<>();
wordList.addAll(Arrays.asList(text.split(" ")));
keyWordList.addAll(Arrays.asList(keyWord.split(" ")));
wordList.removeAll(keyWordList);
System.out.println("" + wordList.toString());
}

Related

How to find similar lines in two text files irrespective of the line number at which they occur

I am trying to open two text files and find similar lines in them.
My code is correctly reading all the lines from both the text files.
I have used nested for loops to compare line1 of first text file with all lines of second text file and so on.
However, it is only detecting similar lines which have same line number,
(eg. line 1 of txt1 is cc cc cc and line 1 of txt2 is cc cc cc, then it correctly finds and prints it),
but it doesn't detect same lines on different line numbers in those files.
import java.io.*;
import java.util.*;
public class FeatureSelection500 {
public static void main(String[] args) throws FileNotFoundException, IOException {
// TODO code application logic here
File f1 = new File("E://implementation1/practise/ComUpdatusPS.exe.hex-04-ngrams-Freq.txt");
File f2 = new File("E://implementation1/practise/top-300features.txt");
Scanner scan1 = new Scanner(f1);
Scanner scan2 = new Scanner(f2);
int i = 1;
List<String> txtFileOne = new ArrayList<String>();
List<String> txtFileTwo = new ArrayList<String>();
while (scan1.hasNext()) {
txtFileOne.add(scan1.nextLine());
}
while (scan2.hasNext())
{
txtFileTwo.add(scan2.nextLine());
}
/*
for(String ot : txtFileTwo )
{
for (String outPut : txtFileOne)
{
// if (txtFileTwo.contains(outPut))
if(outPut.equals(ot))
{
System.out.print(i + " ");
System.out.println(outPut);
i++;
}
}
}
*/
for (int j = 0; j < txtFileTwo.size(); j++) {
String fsl = txtFileTwo.get(j);
// System.out.println(fileContentSingleLine);
for (int z = 0; z < 600; z++) // z < txtFileOne.size()
{
String s = txtFileOne.get(z);
// System.out.println(fsl+"\t \t"+ s);
if (fsl.equals(s)) {
System.out.println(fsl + "\t \t" + s);
// my line
// System.out.println(fsl);
} else {
continue;
}
}
}
}
}
I made your code look nicer, you're welcome :)
Anyway, I don't understand that you get that bug. It runs through all of the list2 for every line in the list1...
import java.io.*;
import java.util.*;
public class FeatureSelection500 {
public static void main(String[] args) throws FileNotFoundException, IOException {
// TODO code application logic here
File file1 = new File("E://implementation1/practise/ComUpdatusPS.exe.hex-04-ngrams-Freq.txt");
File file2 = new File("E://implementation1/practise/top-300features.txt");
Scanner scan1 = new Scanner(file1);
Scanner scan2 = new Scanner(file2);
List<String> txtFile1 = new ArrayList<String>();
List<String> txtFile2 = new ArrayList<String>();
while (scan1.hasNext()) {
txtFile1.add(scan1.nextLine());
}
while (scan2.hasNext()) {
txtFile2.add(scan2.nextLine());
}
for (int i = 0; i < txtFile2.size(); i++) {
String lineI = txtFile2.get(i);
// System.out.println(fileContentSingleLine);
for (int j = 0; j < txtFile1.size(); j++){ // z < txtFileOne.size(
String lineJ = txtFile1.get(j);
// System.out.println(fsl+"\t \t"+ s);
if (lineI.equals(lineJ)) {
System.out.println(lineI + "\t \t" + lineJ);
// my line
// System.out.println(fsl);
}
}
}
}
}
I don't see any problem with your code. Even the block you commented is absolutely fine. Since, you are doing equals() you should make sure that you have same text (same case) in the two files for them to be able to satisfy the condition successfully.
for(String ot : txtFileTwo )
{
for (String outPut : txtFileOne)
{
if(outPut.equals(ot)) /* Check Here */
{
/* Please note that here i will not give you line number,
it will just tell you the number of matches in the two files */
System.out.print(i + " ");
System.out.println(outPut);
i++;
}
}
}

Add element in a ArrayList

I am trying to add a new element to an array list but when I print the structure I get the adress in memory. Any idea?
I read the information from a file and I try to put it in a structure Assignatures which is an in numAssignatures and one ArrayList and assignatura has an string with the name and one integer.
public static void llegeixFitxer(Curs[] curs) throws IOException {
FileReader file = new FileReader("assignatures.txt");
BufferedReader reader = new BufferedReader(file);
for (int j=0; j< 5; j++){
curs[j] = new Curs();
curs[j].numAssignatures = Integer.parseInt(reader.readLine());
for (int i = 0; i<curs[j].numAssignatures; i++){
String aux = reader.readLine();
String[] parts = aux.split("-");
String assignaturallegida = parts[0];
int creditsllegits = Integer.parseInt(parts[1].replace(" ",""));
curs[j].addAssignatura(assignaturallegida,creditsllegits);
}
System.out.println(curs[j].getNumAssignatures() + " + " + curs[j].getAssignatures());
}
reader.close();
}
}
I get this:
7 + [model.Assignatura#7eda2dbb, model.Assignatura#6576fe71, model.Assignatura#76fb509a, model.Assignatura#300ffa5d, model.Assignatura#1f17ae12, model.Assignatura#4d405ef7, model.Assignatura#6193b845]
Thank you!!
You have to override the toString() method in your model.Assignatura class.
The default implementation of toString() would print Fully Qualified class name followed by '#' and the object's hash code in hexadecimal format. That's explaining what you are receiving.

Code to read the dataset

Here I read the dataset and extracted the data lines(not the attributes) and print it.Next I need to sort the dataset.Now this is stored in an ArrayList.How to sort it?
public static void main(String args[]) throws Exception
{
String filen, jsnfl;
Customiseddata data = new Customiseddata();
data.setAlgorithm("C4.5");
data.setUserName("Dahlia");
System.out.println("Enter the file name");
sc = new Scanner(System.in);
filen = sc.nextLine();
data.setFileName("input_files/" + filen);
Mainclass main = new Mainclass();
main.build(data);
}
public void build(Customiseddata data) throws Exception
{
int extension;
String filename;
filename = data.getFileName();
extension = filename.lastIndexOf('.');
String extensionType = filename.substring(extension + 1,
filename.length());
if (extensionType.equalsIgnoreCase("csv"))
{
readcsv(filename);
}
else if (extensionType.equalsIgnoreCase("arff"))
{
readarff(filename);
}
}
public void readarff(String filename) throws Exception
{
#SuppressWarnings("unused")
int filesize, attributesize, c = 0, i;
#SuppressWarnings("unused")
float v = 0;
String s, line1;
ArrayList<String> filelines;
ArrayList<String> attributes;
Customiseddata data = new Customiseddata();
Arfffilereader arfffile = new Arfffilereader();
Extractdata exdata = new Extractdata();
exdata = arfffile.extractInputArff(filename);
filelines = exdata.getFileLines();
attributes = exdata.getAttributes();
filesize = filelines.size();
attributesize = attributes.size();
data.setFilesize(filesize);
System.out.println("Print the attributes");
System.out.println("--------------------");
for (i = 0; i < attributesize; i++)
{
System.out.println(attributes.get(i));
}
System.out.println("\t");
System.out.println("Print the filelines");
System.out.println("--------------------");
for (int j = 0; j < filesize; j++)
{
System.out.println(filelines.get(j));
}
}
But after this I need to sort the dataset.
Since the elements of the list are Strings and since String implements Comparable, sorting a list is as simple as:
Collections.sort(theList);
Note however that it will sort the list in place. If you don't want that, make a copy of the list and sort that copy.

multidimensional String[][] array java

I have an array that is from .split command and want to put it into an array called String[][] datatabvars, I do not know how to turn datatabvars into a two dimensional array and put the data into it.
public String[] getList() {
String file_name = "path";
String[] links = null;
String[][] datatabvars = null; // this var
int numberOfDatatabs = 0;
try {
ReadFile file = new ReadFile(file_name);
String[] aryLines = file.OpenFile();
int i;
for(i=0; i < aryLines.length; i++) { //aryLines.length
if (aryLines[i].substring(0, 7).equals("datatab")) {
aryLines[i] = aryLines[i].replace("datatab["+Integer.toString(numberOfDatatabs)+"] = new Array(", "");
aryLines[i] = aryLines[i].replace(");", "");
datatabvars = aryLines[i].split(","); // this split array
numberOfDatatabs++;
}
}
System.out.println(datatabvars[0]);
}catch (IOException e) {
System.out.println( e.getMessage() );
}
return links;
}
Update the two lines(I added comment) as below: (I am assuming that rest of your code is working)
String[][] datatabvars = null; // this var
int numberOfDatatabs = 0;
try {
ReadFile file = new ReadFile(file_name);
String[] aryLines = file.OpenFile();
datatabvars = new String[aryLines.length][]; // INITIALIZED
int i;
for(i=0; i < aryLines.length; i++) { //aryLines.length
if (aryLines[i].substring(0, 7).equals("datatab")) {
aryLines[i] = aryLines[i].
replace("datatab["+Integer.toString(numberOfDatatabs)+"] =
new Array(", "");
aryLines[i] = aryLines[i].replace(");", "");
datatabvars[i] = aryLines[i].split(","); // this split array: ASSIGNED
numberOfDatatabs++;
}
}
System.out.println(datatabvars[0]);
In general, arrays are to avoided like the plague - use collections if possible:. In this case, split() returns a String[], so use that, but use List<String[]> to store multiple String[]:
List<String[]> datatabvars = new ArrayList<String[]>();
...
String[] array = input.split(",");
datatabvars.add(array);
You find life is much easier using collections than arrays.

array in array list

In the input file, there are 2 columns: 1) stem, 2) affixes. In my coding, i recognise each of the columns as tokens i.e. tokens[1] and tokens[2]. However, for tokens[2] the contents are: ng ny nge
stem affixes
---- -------
nyak ng ny nge
my problem here, how can I declare the contents under tokens[2]? Below are my the snippet of the coding:
try {
FileInputStream fstream2 = new FileInputStream(file2);
DataInputStream in2 = new DataInputStream(fstream2);
BufferedReader br2 = new BufferedReader(new InputStreamReader(in2));
String str2 = "";
String affixes = " ";
while ((str2 = br2.readLine()) != null) {
System.out.println("Original:" + str2);
tokens = str2.split("\\s");
if (tokens.length < 4) {
continue;
}
String stem = tokens[1];
System.out.println("stem is: " + stem);
// here is my point
affixes = tokens[3].split(" ");
for (int x=0; x < tokens.length; x++)
System.out.println("affix is: " + affixes);
}
in2.close();
} catch (Exception e) {
System.err.println(e);
} //end of try2
You are using tokens as an array (tokens[1]) and assigning the value of a String.split(" ") to it. So it makes things clear that the type of tokens is a String[] array.
Next,
you are trying to set the value for affixes after splitting tokens[3], we know that tokens[3] is of type String so calling the split function on that string will yield another String[] array.
so the following is wrong because you are creating a String whereas you need String[]
String affixes = " ";
so the correct type should go like this:
String[] affixes = null;
then you can go ahead and assign it an array.
affixes = tokens[3].split(" ");
Are you looking for something like this?
public static void main(String[] args) {
String line = "nyak ng ny nge";
MyObject object = new MyObject(line);
System.out.println("Stem: " + object.stem);
System.out.println("Affixes: ");
for (String affix : object.affixes) {
System.out.println(" " + affix);
}
}
static class MyObject {
public final String stem;
public final String[] affixes;
public MyObject(String line) {
String[] stemSplit = line.split(" +", 2);
stem = stemSplit[0];
affixes = stemSplit[1].split(" +");
}
}
Output:
Stem: nyak
Affixes:
ng
ny
nge

Categories

Resources