Java - Read characters from file to ArrayList - java

I try to create program which can:
1. read characters from file
2. add these characters to ArrayList
3. Check if in line are only characters a,b,c (no other/no spaces)
If 3 is true -
1. compare first & last character in ArrayList, if they are different print "OK"
example file:
abbcb - OK
abbca - NOT OK
a bbc - NOT OK
abdcb - NOT OK
bbbca - OK
At the moment I got:
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Projekt3
{
public static void main(String[] args) throws IOException
{
List<String> Lista = new ArrayList<String>();
Scanner sc = new Scanner(System.in).useDelimiter("\\s*");
while (!sc.hasNext("z"))
{
char ch = sc.next().charAt(0);
Lista.add(ch);
//System.out.print("[" + ch + "] ");
}
}
}
I have problems with adding character to list. I'll be grateful for help.

import java.io.*;
import java.util.ArrayList;
public class Project3 {
public static void main(String[] args) throws FileNotFoundException, IOException {
BufferedReader reader = new BufferedReader(new FileReader("//home//azeez//Documents//sample")); //replace with your file path
ArrayList<String> wordList = new ArrayList<>();
String line = null;
while ((line = reader.readLine()) != null) {
wordList.add(line);
}
for (String word : wordList) {
if (word.matches("^[abc]+$")) {
if (word.charAt(0) == word.charAt(word.length() - 1)) {
System.out.print(word + "-NOT OK" + " ");
} else {
System.out.print(word + "-OK" + " ");
}
}
}
}
}

i think this is good start for you:
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class Project3 {
public static void main(String[] args) {
String path = "/Users/David/sandbox/java/test.txt";
try (BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(path)))) {
String currentLine = null;
// Array list for your words
List<String> arrayList = new ArrayList<>();
while ((currentLine = br.readLine()) != null) {
// only a, b and c
if (currentLine.contains("a") && currentLine.contains("b") && currentLine.contains("c")) {
// start character equal end character
if (currentLine.substring(0, 1)
.equals(currentLine.substring(currentLine.length()-1, currentLine.length()))) {
arrayList.add(currentLine);
System.out.println(currentLine);
}
}
}
} catch (Throwable e) {
System.err.println("error on read file " + e.getMessage());
e.printStackTrace();
}
}
}

Related

How to implement a Java program to count the number of keywords in a Java file

I am developing a program to read the keywords from a Java file. My goal is to display the total number of keywords excluding the keywords from the strings and comments.
My attempt
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.HashSet;
import java.util.Set;
import java.io.File;
import java.util.Arrays;
public class Exercise21_03 {
public static void main(String args[]){
String filename = args[0];
// Step 1: read the file.
File file = new File(filename);
if(file.exists()){
try {
System.out.println("The number of keywords in "+ filename + " is "+ countKeywords(file));
} catch (Exception e) {
System.out.print(e.getMessage());
}
}
else {
System.out.println("File "+ filename + " does not exist");
}
}
public static int countKeywords(File file) throws Exception {
// Array of all Java keywords + true, ffalse and null
String[] keywordString = {"abstract", "assert", "boolean","break","byte","case","catch","char","class","const","continue","default","do","double","else","enum","extends","for","final","finally","float","goto","if","implements","import","instanceof","int","interface","long","native","new","package","private","protected","public","return","short","static","strictfp","super","switch","synhronized","this","throw","throws","transient","try","void","volatile","while","true","false","null"};
Set<String> keywordSet = new HashSet<>(Arrays.asList(keywordString));
int count = 0;
BufferedReader br = new BufferedReader(new FileReader(file));
String st;
while((st = br.readLine()) != null){
String[] words = st.split(" ");
for(String word : words)
if(keywordSet.contains(word))
count++;
}
return count;
}
}
Welcome.java
public class Welcome {
public static void main(String[] args) {
// Display message Welcome to Java! on the console
// abstract
System.out.println("Welcome to Java!");
}
}
Expected:
java Exercise21_03 Welcome.java
The number of keywords in Welcome.java is 5
Actual
java Exercise21_03 Welcome.java
The number of keywords in Welcome.java is 6
How can I process this?
I believe this will do the trick
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.HashSet;
import java.util.Set;
import java.io.File;
import java.util.Arrays;
public class Exercise21_03 {
public static void main(String args[]){
String filename = args[0];
// Step 1: read the file.
File file = new File(filename);
if(file.exists()){
try {
System.out.println("The number of keywords in "+ filename + " is "+ countKeywords(file));
} catch (Exception e) {
System.out.print(e.getMessage());
}
}
else {
System.out.println("File "+ filename + " does not exist");
}
}
public static int countKeywords(File file) throws Exception {
// Array of all Java keywords + true, ffalse and null
String[] keywordString = {"abstract", "assert", "boolean","break","byte","case","catch","char","class","const","continue","default","do","double","else","enum","extends","for","final","finally","float","goto","if","implements","import","instanceof","int","interface","long","native","new","package","private","protected","public","return","short","static","strictfp","super","switch","synhronized","this","throw","throws","transient","try","void","volatile","while","true","false","null"};
Set<String> keywordSet = new HashSet<>(Arrays.asList(keywordString));
int count = 0;
BufferedReader br = new BufferedReader(new FileReader(file));
String st;
while((st = br.readLine()) != null){
String[] words = st.split(" ");
if(Arrays.asList(words).contains("//"))
continue;
for(String word : words)
if(keywordSet.contains(word))
count++;
}
return count;
}
}

