BufferedReader gives null when file is not in Documents [closed] - java

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 5 years ago.
Improve this question
I am working on a text editor software(kinda like notepad) and whenever I open files from Documents, correct data is displayed but at any other location Null is returned

This is because bff.readLine() is returning null. According to the documentation, it returns null if the end of the stream has been reached.
The previous check bff.readLine() != null doesn't help, because each call advances the reader. Try it like that:
String line;
while ((line = bff.readLine()) != null) {
sk += line + "\n";
}

Related

Large string input isn't being read completely, on an online judge [closed]

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 4 days ago.
Improve this question
There's a question on Codechef. A test case in this, where a string of length 100000, is being input. But when i print the length of string after input, its only coming out to be 65526. Why is that so? Is the input not being taken properly?
For input I use,
JAVA CODE:
BufferedReader br = new BufferedReader(new InputStreamReader(System.in), 131072);
int n = Integer.parseInt(br.readLine());
String c = br.readLine();
NOTE : I also tried doing it in C++, but same problem occurs there.
I also tried, increasing the buffer size in case it causes problem, but no effect.
Also, Scanner didn't work in java!
Can someone please tell me what's going wrong?

How to get postfix expression from parse tree? [closed]

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 4 months ago.
Improve this question
public String auxToPostfixString(Node root) {
String result = "";
if (root == null) {
return "";
}
result += auxToPostfixString(root.getLeft());
result += auxToPostfixString(root.getRight());
result += root.getExp();
return result;
}
I used this code for that, and it should return 342*+8+ but it returns 34+2*8+ (the original expr is 3+4*2+8) What's wrong about this?
Sorry for bad English
it should return 342*+8+ but it returns 34+2*8+ (the original expr is 3+4*2+8)
the problem may come from the original parsed tree : was the priority of multiplication against addition well applied ? Make sure that the tree is not, in fact, equivalent to ((3+4)*2+8).

Read a csv file into one String [closed]

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 9 months ago.
Improve this question
I want to essentially read the contents of csv file and want to input into one string to return. I tried this and it doesnt quite work:
File file = new File(aaa.csv);
FileWriter fw = new FileWriter(file);
BufferReader bw = new BufferReader(fw);
String s;
while (bw.readLine() != null) {
s += (bw.readLine());
}
fw.close();
Is there a easier solution to reading csv file into string that works?
To read the entire file, assuming Java 11 and above, you can simply do:
String content = Files.readString(Paths.get("aaa.csv"));
note that if the file is very large memory becomes a problem.

How to avoid else condition with ?(ternary operator) in one line [closed]

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 4 years ago.
Improve this question
Suppose that i have given below syntax:
Boolean isCapital = city.isCapital();
String isCapitalName;
if(isCapital == null) {
isCapitalName = "";
}
Means that i do not want else condition in my short for(The header of this blog) then what should be the syntax.
I want to minimize the use of if else condition in my project so that i want to use on liner if else.
Please guide.
You can set isCapitalName like so:
String isCapitalName = isCapital == null ? "" : null;
This has an identical behavior to your current code. It sets it to:
an empty string "" if isCapital == null
null by default
Edit taking into account #khelwood's comment:
The default value of an uninitialized local variable is not actually null, but it would cause an error if you used it in your code. I'm not sure why you would leave it uninitialized, however -- you probably want to choose a default value to put in the second clause of the ternary.

how to split a text file by line gaps in java [closed]

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 5 years ago.
Improve this question
I am reading a text file in Java that looks like this,
"
Q1. You are given a train data set having 1000 columns and 1 million rows. The data set is based on a classification problem. Your manager has asked you to reduce the dimension of this data so that model computation time can be reduced. Your machine has memory constraints. What would you do? (You are free to make practical assumptions.)
Q2. Is rotation necessary in PCA? If yes, Why? What will happen if you don’t rotate the components?
Q3. You are given a data set. The data set has missing values which spread along 1 standard deviation from the median. What percentage of data would remain unaffected? Why? "
Now, I want to read this file and then store each of these sentences(questions) in a string array. How can I do that in java?
I tried this,
String mlq = new String(Files.readAllBytes(Paths.get("MLques.txt")));
String[] mlq1=mlq.split("\n\n");
But this is not working.
Try this
String mlq = new String(Files.readAllBytes(Paths.get("MLQ.txt")));
String[] mlq1=mlq.split("\r\n\r\n");
System.out.println(mlq1.length);
System.out.println(Arrays.toString(mlq1));
This should do it by line gap of 2 lines.
File file = new File("C:\\MLques.txt");
BufferedReader br = new BufferedReader(new FileReader(file));
String st;
while ((st = br.readLine()) != null) {
System.out.println(st + "\n");
}
I think it will work.
This is a piece of code from one of my project.
public static List<String> readStreamByLines(InputStream in) throws IOException {
return IOUtils.readLines(in, StandardCharsets.UTF_8).stream()
.map(String::trim)
.collect(Collectors.toList());
}
But!!! If you have really big file, then collecting all content into a List is not good. You have to read InputStream line by line and do all you need for every single row.

Categories