Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
I'm working on this java program and I keep getting error from the IsLetterOrDigit method invocation, it's confusing me as I've used isLetter() already in the program and I didn't get any error from that one, somebody help me please.
import java.io.*;
import java.util.*;
import java.lang.*;
public class A11{
public static void main(String args[]) throws IOException{
BufferedReader br = new BufferedReader(new FileReader(args[0]));
BufferedWriter bw = new BufferedWriter(new FileWriter("A1.output"));
List<String> keywords = Arrays.asList("IF", "ELSE", "WRITE", "READ", "RETURN", "BEGIN", "END", "MAIN", "INT", "REAL");
List<String> l = new LinkedList<String>();
String line, word;
while((line = br.readLine()) != null){
StringTokenizer tk = new StringTokenizer(line, " ;)*,(");
while(tk.hasMoreTokens()){
word = tk.nextToken();
if(!keywords.contains(word)){
if(Character.isLetter(word.charAt(0) )){
for(int i=0; i<word.length(); ++i){
if(Character.IsLetterOrDigit(word.charAt(i))){//Error here
System.out.println(word);
}
}
}
}
}
}
}
}
Remember Java is case-sensitive:
IsLetterOrDigit(...)
must be
isLetterOrDigit(...)
change this
Character.IsLetterOrDigit(word.charAt(i))
to
Character.isLetterOrDigit(word.charAt(i))
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
Following code gives me only the first word of each line. But there are multiple words in several lines. I think, that there is be a problem in while condition.
public void nacti() throws IOException, FileNotFoundException{
if(!jeNacten){
BufferedReader reader = new BufferedReader(new FileReader("analyza.txt"));
String slovo;
StringTokenizer tokenizer;
while((slovo = reader.readLine()) != null){
tokenizer = new StringTokenizer(slovo, " //.//,");
slovo = tokenizer.nextToken();
seznamSlov.add(new Slovo(slovo));
}
reader.close();
jeNacten=true;
}
}
After slovo = reader.readLine(), how can I get all words from String slovo?
You need to iterate through all tokens.
while((slovo = reader.readLine()) != null){
tokenizer = new StringTokenizer(slovo, " //.//,");
while(tokenizer.hasMoreTokens()) {
slovo = tokenizer.nextToken();
seznamSlov.add(new Slovo(slovo));
}
}
You only have a loop that iterates through lines of input. With this approach, you need to add an inner that will loop through words on one line.
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 2 years ago.
Improve this question
How can I remove lines which contains * using core Java, for example:-
hello how are you.
what are you * doing.
In the above example, I have to remove the entire line what are you * doing which contains * in the line.
it will work ..
String str="hello how are you. \nwhat are you * doing.";
str.replace("*","");
I think this will work fine for your problem:
String s="hello how are you. \nwhat are you * doing.";
String p[]=s.split("\n");
StringBuffer sb=new StringBuffer();
for(String n:p)
{
if(n.contains("*"))
{
//do nothing
}
else{
sb.append(n);
sb.append("\n");
}
}
System.out.println(sb);
I'm new to Java so if there's something wrong pls correct me, but I would suggest if you read out of a file:
FileInputStream fstream = new FileInputStream("file.txt");
BufferedReader bf = new BufferedReader(new InputStreamReader(fstream));
String s = bf.readLine();
if(s!=null)
{
if(s.contains("*"))
{
s="";
}
else
{
//work with the rest of your line stored in String
}
}
to put it in a loop (e.g. for or while)
you can simply use string methods contains.
String s="something *";
if(s.contains(*))
{
s="";
}
else
{
///do else
}
You could use 2 lists as per this code snippet:
List<String> originalList = Arrays.asList(
"hello how are you.",
"what are you * doing.");
List<String> filteredList = new ArrayList<>();
for (String item : originalList) {
if (!item.contains("*"))
filteredList.add(item);
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I've got this question and I'm not very sure how to answer it - I am able to read the file but unsure on how to display only the words with more than 10 characters
Although other answers are correct , I recommend using scanner class to read files, as it safely allows to detect end of file conditions and has simpler/more useful utility methods:-
Scanner input = new Scanner(new File("file.txt"));
while(input.hasNextLine())
{
String word = input.nextLine();
if(word.length()>10){
System.out.println(word)
}
}
This should work:
private static void readFile(File fin) throws IOException {
FileInputStream fis = new FileInputStream(fin);
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
String line = null;
while ((line = br.readLine()) != null) {
if(line.length()>10) System.out.println(line);
}
br.close();
}
Suposing you have the word in a String variable called word:
if (word.length() > 10)
System.out.println(word);
PS: google before asking!!
Here's some pseudo code to help you get started
create empty list //where we'll add all the >10char words
read file(split per newline) //see the apache commons api
for each line
split on space
for each word in splitted sentence
if wordLength > 10 add to empty list
print each entry in your filled list
Here is your logic.
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
public class Test {
public static void main(String[] args) throws Exception{
FileInputStream fis = new FileInputStream(new File("your.txt")); // path for the text file
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
String line = null;
while ((line = br.readLine()) != null) {
String st[] = line.split(" ");
for(int i=0; i<st.length; i++){
if(st[i].length()>10) System.out.println(st[i]);
}
}
}
}
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 8 years ago.
Improve this question
Hi Have a file called read.txt and below is the data inside file.
OS:B,A,Linux,Windows 7,Windows
ARCH:32 Bit,64 Bit
Browser:Chrome,Firefox,IE
I want to read the file and want to store the data into String array for each column by spiting with
":" symbol.
example is below
String a[] = { "A","B","Linux", "Windows 7", "Windows" };
String b[] = { "32 Bit", "64 Bit"};
String c[] = { "Chrome", "Firefox" ,"IE"};
A way would be to extract eachline through ReadLine.
Once we have a string containing the line, split the line assuming that we have a single ":" as delimiter.
Extract the 2nd element of the array and do another split using "," as delimiter
Using apache commons io...
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.io.FileUtils;
public class StackOverflowExample {
public static void main(String[] args) throws IOException{
List<String> lines = FileUtils.readLines(null, "UTF-8");
List<String[]> outLines = new ArrayList<String[]>();
for(int i = 0; i < lines.size(); i++){
String line = lines.get(i);
outLines.add(line.split("[:,]"));
}
}
}
As has already been pointed out - you really should include an example of the code you are using that isn't doing what you expect it to do. If you really have no idea how to do it at all and have no code - I'm not sure this will help anyway.
Here's how you read a file:
BufferedReader reader = new BufferedReader("read.txt");
while((line = reader.readLine()) != null)
{
//process line
}
So to receive the result you want:
ArrayList<String[]> arrays = new ArrayList<String[]>;
BufferedReader reader = new BufferedReader("read.txt");
while((line = reader.readLine()) != null)
{
//process line
line = line.split(":")[1];//get the second part
arrays.add(line.split(","));//split at "," and save into the ArrayList
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I need to read lines from an input path (file).
to do so the main calls a class that uses BufferedReader , it iterates over each line and adds it to an Array.
the problem is:
I want to catch all exceptions thrown from the method in the class in the main.
public static void main (String[] args){
if (args.length != 2){
System.err.print("ERROR");
return;
}
MyFileScript.sourceDir = args[SOURCE_DIR_INDEX];
MyFileScript.commandFile = args[COMMAND_INDEX];
try (FileReader file = new FileReader(MyFileScript.commandFile);
BufferedReader reader = new BufferedReader(file)){
fileParsing = new CommandFileParser(reader);
sectionList = fileParsing.parseFile();
}catch (FileNotFoundException error){
System.err.print(ERROR_MESSAGE);
return;
}catch(IOException error){
System.err.print(ERROR_MESSAGE);
return;
}catch(ErrorException error){
System.err.print(error.getMessage());
return;
}
}
public class CommandFileParser {
public CommandFileParser (BufferedReader reader){
this.reader = reader;
}
/**
* read all lines from a file.
*
* #return a string array containing all file lines
*/
public String[] readFileLines(){
ArrayList<String> fileLines = new ArrayList<String>();
String textLine;
while ((textLine = this.reader.readLine()) != null){
fileLines.add(textLine);
}
String[] allFileLines = new String[fileLines.size()];
fileLines.toArray(allFileLines);
return allFileLines;
}
in the while loop I get a compilation error for unhandling the IOException.
How can I catch all exceptions in main,
and so the class takes only one string argument?
your readFileLines method is lacking a throws clause.
public String[] readFileLines() throws IOException {