Split with new line and store in datastructure

I have text file data like :
2,2,1
data1,123,89,1
data2,124,90,2
data3,125,91,3
data4,126,92,4
data5,127,93,5
data6,128,94,6
data7,129,95,7
data8,130,96,8
data9,131,97,9
data10,132,98,10
The first line 2,2,1 indicate 2 lines from 1st set of lines and store it in nodeFile, 2 lines from 2nd set of lines store it in linkFile and 1 line from 3rd set of lines store it in moduleFile. However for example purpose I have shows small number of lines but its a larger file.
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class ReadFile {
static List<String> moduleFile = new ArrayList<>();
static List<String> linkFile = new ArrayList<>();
static List<String> nodeFile = new ArrayList<>();
static int a[];
public static void main(String[] args) {
File file11 = new File("/home/madhu/Desktop/node.txt");
Scanner scAll = null;
try {
scAll = new Scanner(file11);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
String[] numberOfLines = (scAll.nextLine()).split(",");
int flag = 0;
int counter = 1;
while (scAll.hasNext()) {
if (flag == 0 && "\\n\\n".equals(scAll.nextLine()) && counter <= Integer.parseInt(numberOfLines[0].trim())) {
for (int i = 0; i < Integer.parseInt(numberOfLines[0].trim()); i++) {
System.out.println(scAll.nextLine());
nodeFile.add(scAll.nextLine());
counter++;
}
if (counter > Integer.parseInt(numberOfLines[0].trim())) {
flag = 1;
counter = 1;
}
} else if (flag == 1 && "\\n\\n".equals(scAll.nextLine())
&& counter <= Integer.parseInt(numberOfLines[1].trim())) {
for (int i = 0; i < Integer.parseInt(numberOfLines[1].trim()); i++) {
System.out.println(scAll.nextLine());
linkFile.add(scAll.nextLine());
counter++;
}
if (counter > Integer.parseInt(numberOfLines[1].trim())) {
flag = 2;
counter = 1;
}
} else if (flag == 2 && "\\n\\n".equals(scAll.nextLine())
&& counter <= Integer.parseInt(numberOfLines[2].trim())) {
for (int i = 0; i < Integer.parseInt(numberOfLines[2].trim()); i++) {
System.out.println(scAll.nextLine());
moduleFile.add(scAll.nextLine());
counter++;
}
} else {
continue;
}
}
scAll.close();
}
}
I have written the above code, but this code gets terminated during execution. How to get the desired result? Please help.
Hopefully I am not misunderstanding, but this is what I'd do.
I didn't check to see if this is fully working example, but it should work more or less.
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;
class ReadFile {
// basically just do what you did
static List<String> nodeFile;
static List<String> linkFile;
static List<String> moduleFile;
public static void main(String[] args) throws FileNotFoundException {
final File file = new File("/home/madhu/Desktop/node.txt");
final Scanner scanner = new Scanner(file);
// make it a little better for indexing
final List<Integer> selections = Arrays
.stream(scanner.nextLine().split(","))
.map(Integer::parseInt)
.collect(Collectors.toList());
// this is the meat of the code
// basically each split up block of lines is a block
final List<List<String>> blocks = new ArrayList<>();
while (scanner.hasNextLine()) {
List<String> lines = new ArrayList<>();
String line;
while (!(line = scanner.nextLine()).equals("\n")) {
lines.add(line);
}
if (!lines.isEmpty()) {
blocks.add(lines);
}
}
// allocate your files now
nodeFile = blocks.get(0).subList(0, selections.get(0));
linkFile = blocks.get(1).subList(0, selections.get(1));
moduleFile = blocks.get(2).subList(0, selections.get(2));
scanner.close();
}
}
try this code , i use BufferedReader because its more cleaner :
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class ReadFile {
static List<String> moduleFile = new ArrayList<>();
static List<String> linkFile = new ArrayList<>();
static List<String> nodeFile = new ArrayList<>();
public static void main(String[] args) {
File file = new File("/home/madhu/Desktop/node.txt");
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(file));
String data[] = reader.readLine().split(",");
String s;
int nbline=0, i=0,block =0;
while ((s = reader.readLine())!=null && block < data.length) {
if(s.equals("")){
block++;
nbline = Integer.parseInt(data[block-1]);
i = 0;
}
for(;i<nbline;i++){
s = reader.readLine();
if(s == null) break;
else if(s.equals("")){
block++;
break;
}
switch(block){
case 1 :
nodeFile.add(s);
break;
case 2:
linkFile.add(s);
break;
default: moduleFile.add(s);
}
}
}
} catch (IOException | NumberFormatException ex) {
System.err.println(ex.getStackTrace());
}
finally{
closeReader(reader);
}
System.out.println("nodeFile : "+nodeFile);
System.out.println("linkFile : "+linkFile);
System.out.println("moduleFile : "+moduleFile);
}
public static void closeReader(BufferedReader reader) {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
}
}
}
}
output :
nodeFile : [data1,123,89,1, data2,124,90,2]
linkFile : [data5,127,93,5, data6,128,94,6]
moduleFile : [data8,130,96,8]

