New line in different languages [closed] - java

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've encountered a problem while working on my testing system. I'm creating a file in Java, writing to it in Java, but reading from it in Pascal compilators.
So, this may be not unclear, but when I do something like this in Java (Eclipse)
File file = new File("D:/i.txt");
BufferedReader bf = new BufferedReader(new FileReader(file));
PrintWriter pw = new PrintWriter(new FileWriter(file));
pw.print("hey\n");
pw.print("you");
bf.close();
pw.close();
It gives me a file that looks like
hey
you
And when I run this code on Pascal language
begin
assign(input,'D:/i.txt'); reset(input);
while not eoln(input) do write(1);
end.
Which means: Write "1" until you find a new line separator.
It won't stop to write ones.
But this is okay. Here is another strange thing: Pascal must have a line break or a line separator, or new line indicator and I found this to be Char number 10 on ASCII table (LF, new line).
So, I decided to do other way.
File file = new File("D:/i.txt");
BufferedReader bf = new BufferedReader(new FileReader(file));
PrintWriter pw = new PrintWriter(new FileWriter(file));
pw.print("hey"+(char)10);
pw.print("you");
bf.close();
pw.close();
This one would give me the same output file as the first bit of code ( at last apparently ).
But all my Pascal compilators still keep complaining and writing hundreds of ones.
How can I solve the problem with new lines?
Thank you.

I think the infinite loop (writing hundreds of ones) is because you never read anything from the input so it is never at the end-of-line. Try putting a read(input,ch); in the loop.

Related

What would be a good program that includes writing data to a text or csv file and/or reading from a text or csv file? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I am still a beginner in java we are task to create a program that includes writing data to a text or csv file and/or reading from a text or csv file.A program that can help or any modern problem. any simple idea?
If you want to perform this task as fast as possible, I suggest you looking at this library that handles CSV file I/O operations:
Apache Commons CSV
Other resources at this link: https://github.com/akullpp/awesome-java#CSV
For a homemade solution try to implement by yourself the logic using Java Core with BufferedReader to read a file line by line, and FileWriter for write strings to append in the file.
BufferedReader example:
BufferedReader csvReader = new BufferedReader(new FileReader(pathInputFile));
while ((row = csvReader.readLine()) != null) {
// do for each row something
// if you use a csv get every values using split method:
// String[] data = row.split(",");
}
csvReader.close();
FileWriter example:
FileWriter fileWriter = new FileWriter(pathOutputFile);
// use csvWriter.append(data) for write strings
// If you want to use a csv structure:
//csvWriter.append("Value1");
//csvWriter.append(",");
//csvWriter.append("Value2");
//if a csv line is finished go down with \n token
//csvWriter.append("\n");
//when finish the write operation
csvWriter.flush();
csvWriter.close();

Is there a way on getting Scanner input without having to create an instance of the class? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
Let's say I have this code:
Scanner scan=new Scanner(System.in);
char x = 'h', aux;
System.out.println("Insert a single letter");
aux=scan.nextLine().charAt(0);
if (x==aux)System.out.println("match");
Is there a way to avoid
Scanner scannerobject = new Scanner();
and get the work done with a function tool like C's scanf() or fget()?
I was just confused about this!
You don't have to use a Scanner to get input in Java. Scanner is just a high level class for parsing input while reading it. You can read from the static InputSteam System.in. Your probably want to wrap it in to with a BufferedReader though since you likely want to deal with Strings more than byte arrays.
e.g.
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = br.readLine();
You can use a Scanner object to get input from console or from files to my knowledge but maybe more sources. If your method / class was doing both one after another repeatedly it would be useful to have separate objects to do so. Otherwise all of the configuration would have to be done each time you wanted to get input;

String buffer help needed [closed]

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 7 years ago.
Improve this question
I did exactly what has been instructed in this tutorial(https://www.youtube.com/watch?v=hyVmCqiCNmY) of stringbuffer, but I am getting an error in Netbeans with an orange line right across this line of code
StringBuffer b = New StingBuffer("how are you doing today");
whereas when Iam doing the same thing in eclipse, there is no such error
Program:
public class StringBuff {
public static void main(String[] args) {
StringBuffer b = New StingBuffer("how are you doing today");
System.out.println(b);
}
}
I tried to add the image of the program from Netbeans, but I don't qualify yet to add images.
It seems that you have typo in New. Java is case sensitive so New is different than new.
BTW StringBuffer is supplemented by StringBuilder. The orange line does not indicate an error but code that should be improved.
This class is designed for use as a drop-in replacement for StringBuffer in places where the string buffer was being used by a single thread (as is generally the case). Where possible, it is recommended that this class be used in preference to StringBuffer as it will be faster under most implementations.
Read the official Java Tutorial, not some YT video.

Efficient Way to Read a File 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 8 years ago.
Improve this question
What is the most efficient way to read input from a file ?
I have a very very large file which contains a list of words separated by a newline
e.g
computer
science
is
fun
really
I was thinking about using a BufferedReader object however I was confused by this line in the documentation.
"In general, each read request made of a Reader causes a corresponding read request to be made of the underlying character or byte stream. It is therefore advisable to wrap a BufferedReader around any Reader whose read() operations may be costly, such as FileReaders and InputStreamReaders. For example,
BufferedReader in = new BufferedReader(new FileReader("foo.in"));
will buffer the input from the specified file. Without buffering, each invocation of read() or readLine() could cause bytes to be read from the file, converted into characters, and then returned, which can be very inefficient. " <
Can some please explain this to me?
On second read I starting to believe the BufferedReader is my best bet. Is there a better way?
This post may help.
BufferedReader is a good choice, letting you turn a BufferedReader into a java.util.Stream in Java 8.
Parsing a large CSV file for instance with java.util.stream package:
InputStream is = new FileInputStream(new File("persons.csv"));
BufferedReader br = new BufferedReader(new InputStreamReader(is));
List<Person> persons = br.lines()
.substream(1)
.map(mapToPerson)
.filter(person -> person.getAge() > 17)
.limit(50)
.collect(toList());
Unlike Collections which are in-memory data structures which hold elements within it , Streams allow parallel processing and behave like fixed data structures which computes the elements on-demand basis.
Moreover, Streams also support Pipelining and Internal Iterations.

i= Integer.parseInt(dis.readLine()) [Deprecated].. How to resolve it? [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 9 years ago.
Improve this question
I have encountered the following error while compiling my Demo code on DataInputStreamDemo:
error:
i= Integer.parseInt(dis.readLine()) [Deprecated]
//where dis = reference DataInputStream obj
Reason for the method being deprecated :
This method does not properly convert bytes to characters.
Solution
Existing code : DataInputStream d = new DataInputStream(in);
Modified code : BufferedReader d
= new BufferedReader(new InputStreamReader(in));
refrences
The javadoc for this method makes it reasonably clear why it's deprecated, and has been for a long time, and suggests a better alternative.
Quoting from the Javadoc:
This method does not properly convert bytes to characters. As of JDK 1.1, the preferred way to read lines of text is via the BufferedReader.readLine() method. Programs that use the DataInputStream class to read lines can be converted to use the BufferedReader class by replacing code of the form:
DataInputStream d = new DataInputStream(in);
with:
BufferedReader d
= new BufferedReader(new InputStreamReader(in));

Categories

Resources