I have a text file in which I want to read in, remove all non-alphabetic characters and white space including the empty lines. Then convert the text to lowercase. This is what I have so far in terms of the code:
public static String replace(String file ){
String plaintext = "";
try{
Scanner input = new Scanner(new File(file));
while(input.hasNext()){
//text = text + input.nextLine();
plaintext = input.nextLine();
plaintext = plaintext.replaceAll("[^a-zA-Z]", "");
plaintext = plaintext.toLowerCase();
System.out.println(plaintext);
}
input.close();
}
catch(FileNotFoundException e){
System.out.println("File not found. ");
}
return "";
}//end of replace method
The only problem I'm facing is that I am not sure how to removed the black lines of spaces in between each paragraph of the text file. My output shows like this:
csthesciencethatdealswiththetheoryandmethodsofprocessinginformationindigitalcomputersthedesignofcomputerhardwareandsoftwareandthe
applicationsofcomputers
itthedevelopmentimplementationandmaintenanceof
computerhardwareandsoftwaresystemstoorganizeandcommunicateinformation
electronicallyabbreviationit
computersaremanmadetoolsthataidusinsolvingotherproblemsabiologististryingtofigureouthowlifeworksphysicistsandchemistsaretryingtofigureouthowitemsreactinouruniversemathematiciansaretryingtofigureoutrulesformanmadesystems
anyresearchproblemthatmayimproveacomputerscapabilityofhelpingsolveaproblemoranyresearchproblemthatshedslightaboutanewwaytodosomethingwithacomputerispartofcs
mostexcitingresearchmedicalapplicationsexpertsystemsfordiagnosis
The code below should work; please note that it makes use of the JSR 203 API as far as file handling is concerned (since Java 7; in store for 10+ years for you to use) and of Java 8 for streams and their associated methods. Also note that it won't work with code points outside the BMP:
public static String trimFile(final String file)
throws IOException
{
final StringBuilder sb = new StringBuilder();
final Path path = Paths.get(file);
try (
final Reader r = Files.newBufferedReader(path);
) {
int c;
while ((c = r.read()) != -1)
if (Character.isLetter(c))
sb.appendCodePoint(c);
}
return sb.toString();
}
A little explanation here:
we don't need anything else than a Reader given the requirements; and even though Files.newBufferedReader() returns a BufferedReader, we don't care about reading line by line, therefore we downgrade it to a Reader, and we trust the JRE implementation to do its thing;
the read() method of a Reader returns an int... This is fine given the requirements (ie, we should not expect code points outside the BMP);
the Character.isLetter() implementation used is the one that takes an int as an argument, well, so what: as stated in the previous point, we don't expect code points outside the BMP, in which case this method behaves the same way as its counterpart expecting a char, so no harm done;
we do, however, have to use the appendCodePoint of StringBuilder; this class' .append method taking an int as an argument will append the string representation of the integer as an argument, but this is not what we want.
Related
public class Example {
public static void main(String[] args) throws IOException {
byte[] bytes = new byte[100];
InputStreamReader fileInputStream = new InputStreamReader(new FileInputStream("/Users/deni/Desktop/Input.txt"));
while (fileInputStream.read() != -1) {
int i = 0;
bytes[i] = (byte) fileInputStream.read();
i++;
}
String string = new String(bytes, StandardCharsets.UTF_8);
System.out.println(string);
}
}
File contains only just number.But when I run this method I get question mark.
What is source of this problem and how to solve it? I am from Russia.I have read some other posts related with this problems but it doesn't help me.I tried write javac -J-Duser.language=en Example java.
Thank you.
You either want to use a Reader and read diretly into char[]/String (the preferred method) or use an InputStream, read into a byte[] and transfer that into a String later on. You combine both, effectively double-decoding the input, which might accidentally work for pure ASCII text, but will mangle all other text.
You don't actually fill the byte[] array because you declare int i = 0 inside the loop meaning you'll only ever fill the byte[0] with a value.
You ignore every second read() result by calling read() twice in your loop and checking one return value for -1 and storing the other in bytes[]. Instead, you want to call read() once, assign it to a variable, check that variable for -1 and if it isn't use that variable to store in the target.
Files.readString(Path, Charset) does everything you're trying to do without having to implement it manually.
But in the interest of actually interacting with your code, this is a functioning method with as few changes as possible:
char[] chars = new char[100];
InputStreamReader reader = new InputStreamReader(new FileInputStream("/Users/deni/Desktop/Input.txt"), StandardCharsets.UTF_8);
int i = 0;
int c;
while ((c = reader.read()) != -1) {
chars[i] = (char) c;
i++;
}
String string = new String(chars, 0, i);
System.out.println(string);
This is still needlessly complicated, breaks if the input is more than 100 chars and produces weird output if it's less than 100. But should demonstrated the necessary changes.
I am writing a program which will allow users to reserve a room in a hotel (University Project). I have got this problem where when I try and read data from the file and store it in an array I receive a NumberFormatException.
I have been stuck on this problem for a while now and cannot figure out where I am going wrong. I've read up on it and apparently its when I try and convert a String to a numeric but I cannot figure out how to fix it.
Any suggestions, please?
This is my code for my reader.
FileReader file = new FileReader("rooms.txt");
Scanner reader = new Scanner(file);
int index = 0;
while(reader.hasNext()) {
int RoomNum = Integer.parseInt(reader.nextLine());
String Type = reader.nextLine();
double Price = Double.parseDouble(reader.nextLine());
boolean Balcony = Boolean.parseBoolean(reader.nextLine());
boolean Lounge = Boolean.parseBoolean(reader.nextLine());
String Reserved = reader.nextLine();
rooms[index] = new Room(RoomNum, Type, Price, Balcony, Lounge, Reserved);
index++;
}
reader.close();
This is the error message
This is the data in my file which I am trying to read:
Change your while loop like this
while (reader.hasNextLine())
{
// then split reader.nextLine() data using .split() function
// and store it in string array
// after that you can extract data from the array and do whatever you want
}
You're trying to parse the whole line to Integer. You can read the whole line as a String, call
.split(" ")
on it. This will split the whole line into multiple values and put them into an array. Then you can grab each item from the array and parse separately as you intended.
Please avoid posting screenshots next time, use proper formatting and text so someone can easily copy your code or test data to IDE and reproduce the scenario.
Use next() instead of nextLine().
With Scanner one must use hasNextLine, nextLine, hasNext, next, hasNextInt, nextInt etcetera. I would do it as follows:
Using Path and Files - the newer more general classes i.o. File.
Files can read lines, here I use Files.lines which gives a Stream of lines, a bit like a loop.
Try-with-resources: try (AutoCloseable in = ...) { ... } ensures that in.close() is always called implicitly, even on exception or return.
The line is without line ending.
The line is split into words separated by one or more spaces.
Only lines with at least 6 words are handled.
Create a Room from the words.
Collect an array of Room-s.
So:
Path file = Paths.get("rooms.txt");
try (Stream<String> in = Files.lines(file)) {
rooms = in // Stream<String>
.map(line -> line.split(" +")) // Stream<String[]>
.filter(words -> words.length >= 6)
.map(words -> {
int roomNum = Integer.parseInt(words[0]);
String type = words[1];
double price = Double.parseDouble(words[2]);
boolean balcony = Boolean.parseBoolean(words[3]);
boolean lounge = Boolean.parseBoolean(words[4]);
String reserved = words[5];
return new Room(roomNum, type, price, balcony, lounge, reserved);
}) // Stream<Room>
.toArray(Room[]::new); // Room[]
}
For local variables use camelCase with a small letter in front.
The code uses the default character encoding of the system to convert the bytes in the file to java Unicode String. If you want all Unicode symbols,
you might store your list as Unicode UTF-8, and read them as follows:
try (Stream<String> in = Files.lines(file, StandardCharsets.UTF_8)) {
An other issue is the imprecise floating point double. You might use BigDecimal instead; it holds a precision:
BigDecimal price = new BigDecimal(words[2]);
It is however much more verbose, so you need to look at a couple of examples.
Hi guys I'm writing a method which counts words in a file, but apparently there is a mistake somewhere in the code and the method does not work. Here's my code:
public class Main2 {
public static void main(String[] args) {
count("/home/bruno/Desktop/WAR_JEE_S_09_Podstawy/MojPlik");
}
static int count(String fileName){
Path path = Paths.get(fileName);
int ilosc = 0;
String wyjscie = "";
try {
for (String charakter : Files.readAllLines(path)){
wyjscie += charakter;
}
StringTokenizer token = new StringTokenizer(wyjscie," \n");
} catch (IOException e) {
e.printStackTrace();
}
return ilosc;
}
}
The file path is correct, here is the file content
test test
test
test
after i call the method in main it displays nothing. Where is the mistake ?
Your code would count lines in a file ... well, if you followed up on that thought.
Right ow your code is simply reading lines, putting them into one large string, to then do nothing about the result of that operation. You have a single int counter ... who is init'ed to 0, and then just returned without ever being used/increased! And unless I am mistaken, readAllLines() will automatically remove the newline char in the end, so overall, your code is nothing but useless.
To count words you have to take each line and (for example) split that one-line-string for spaces. That gives you a number. Then add up these numbers.
Long story short: the real answer here is that you should step back. Don't just write code, assuming that this will magically solve the problem. Instead: first think up a strategy (algorithm) that solves the problem. Write down the algorithm ideas using a pen and paper. Then "manually" run the algorithm on some sample data. Then, in the end, turn the algorithm into code.
Also, beside that you does not output anything, there is a slight error behind you logic. I have made a few changes here and there to get your code working.
s.trim() removes any leading and trainling whitespace, and trimmed.split("\\s+") splits the string at any whitespace character, including spaces.
static int count(String fileName) throws IOException {
Path path = Paths.get(fileName);
int count = 0;
List<String> lines = Files.readAllLines(path);
for (String s : lines) {
String trimmed = s.trim();
count += trimmed.isEmpty() ? 0 : trimmed.split("\\s+").length;
}
return count;
}
Here is the code using functional-style programming in Java 8. This is also a common example of using Stream's flatMap - may be used for counting or printing words from a file.
long n = Files.lines(Paths.get("test.txt"))
.flatMap(s -> Stream.of(s.split("\\s+")))
.count();
System.out.println("No. of words: " + n);
Note the Files.lines(Path) returns a Stream<String> which has the lines from the input file. This method is similar to readAllLines, but returns a stream instead of a List.
I'm coding in Java and I want to split my string. I want to split it at.
/* sort */
Yes I plan to split a .java file that I have read as a string so I need it to include "/* sort */". I'm creating a code that sorts Arrays that are predefined in java class file.
Exactly that and do another split at
}
and then I wanted help how to go about splitting up the array since I'll be left with
an example would be this
final static String[] ANIMALS = new String[] /* sort */ { "eland", "antelope", "hippopotamus"};
My goal would be to sort that Array inside a .java file and replace it. This is my current code
private void editFile() throws IOException {
//Loads the whole Text or java file into a String
try (BufferedReader br = new BufferedReader(new FileReader(fileChoice()))) {
StringBuilder sb = new StringBuilder();
String line = br.readLine();
while (line != null) {
sb.append(line);
sb.append(System.lineSeparator());
line = br.readLine();
}
everything = sb.toString();
}
arrayCutOff = everything.split("////* sort *////");
for(int i = 0; i < arrayCutOff.length; i++){
System.out.println(arrayCutOff[i]);
}
}
This basically reads the whole .txt or .java file completely with the exact same formatting into one string. I planned to split it at /* sort */ and sort the array inside but I realized if I did that I probably can't replace it.
Considered your're using java 8 you might go this direction:
private void editFile() throws IOException {
List<String> lines = Files.readAllLines(Paths.get(fileChoice()));
String content = lines.stream().collect(Collectors.joining(System.lineSeparator()));
Stream.of(content.split(Pattern.quote("/* sort */"))).forEach(System.out::println);
}
However, the trick you're asking for is Pattern.quote, which dates back Java 5. It'll qoute a literal so it can be used as a literal in regExs and is a bit more convenient (and reliable I think) than wrestling around with backslashes...
So I have a .txt file with only this as the contents:
pizza 4
bowling 2
sleepover 1
What I'm trying to do is, for example in the first line, ignore the "pizza" part but save the 4 as an integer.
Here is the little bit of code I have so far.
public static void addToNumber() {
PrintWriter writer;
Int pizzaVotes, bowlingVotes, sleepOverVotes;
try {
writer = new PrintWriter(new FileWriter("TotalValue.txt"));
}
catch (IOException error) {
return;
}
// something like if (stringFound)
// ignore it, skip to after the space, then put the number
// into a variable of type int
// for the first line the int could be called pizzaVotes
// pizzaVotes++;
// then replace the number 4 in the txt file with pizzaVote's value
// which is now 5.
// writer.print(pizzaVotes); but this just overwrites the whole file.
// All this will also be done for the other two lines, with bowlingVotes
// and sleepoverVotes.
writer.close();
} // end of method
I am a beginner. As you can see my actual, functioning code is very short and I don't know to proceed. If anyone would be so kind as to point me in the right direction, even if you just give me a link to a site, it would be extremely helpful...
EDIT: I stupidly thought PrintWriter could read a file
It's pretty simple actually. All you need is a Scanner, and it's function nextInt()
// The name of the file which we will read from
String filename = "TotalValue.txt";
// Prepare to read from the file, using a Scanner object
File file = new File(filename);
Scanner in = new Scanner(file);
int value = 0;
while(in.hasNextLine()){
in.next();
value = in.nextInt();
//Do something with the value here, maybe store it into an ArrayList.
}
I have not tested this code, but it should work, but the value in the while loop is going to be the current value of the current line.
I don't fully understand your question, so comment if you want some clearer advice
Here is a common pattern you'll use in Java:
Scanner sc=new Scanner(new File(.....));
while(sc.hasNextLine(){
String[] line=sc.nextLine().split("\\s");//split the string up by writespace
//....parse tokens
}
// now do something
In your case, it seems like you want to do something like:
Scanner sc=new Scanner(new File(.....));
FrequencyCloud<String> votesPerActivity=new FrequencyCloud<String>()
while(sc.hasNextLine(){
String[] line=sc.nextLine().split("\\s");//split the string up by writespace
//if you know the second token is a number, 1st is a category you can do
String activity=line[0];
int votes=Integer.parseInt(line[1]);
while(votes>0){
votesPerActivity.incremendCloud(activity);//no function in the FrequencyCloud for mass insert, yet
votes--;
}
}
///...do whatever you wanted to do,
//votesPerActivity.getCount(activity) gets the # of votes for the activity
/// for(String activity:votesPerActivity.keySet()) may be a useful line too
FrequencyCloud: http://jdmaguire.ca/Code/JDMUtil/FrequencyCloud.java
String num = input.replaceAll("[^0-9]", " ").trim();
For sake of diversity this uses regular expressions.