Splitting a String by Comma, then put it in a Treemap

I have a file called "marathon", where I have 7 keys:
sex
time
athlete
athlete's nationality
date
city
country
splitted by a comma ",". I have to put the second key (time) in a Treemap.
At the moment I am just trying to show only the time in the console.
So here is my code:
public class Text {
#SuppressWarnings("resource")
public static void main(String[] args) throws FileNotFoundException {
try {
BufferedReader in = new BufferedReader(new FileReader("marathon"));
String str;
str = in.readLine();
while ((str = in.readLine()) != null) {
//System.out.println(str);
String[] ar=str.split(",");
System.out.println(ar[0]);
}
in.close();
} catch (IOException e) {
System.out.println("File Read Error");
}
}
}
This is what a line of the text looks like:
M, 2:30:57.6, Harry Payne, GBR, 1929-07-05, Stamford Bridge, England
When I start the program of my code example and put in System.out.println(ar[0]); a[0] shows me the first line in the console so M's and F's. But when I put a[1] there is an exception:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1
As others have pointed out, you do readline twice before you get into the body of the loop, so you will miss the first line.
But you are also not checking that readline resulted in a properly formatted line. It may be an empty line or a line that in some other way does not result in an array that you expect.
So you should add an if-statement that checks that you have what you expected, like so...
public class Text {
#SuppressWarnings("resource")
public static void main(String[] args) throws FileNotFoundException {
try {
BufferedReader in = new BufferedReader(new FileReader("marathon"));
String str = "";
while ((str = in.readLine()) != null) {
String[] ar=str.split(",");
if(ar.length >= 7) {
System.out.println(ar[0] + ", " + ar[1]);
}
}
in.close();
} catch (IOException e) {
System.out.println("File Read Error");
}
}
}
Please try the code below. It's working for me.
You should have read the line only once in the while loop.
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class Text {
#SuppressWarnings("resource")
public static void main(String[] args) throws FileNotFoundException {
try {
BufferedReader in = new BufferedReader(new FileReader("marathon"));
String str;
while ((str = in.readLine()) != null) {
//System.out.println(str);
String[] ar=str.split(",");
System.out.println(ar[0]);
System.out.println(ar[1]);
}
in.close();
} catch (IOException e) {
System.out.println("File Read Error");
}
}
}
SortedMap<String, String[]> map = new TreeMap<>();
Path path = Paths.get("marathon");
Files.lines(path, Charsets.defaultCharset())
.map(line -> line.split(",\\s*"))
.peek(words -> {
if (words.length != 7) {
Logger.getLogger(getClass().getName()).info("Line wrong: " + line);
}
})
.filter(words -> words.length == 7)
.forEach(words -> map.put(word[1], words));
However there are CSV reader classes out there, that can handle quoted fields with commas and such.
Java 8 just for fun
import java.io.IOException;
import java.nio.file.*;
import java.util.*;
import java.util.stream.Stream;
import static java.nio.charset.Charset.defaultCharset;
import static java.lang.System.out;
import static java.nio.file.Files.lines;
public class Main {
public static void main(String[] args) throws IOException {
Map<String, String[]> map = new TreeMap<>( );
try( Stream<String> lines = lines(Paths.get("marathon"), defaultCharset())){
lines.map(line -> line.split( "," )).forEach( entry -> map.put(entry[1], entry ));
map.values().forEach( entry -> out.println(Arrays.toString( entry )) );
}
}
}

