Buffered Reader read certain line and text - java

This is my first post, so i'm not sure how things work here.
Basically, i need some help/advice with my code. The method need to read a certain line and print out the text after the inputted text and =
The text file would like
A = Ant
B = Bird
C = Cat
So if the user it input "A" it should print out something like
-Ant
So far, i manage to make it ignore "=" but still print out the whole file
here is my code:
public static void readFromFile() {
System.out.println("Type in your word");
Scanner scanner = new Scanner(System.in);
String input = scanner.next();
String output = "";
try {
FileReader fr = new FileReader("dictionary.txt");
BufferedReader br = new BufferedReader(fr);
String[] fields;
String temp;
while((input = br.readLine()) != null) {
temp = input.trim();
if (temp.startsWith(input)) {
String[] splitted = temp.split("=");
output += splitted[1] + "\n";
}
}
System.out.print("-"+output);
}
catch(IOException e) {
}
}

It looks like this line is the problem, as it will always be true.
if (temp.startsWith(input))
You need to have a different variables for the lines being read out of the file and for the input you're holding from the user. Try something like:
String fileLine;
while((fileLine = br.readLine()) != null)
{
temp = fileLine.trim();
if (temp.startsWith(input))
{
String[] splitted = temp.split("=");
output += splitted[1] + "\n";
}
}

You can use useDelimiter() method of Scanner to split input text
scanner.useDelimiter("(.)*="); // Matches 0 or more characters followed by '=', and then gives you what is after `=`
The following code is something I've tried in IDEONE (http://ideone.com/TBwCFj)
Scanner s = new Scanner(System.in);
s.useDelimiter("(.)*=");
while(s.hasNext())
{
String ss = s.next();
System.out.print(ss);
}
/**
* Output
*
*/
Ant
Bat

You need to first split the text file by new line "\n" (assuming after each "A = Ant", "B = Bird" ,"C = Cat" declaration it starts with a new line) and THEN locate the inputted character and further split that by "=" as you were doing.
So you will need two arrays of Strings (String[ ]) one for each line and one for the separation of each line into e.g. "A" and "Ant".
You are very close.

try this, it works: STEPS:
1) read input using scanner
2) read file using bufferedreader
3) split each line using "-" as a delimiter
4) compare first character of line with input
5) if first character is equal to input then print the associated value, preceded by a "-"
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.File;
import java.util.Scanner;
class myRead{
public static void main(String[] args) throws FileNotFoundException, IOException {
System.out.println("Type in your word");
Scanner scanner = new Scanner(System.in);
String input = scanner.next();
long numberOfLines = 0;
BufferedReader myReader = new BufferedReader(new FileReader("test.txt"));
String line = myReader.readLine();
while(line != null){
String[] parts = line.split("=");
if (parts[0].trim().equals(input.trim())) {
System.out.println("-"+parts[1]);
}
line = myReader.readLine();
}
}
}
OUTPUT (DEPENDING ON INPUT):
- Ant
- Bird
- Cat

Related

StringBuffer: Adding a newline after a certain amount of words for formatting

Okay so my problem is formatting the output of my program. My program is meant to be a madlib, It reads in a file and then allows the user to enter nouns, adjectives, plurals etc. and then it prints the madlib back out with the updated version of what the user entered.
Here's my text file:
One of the most adjective characters in fiction is named "Tarzan of the plural-noun." Tarzan was raised by a/an noun and lives in the adjective jungle in the heart of darkest place. He spends most of this time eating plural-noun and swinging from tree to noun. Whenever he gets angry, he beats on his chest and says, "funny-noise !" This is his war cry. Tarzan always dresses in adjective shorts made from the skin of a/an noun and his best friend is a/an adjective chimpanzee names Cheetah. He is supposed to be able to speak to elephants and plural-noun. In the movies, Tarzan is played by person's-name.
The tokens I scan for in the file are these <> (I didn't show them in the text file above but where it says adjective or noun or funny noises its really < adjective > with no space between left arrow adjective and right arrow adjective) and that's where the users inputs are placed. Everything in my program works except until I print it out. Instead of printing the madlib out in the format above, it just prints it out in one long line. It doesn't have to match the above format, I'd just like it to print a newline after a length of 50 for instance just so it's easier to read.
Here's my code:
import java.util.Map;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.HashMap;
import java.util.Scanner;
import java.util.StringTokenizer;
public class ReadFile
{
public static void main(String[] args) //throws Exception
{
Scanner s=new Scanner(System.in);
BufferedReader br = new BufferedReader(new FileReader(args[0]));
String line;
StringBuffer storybuffer=new StringBuffer();
//Accept lines until next line null
while((line=br.readLine()) != null)
storybuffer.append(" "+line);
//Remove first space
storybuffer.delete(0, 1);
String story=storybuffer.toString();
//Split
StringTokenizer str=new StringTokenizer(story);
String word;
StringBuffer finalstory=new StringBuffer();
//Store added elements
Map<String,String> hash=new HashMap<String,String>();
while(str.hasMoreTokens())
{
word=str.nextToken();
if(word.contains("<"))
{
String add="";
//Element prompt could be more than one word
if(!word.contains(">"))
{
//Build multi-word prompt
String phrase="";
do{
phrase+=word+" ";
}while(!(word=str.nextToken()).contains(">"));
word=phrase+word;
}
//Account for element placeholder being immediately followed by . or , or whatever.
if(word.charAt(word.length()-1)!='>')
add=word.substring(word.lastIndexOf('>')+1);
//Store id of element in hash table
String id=word.substring(0,word.lastIndexOf('>')+1);
String value;
if(!hash.containsKey(id))
{
//New element
System.out.println("Enter a "+ id);
value=s.nextLine()+add;
hash.put(id, value);
}
//Previously entered element heres the problem for duplicates!
else
value=hash.get(id);
word=value;
}
finalstory.append(word+" ");
// if(finalstory.length() > 50){
// finalstory.append("\n");
}
System.out.println(finalstory.toString());
s.close();
}
}
Anyone have any ideas of how to fix this?
import java.util.Map;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.HashMap;
import java.util.Scanner;
import java.util.StringTokenizer;
public class ReadFile {
public static void main(String[] args) //throws Exception
{
Scanner s = new Scanner(System.in);
BufferedReader br = new BufferedReader(new FileReader(args[0]));
String line;
StringBuffer storybuffer = new StringBuffer();
//Accept lines until next line null
while ((line = br.readLine()) != null)
storybuffer.append(" " + line);
//Remove first space
storybuffer.delete(0, 1);
String story = storybuffer.toString();
//Split
StringTokenizer str = new StringTokenizer(story);
String word;
StringBuffer finalstory = new StringBuffer();
//Store added elements
Map < String, String > hash = new HashMap < String, String > ();
while (str.hasMoreTokens()) {
word = str.nextToken();
if (word.contains("<")) {
String add = "";
//Element prompt could be more than one word
if (!word.contains(">")) {
//Build multi-word prompt
String phrase = "";
do {
phrase += word + " ";
} while (!(word = str.nextToken()).contains(">"));
word = phrase + word;
}
//Account for element placeholder being immediately followed by . or , or whatever.
if (word.charAt(word.length() - 1) != '>')
add = word.substring(word.lastIndexOf('>') + 1);
//Store id of element in hash table
String id = word.substring(0, word.lastIndexOf('>') + 1);
String value;
if (!hash.containsKey(id)) {
//New element
System.out.println("Enter a " + id);
value = s.nextLine() + add;
hash.put(id, value);
}
//Previously entered element
else
value = hash.get(id);
word = value;
}
finalstory.append(word + " ");
// if(finalstory.length() > 50){
// finalstory.append("\n");
}
System.out.println(finalstory.toString());
s.close();
}
}

extract integer from txt file java

I have a big problem with extracting some integer values from a file which looks like:
[41; 48; 36; 128; 1;...........105]
I was trying to do it by this code:
Scanner scanner = new Scanner(new File("ala1.txt"));
int [] tall = new int [800];
int i = 0;
while(scanner.hasNext())
{
if (scanner.hasNextInt()){
tall[i++] = scanner.nextInt();
}
But it doesn't work. Can anyone help me ??
Problem is that Scanner sees characters like [, : as proper text, so it can't parse token like [41; or 48; as integer. To solve this problem you could set Scanner to treat every non-digit character as delimiter.
Since Scanner uses regex syntax you could use delimiter like \D+ which represents
+ one or more
\D non-digit character
So your code can look something like
Scanner scanner = new Scanner(new File("ala1.txt"));
scanner.useDelimiter("\\D+");
while(scanner.hasNextInt()){
System.out.println(scanner.nextInt());
}
To extract all the integers from the line:
Scanner scanner = new Scanner(new File("ala1.txt"));
String line = scanner.nextLine();
List<Integer> ints = new ArrayList<>();
Matcher m = Pattern.compile("\\d+").matcher(line);
while(m.find()) {
ints.add(Integer.parseInt(m.group()));
}
import java.io.BufferedReader;
import Java.io.FileReader;
public class ReadLine
{
BufferedReader br = new BufferedReader(new FileReader("youtFile.txt"));
try
{
String line;
while((line= br.readLine())!=null)
{
System.out.pringln(line)
int vl=Integer.parseInt(line);
}
}
catch(Esception exo)
{
}
finally
{
br.close();
}
}
If More than one value in one line so you can use method of split. than parse in to integer.
import java.io.BufferedReader;
import Java.io.FileReader;
public class ReadLine
{
BufferedReader br = new BufferedReader(new FileReader("youtFile.txt"));
try
{
String line;
while((line= br.readLine())!=null)
{
System.out.pringln(line)
for(int i=0i<line.split(",").length)
{
int vl=Integer.parseInt(line.split(",")[i]);
}
}
}
catch(Esception exo)
{
}
finally
{
br.close();
}
}

Input a text file in a two dimensional array with doubles

I'm new in Java.
I want to input a text file and create from it a two dimensional array the input is
like this
12,242 323,2324
23,4434 23,4534
23,434 56,3434
....
34,434 43,3443
I have tried
import java.util.Scanner;
import java.io.File;
import java.io.IOException;
public class InputText {
/**
* #param args the command line arguments
* #throws java.io.IOException
*/
public static void main(String[] args) throws IOException {
int i=0;
File file;
file = new File("file.txt");
Scanner read=new Scanner(file);
while (read.hasNextLine()) {
String line=read.nextLine();
System.out.println(line);
}
}
}
which gives me the input but I cannot insert this in an array I tried different ways like splitting it.
Any suggestions?
Sorry for not being clear. The input i mentioned is doubles seperated by spaces. Also the format i gave you is what i get after i run the part of the programm i wrote. What i see in the text file is the numbers seperated by spaces. I tried to implement your suggestion but nothing seemed to work. I'm really lost here....
If you want to split a line to two numbers you can use
string[] numbers = line.split("\\s+");
If you want to read a double with comma
NumberFormat format = NumberFormat.getInstance(Locale.FRANCE);
...
double d1 = format.parse(numbers[0]).doubleValue();
double d2 = format.parse(numbers[1]).doubleValue();
Personally i prefer to use scanner. In that case create it with
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
Scanner scanner2 = new Scanner(scanner.nextLine()).useLocale(Locale.FRANCE);
if (!scanner2.hasNextDouble()){
System.out.println("Do not have a pair");
continue;
}
double d1 = scanner2.nextDouble();
if (!scanner2.hasNextDouble()){
System.out.println("Do not have a pair");
continue;
}
double d2 = scanner2.nextDouble();
//do something
}
After reading the line.. you will have to again split the string on ','. The split string need to be converted into interger. YOu can see as below:
import java.util.Scanner;
import java.io.File;
import java.io.IOException;
public class InputText {
/**
* #param args the command line arguments
* #throws java.io.IOException
*/
public static void main(String[] args) throws IOException {
int i = 0;
File file;
file = new File("file.txt");
Scanner read = new Scanner(file);
while (read.hasNextLine()) {
String line = read.nextLine();
String[] numbers = line.split(",");
for (i = 0; i < numbers.lenght; i++) {
String numStr = numbers[i];
String x=numStr.replaceAll("\\s+",""); //eleminate the space in any.
Double num = Double.valueOf(x);
System.out.println(" num is: " + num); //Here you can store the number in array.
}
}
}
}
Try to use something like that(add also try catch statement)
String line = "";
br = new BufferedReader(new FileReader("file.txt"));
int i=0;
while ((line = br.readLine()) != null) {
// use comma as separator
String[] lineArray= line.split(",");
for(int j=0;j<lineArray.length;j++){
my2DArray[i][j] = lineArray[j];
}
i++;
}
for(int i=0;i<my2DArray[0].length;i++){
for(int j=0;j<my2DArray[1].length;j++){
System.out.print(my2DArray[i][j] + " ");
}
}

How to tokenize an input file in java

i'm doing tokenizing a text file in java. I want to read an input file, tokenize it and write a certain character that has been tokenized into an output file. This is what i've done so far:
package org.apache.lucene.analysis;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
class StringProcessing {
// Create BufferedReader class instance
public static void main(String[] args) throws IOException {
InputStreamReader input = new InputStreamReader(System.in);
BufferedReader keyboardInput = new BufferedReader(input);
System.out.print("Please enter a java file name: ");
String filename = keyboardInput.readLine();
if (!filename.endsWith(".DAT")) {
System.out.println("This is not a DAT file.");
System.exit(0);
}
File File = new File(filename);
if (File.exists()) {
FileReader file = new FileReader(filename);
StreamTokenizer streamTokenizer = new StreamTokenizer(file);
int i = 0;
int numberOfTokensGenerated = 0;
while (i != StreamTokenizer.TT_EOF) {
i = streamTokenizer.nextToken();
numberOfTokensGenerated++;
}
// Output number of characters in the line
System.out.println("Number of tokens = " + numberOfTokensGenerated);
// Output tokens
for (int counter = 0; counter < numberOfTokensGenerated; counter++) {
char character = file.toString().charAt(counter);
if (character == ' ') { System.out.println(); } else { System.out.print(character); }
}
} else {
System.out.println("File does not exist!");
System.exit(0);
}
System.out.println("\n");
}//end main
}//end class
When i run this code, this is what i get:
Please enter a java file name: D://eclipse-java-helios-SR1-win32/LexractData.DAT
Number of tokens = 129
java.io.FileReader#19821fException in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 25
at java.lang.String.charAt(Unknown Source)
at org.apache.lucene.analysis.StringProcessing.main(StringProcessing.java:40)
The input file will look like this:
-K1 Account
--Op1 withdraw
---Param1 an
----Type Int
---Param2 amount
----Type Int
--Op2 deposit
---Param1 an
----Type Int
---Param2 Amount
----Type Int
--CA1 acNo
---Type Int
-K2 CheckAccount
--SC Account
--CA1 credit_limit
---Type Int
-K3 Customer
--CA1 name
---Type String
-K4 Transaction
--CA1 date
---Type Date
--CA2 time
---Type Time
-K5 CheckBook
-K6 Check
-K7 BalanceAccount
--SC Account
I just want to read the string which are starts with -K1, -K2, -K3, and so on... can anyone help me?
The problem is with this line --
char character = file.toString().charAt(counter);
file is a reference to a FileReader that does not implement toString() .. it calls Object.toString() which prints a reference around 25 characters long. Thats why your exception says OutofBoundsException at the 26th character.
To read the file correctly, you should wrap your filereader with a bufferedreader and then put each readline into a stringbuffer.
FileReader fr = new FileReader(filename);
BufferedReader br = new BufferedReader(fr);
StringBuilder sb = new StringBuilder();
String s;
while((s = br.readLine()) != null) {
sb.append(s);
}
// Now use sb.toString() instead of file.toString()
If you are wanting to tokenize the input file then the obvious choice is to use a Scanner. The Scanner class reads a given input stream, and can output either tokens or other scanned types (scanner.nextInt(), scanner.nextLine(), etc).
import java.util.Scanner;
import java.io.File;
import java.io.IOException;
public static void main(String[] args) throws IOException {
Scanner in = new Scanner(new File("filename.dat"));
while (in.hasNext) {
String s = in.next(); //get the next token in the file
// Now s contains a token from the file
}
}
Check out Oracle's documentation of the Scanner class for more info.
public class FileTokenize {
public static void main(String[] args) throws IOException {
final var lines = Files.readAllLines(Path.of("myfile.txt"));
FileWriter writer = new FileWriter( "output.txt");
String data = " ";
for (int i = 0; i < lines.size(); i++) {
data = lines.get(i);
StringTokenizer token = new StringTokenizer(data);
while (token.hasMoreElements()) {
writer.write(token.nextToken() + "\n");
}
}
writer.close();
}

Java file read problem

I have a java problem. I am trying to read a txt file which has a variable number of integers per line, and for each line I need to sum every second integer! I am using scanner to read integers, but can't work out when a line is done. Can anyone help pls?
have a look at the BufferedReader class for reading a textfile and at the StringTokenizer class for splitting each line into strings.
String input;
BufferedReader br = new BufferedReader(new FileReader("foo.txt"));
while ((input = br.readLine()) != null) {
input = input.trim();
StringTokenizer str = new StringTokenizer(input);
String text = str.nextToken(); //get your integers from this string
}
If I were you, I'd probably use FileUtils class from Apache Commons IO. The method readLines(File file) returns a List of Strings, one for each line. Then you can simply handle one line at a time.
Something like this:
File file = new File("test.txt");
List<String> lines = FileUtils.readLines(file);
for (String line : lines) {
// handle one line
}
(Unfortunately Commons IO doesn't support generics, so the there would be an unchecked assignment warning when assigning to List<String>. To remedy that use either #SuppressWarnings, or just an untyped List and casting to Strings.)
This is, perhaps, an example of a situation where one can apply "know and use the libraries" and skip writing some lower-level boilerplate code altogether.
or scrape from commons the essentials to both learn good technique and skip the jar:
import java.io.*;
import java.util.*;
class Test
{
public static void main(final String[] args)
{
File file = new File("Test.java");
BufferedReader buffreader = null;
String line = "";
ArrayList<String> list = new ArrayList<String>();
try
{
buffreader = new BufferedReader( new FileReader(file) );
line = buffreader.readLine();
while (line != null)
{
line = buffreader.readLine();
//do something with line or:
list.add(line);
}
} catch (IOException ioe)
{
// ignore
} finally
{
try
{
if (buffreader != null)
{
buffreader.close();
}
} catch (IOException ioe)
{
// ignore
}
}
//do something with list
for (String text : list)
{
// handle one line
System.out.println(text);
}
}
}
This is the solution that I would use.
import java.util.ArrayList;
import java.util.Scanner;
public class Solution1 {
public static void main(String[] args) throws IOException{
String nameFile;
File file;
Scanner keyboard = new Scanner(System.in);
int total = 0;
System.out.println("What is the name of the file");
nameFile = keyboard.nextLine();
file = new File(nameFile);
if(!file.exists()){
System.out.println("File does not exit");
System.exit(0);
}
Scanner reader = new Scanner(file);
while(reader.hasNext()){
String fileData = reader.nextLine();
for(int i = 0; i < fileData.length(); i++){
if(Character.isDigit(fileData.charAt(i))){
total = total + Integer.parseInt(fileData.charAt(i)+"");
}
}
System.out.println(total + " \n");
}
}
}

Categories

Resources