Trouble with recursion and text file

This program takes in input the "hello my name is bob" and spits it out backwards. I really need help making it that the program reads in a text file and spits out the text file backwards. Thanks in advance!
public class Recursion
{
public static void main(String[] args) throws FileNotFoundException {
System.out.println(printBackwards("hello my name is bob"));
}
public static String printBackwards(String s){
if(s.length() <= 1)
return s;
else
return printBackwards(s.substring(1,s.length()))+s.charAt(0);
}
}
Based on the comments for the question, this will read a file called input.txt and save it to a new file called output.txt using your method for reversing a String.
All lines in input.txt are firstly added to a List.
The List is then iterated through backwards from the last element, and with each iteration the reversed String written to output.txt.
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
import java.util.Scanner;
public class Example {
public static void main(String[] args) throws IOException {
try (BufferedWriter writer = new BufferedWriter(new FileWriter("output.txt"))) {
Scanner scanner = new Scanner(new File("input.txt"));
List<String> fileContents = new ArrayList<>();
while (scanner.hasNextLine()) {
fileContents.add(scanner.nextLine());
}
ListIterator<String> it = fileContents.listIterator(fileContents.size());
while (it.hasPrevious()) {
writer.write(printBackwards(it.previous()));
writer.newLine();
}
}
}
public static String printBackwards(String s) {
if (s.length() <= 1) {
return s;
} else {
return printBackwards(s.substring(1, s.length())) + s.charAt(0);
}
}
}
If however you just want to display it to the standard output, you can adjust it to the following:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
import java.util.Scanner;
public class Example {
public static void main(String[] args) throws FileNotFoundException {
Scanner scanner = new Scanner(new File("input.txt"));
List<String> fileContents = new ArrayList<>();
while (scanner.hasNextLine()) {
fileContents.add(scanner.nextLine());
}
ListIterator<String> it = fileContents.listIterator(fileContents.size());
while (it.hasPrevious()) {
System.out.println(printBackwards(it.previous()));
}
}
public static String printBackwards(String s) {
if (s.length() <= 1) {
return s;
} else {
return printBackwards(s.substring(1, s.length())) + s.charAt(0);
}
}
}
Or as I said in my comment earlier, you can just read the whole file in one go and reverse it:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Example {
public static void main(String[] args) throws FileNotFoundException {
System.out.println(printBackwards(new Scanner(new File("file.txt")).useDelimiter("\\Z").next()));
}
public static String printBackwards(String s) {
if (s.length() <= 1) {
return s;
} else {
return printBackwards(s.substring(1, s.length())) + s.charAt(0);
}
}
}
Use this code to get the string that you want to reverse from a text file:
try{
String myString;
BufferedReader input = new BufferedReader(new FileReader("Filepath.txt");
while((myString = input.readLine()) != null){}
}
catch(FileNotFoundException ex){
//Error Handler Here
}
catch(IOException ex){
//Error Handler Here
} finally {
try{
if(br != null) br.close();
}
catch(IOException ex){
ex.printStackTrace();
}
}
Don't forget to import:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.FileNotFoundException
This should work as you expect. I assume that in the filename_in.txt you have only one line, otherwise you have to loop (I let you to do this as exercise):
public static void main(String[] args){
Scanner in = null;
PrintWriter writer = null;
try{
in = new Scanner(new FileReader("filename_in.txt"));
writer = new PrintWriter("filename_out.txt");
writer.println(printBackwards(in.nextLine()));
} catch (Exception e) {
e.printStackTrace();
}
finally {
in.close();
writer.close();
}
}

file reading into array

I am trying to read contents of a file using string tokenizer and store all the tokens in an array but i keep getting exception in main error. I need advise on how to do this.Below is the code am using for that;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.StringTokenizer;
public class FileTokenizer
{
private static final String DEFAULT_DELIMITERS = "< , { } >";
private static final String DEFAULT_TEST_FILE = "trans1.txt";
public List<String> tokenize(Reader reader) throws IOException
{
List<String> tokens = new ArrayList<String>();
BufferedReader br = null;
try
{
int i = 0;
br = new BufferedReader(reader);
Scanner scanner = new Scanner(br);
while (scanner.hasNext())
{
StringTokenizer st = new StringTokenizer(scanner.next(), DEFAULT_DELIMITERS, true);
while (st.hasMoreElements())
{
String[] t = new String[200];
tokens.add(st.nextToken());
t[i] = st.nextToken();
System.out.println(t[i]);
i++;
}
}
}
finally
{
close(br);
}
return tokens;
}
public static void close(Reader r)
{
try
{
if (r != null)
{
r.close();
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
{
try
{
String fileName = ((args.length > 0) ? args[0] : DEFAULT_TEST_FILE);
FileReader fileReader = new FileReader(new File(fileName));
FileTokenizer fileTokenizer = new FileTokenizer();
List<String> tokens = fileTokenizer.tokenize(fileReader);
//System.out.println(tokens);
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
My file looks like;
PDA = (
{ q1, q2, q3, q4},
{ 0, 1 },
{ 0, $ },
{ (q1, #, #) -> { (q2, $) }, (q2, 0, #) -> { (q2, 0) },
(q2, 1, 0) -> { (q3, #) }, (q3, 1, 0) -> { (q3, #) },
(q3, #, $) -> { (q4, #) } },
q1,
{ q1, q4}
)
You will get the java.util.NoSuchElementException since you are calling st.nextToken() twice within the loop
while (st.hasMoreElements())
Modifying harigm's example, you can then add t[i] to tokens as you require
String[] t = new String[200];
System.out.println(t[i]);
tokens.add(t[i]);
Delimiters shouldn't be separated by spaces:
private static final String DEFAULT_DELIMITERS = "<,{}>";
Also, keep the following in mind (from the Javadoc):
StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead.
String.split() was introduced in JDK 1.4.
That said:
Using a Scanner to tokenize a stream together with a StringTokenizer looks a bit weird to me;
You call st.nextToken() twice in the inner loop;
t is useless. You re-create it each time in your inner loop and use only one element of it.
It seems that what you are trying to build is a lexical analyzer. Maybe you should look up some documentation on the subject.
HI,
I have modified your code and Now works perfectly fine, check this
package org.sample;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.StringTokenizer;
public class FileTokenizer
{
private static final String DEFAULT_DELIMITERS = "< , { } >";
// private static final String DEFAULT_TEST_FILE = "trans1.txt";
public List<String> tokenize(Reader reader) throws IOException
{
List<String> tokens = new ArrayList<String>();
BufferedReader br = null;
try
{
int i = 0;
br = new BufferedReader(reader);
Scanner scanner = new Scanner(br);
while (scanner.hasNext())
{
StringTokenizer st = new StringTokenizer(scanner.next(), DEFAULT_DELIMITERS, true);
while (st.hasMoreElements())
{
String[] t = new String[200];
// tokens.add(st.nextToken());
// t[i] = st.nextToken();
System.out.println(t[i]);
i++;
}
}
}
finally
{
close(br);
}
return tokens;
}
public static void close(Reader r)
{
try
{
if (r != null)
{
r.close();
}
}
catch (IOException e)
{
e.printStackTrace();
}
}
public static void main(String[] args)
{
try
{
// String fileName = ((args.length > 0) ? args[0] : DEFAULT_TEST_FILE);
FileReader fileReader = new FileReader(new File("c:\\DevTest\\1.txt"));
FileTokenizer fileTokenizer = new FileTokenizer();
List<String> tokens = fileTokenizer.tokenize(fileReader);
//System.out.println(tokens);
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
Looking at your input file, I should point out that its hierarchical and irregular structure makes it more suited to be parsed by an actual parser. You may have to learn how to use a parser generator and write a lexer and grammar for it etc, but in the end you'll end up with a much more maintainable code. Doing this yourself is rather painstaking and error-prone.
I recommend ANTLR. It's quite mature, and it has a wide enough user base that I'm sure you can get help easily.

Categories

Resources