In a part of my program I am reading lines that contain "ua, " and setting them equal to however many lines I want to process. I want to use arrays to make this flexible to however many lines I want.
This is how it works with 4 lines
instead of having multiple else if statements, I want to simplify this so that I can define a number of lines I want to process and not have to edit this part
try (BufferedReader br = new BufferedReader(new FileReader(f.getAbsolutePath()))) {
String line1 = null, line2 = null, line3 = null, line4 = null, line = null;
boolean firstLineMet = false;
boolean secondLineMet = false;
boolean thirdLineMet = false;
while ((line = br.readLine()) != null) {
if (line.contains("ua, ")) {
if (!firstLineMet) {
line1 = line;
firstLineMet = true;
} else if (!secondLineMet) {
line2 = line;
secondLineMet = true;
} else if (!thirdLineMet) {
line3 = line;
thirdLineMet = true;
} else {
line4 = line;
ProcessLines(uaCount, line1, line2, line3, line4);
line1 = line2;
line2 = line3;
line3 = line4;
}
}
}
}
Alternative you can do following to achieve your goal.
int counter = 0;
int limit = 3; // set your limit
String[] lines = new String[limit];
boolean[] lineMet = new boolean[limit];
while ((line = br.readLine()) != null) {
if (line.contains("ua, ")) {
lines[counter] = line;
lineMet[counter] = true; // doesn't make any sense, however
counter++;
}
if (counter == limit){
// tweak counter otherwise previous if will replace those lines with new ones
counter = 0;
ProcessLines(uaCount, lines); // send whole array
lines[0] = lines[1]; // replace first line with second line
lines[1] = lines[2]; // replace second line with third line
lines[2] = lines[3]; // replace third line with fourth line
// ProcessLines(uaCount, lines[0], lines[1], lines[2], lines[3]);
// Do Something
}
}
I hope this will help you.
Assuming reading the whole file in memory is ok, you could use the convenience methods provided by Files:
List<String> lines = Files.readAllLines(yourFile, charset);
ProcessLines(uaCount, lines.get(0), lines.get(1), ...);
Or if you want to process the lines sequentially, but only up to a certain limit:
for (int i = 0; i < limit && i < lines.length(); i++) {
processLine(lines.get(i));
}
Related
One part of what I'm working on requires me to prompt the user to choose if they want a .txt file to be organized via the last name alphabetically or vice versa. I've gotten all the code down so my trouble isn't in editing or changing my .txt file but I want to prevent the user from selecting last name if last name has already been applied or vice versa. Basically, like a light switch, where if it's on (organized first name) then the only option is off (organize by last name).
I'm not sure how to best describe this, but if you run the code firstOrLast.equals("last") then run firstOrLast.equals("first") it will do nothing, but you can continue to run "last". I believe my error involves how I assign the boolean when checking what its current value is.
public static boolean isFirst = true;
public static void sortPatientsByName(String firstOrLast, String fileName) throws IOException {
if (firstOrLast.equals("last")) { // CODE FOR LAST NAME, FIRST NAME
if (isFirst == true) { // BOOLEAN TO SEE IF OTHER HAS BEEN CALLED
FileReader fileReader = new FileReader(fileName);
BufferedReader bufferedReader = new BufferedReader(fileReader);
String[] words = null;
ArrayList<String> sortName = new ArrayList<>();
String line = "";
while ((line = bufferedReader.readLine()) != null) {
words = line.split(" ");
String swapFirst = "";
String swapLast = "";
String holdBirth = "";
String holdStatus = "";
swapFirst = words[1];
swapLast = words[0];
holdBirth = words[2];
holdStatus = words[3];
line = swapFirst + " " + swapLast + " " + holdBirth + " " + holdStatus;
sortName.add(line);
}
bufferedReader.close();
Collections.sort(sortName);
FileWriter writer = new FileWriter(fileName);
for (int i = 0; i < sortName.size(); i++) {
writer.write(sortName.get(i));
writer.write("\r\n");
}
writer.close();
//isFirst = false; // NOT SURE IF ASSIGNING BOOLEAN FALSE SHOULD BE CALLED WITHIN
}
isFirst = false;
}
if (firstOrLast.equals("first")) { // CODE FOR FIRST NAME, LAST NAME
if (isFirst == false) { // BOOLEAN TO SEE IF OTHER HAS BEEN CALLED
FileReader fileReader = new FileReader(fileName);
BufferedReader bufferedReader = new BufferedReader(fileReader);
ArrayList<String> sortName = new ArrayList<>();
String line = "";
while ((line = bufferedReader.readLine()) != null) {
sortName.add(line);
}
bufferedReader.close();
Collections.sort(sortName);
FileWriter writer = new FileWriter(fileName);
for (int i = 0; i < sortName.size(); i++) {
writer.write(sortName.get(i));
writer.write("\r\n");
}
writer.close();
//isFirst = true;
}
isFirst = true;
}
}
The issue here is that when you start your code the only thing that can run is the "first" part.
You can try to change the boolean type to Boolean and set it to null like :
static Boolean isFirst = null
Then in your code you check if boolean is null (for the first time is run) and at then end of the run set it to the corresponding value:
static Boolean isFirst = null;
public static void sortPatientsByName() throws IOException {
if (firstOrLast.equals("last")) {
if (isFirt == null || isFirst == false) {
***
isFirst = false;
}
if (firstOrLast.equals("first")) {
if (isFirt == null || isFirst == true) {
***
isFirst = true;
}
}
That way when you'll run it it pass the first time due to null and after it can only pass to the first one it pass through
One cleaner way would be to create a enum for the different possibility
This is my code, I am using bufferedreader to read from a file. It stores the values it reads into the array, but when i try to print out the array it returns null values. Why does this happen? Code:
BufferedReader reader = new BufferedReader(new FileReader("valid file path here"));
int lines = 0;
while (reader.readLine() != null) {
lines++;
}
//declare and fill array
String[] coin_names = new String[lines];
String line;
int x = 0;
while ((line = reader.readLine()) != null) {
coin_names[x] = line;
x++;
}
for (int y = 0; y < lines; y++) {
System.out.println(coin_names[y]);
}
Why does it return null for all of the values it gets?
Here is the problem:
while (reader.readLine() != null) {
lines++;
}
Your initial while loop is consuming the entire file. A better approach would be to remove it, and instead just use a list to store the lines:
List<String> coinNames = new ArrayList<>();
String line;
int x = 0;
while ((line = reader.readLine()) != null) {
coinNames.add(line);
x++;
}
for (String name : coinNames) {
System.out.println(name);
}
While you could try to reset the reader, there is no reason why you should have to read the entire file twice just to intialize an array. Use the right data structure for the job.
Ok now, here's my question. I wrote an algorithm to do specific things. Currently I create my processes myself in the class constructor and store them in a priority queue. However I want to be able to write a .txt file with multiple lines. Each line will represent a process with its different attributes separated by space. Here's what my .txt will look like:
P1 0 8
P2 1 4
P3 2 9
P4 3 3
END 4 9999
p1, p2... etc are the names of each process. Then the second column is the first attribute and the third column is the second attribute.
I need to be able to read each column at a time and store the value in my processes. How can I read those values and distinguish between them? (treat them as separate things)
So you want to read the file line-by-line and separate each line?
BufferReader in=new BufferedReader...
String line;
while ((line=in.readLine())!=null) {
String[] data=line.split(" ");
//now, data will be a array which contains the data
//data[0] = the first item in the line
//data[1] = the first number
//data[2] = the second number
}
Have a look at the java.util.Scanner class, it can help to read separate tokens from a Reader.
It has methods to read the next token as an integer, as a string or many other types. There are also some examples in the class Javadoc...
You got both whitespace (seperating the attributes) and new line (seperates the whole process information) as seperators.
Using a BufferedReader, you could either read a whole line (reader.readLine()) to parse one whole process information and use String.split() to seperate the attributes (edit: see answer from dyslabs).
An obviously more performant (but less intuitive) approach is to read single characters (reader.read()) and check if you either read a whitespace- or a new-line-character:
// caution: code is not tested but shows the general approach
List<ProcessInformation> processInfo = new ArrayList<>();
String pInfoStr = new String[3];
int processInfoIndex = 0;
String[] processInfoHolder = new String[3];
String processInfo = "";
int c;
while( (c = reader.read()) != -1 ) {
if (Character.isWhitespace(c)) {
processInfoHolder[processInfoIndex++] = processInfo;
processInfoStr = "";
}
else if (c == 10) { // not sure if correct codepoint for whitespace
processInfo.add(new ProcessInfo(processInfoHolder));
processInfoIndex = 0;
}
else {
processInfoStr += c;
}
}
You could even more optimize this method by using StringBuilder.
In order to be able to read a file line by line I use readLine() != null while in order to retrieve the values separated by whitespace, use the split method and store each value of a single line in an array,
here's how I implemented your example:
public static void main(String[] args) {
// TODO Auto-generated method stub
BufferedReader buffer;
FileReader fileReader;
String p1[] = new String[4];
String p2[] = new String[4];
String p3[] = new String[4];
String p4[] = new String[4];
String end[] = new String[4];
try {
fileReader = new FileReader(new File("file.txt"));
buffer = new BufferedReader(fileReader);
String line;
line = buffer.readLine();
// ============= Read the fist line =============
p1 = line.split("\\s+");
while((line = buffer.readLine()) != null) {
// ============= Read the second line =============
p2 = line.split("\\s+");
// ============= Read the third line =============
if((line = buffer.readLine()) != null) {
p3 = line.split("\\s+");
}
// ============= Read the forth line =============
if((line = buffer.readLine()) != null) {
p4 = line.split("\\s+");
}
// ============= Read the last line =============
if((line = buffer.readLine()) != null) {
end = line.split("\\s+");
}
}
fileReader.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int v1[] = new int[3];
int v2[] = new int[3];
int v3[] = new int[3];
int v4[] = new int[3];
int v_end[] = new int[3];
for (int i = 0 ; i < p1.length; i++)
System.out.print(p1[i]+ " ");
System.out.println();
for (int i = 0 ; i < p2.length; i++)
System.out.print(p2[i]+ " ");
System.out.println();
for (int i = 0 ; i < p3.length; i++)
System.out.print(p3[i]+ " ");
System.out.println();
for (int i = 0 ; i < p4.length; i++)
System.out.print(p4[i]+ " ");
System.out.println();
for (int i = 0 ; i < end.length; i++)
System.out.print(end[i]+ " ");
}
sorry about the poor title, didn't quite know what to call it!
basically I made the loop below to show the first six lines from the file, got that sorted. When it comes to showing the six lines though, I'm not sure how to get them to appear on a different line each time. The closest I got, was including the joptionpane in the loop, showing one line, then on the next joptionpane on the next et al. The second joptionpane at the bottom shows all the lines but on the same line instead of the next etc. How ought I make it so they appear on the next line each time? \n doesn't seem to work.
private static void doOptionTwo(int balance) throws IOException {
JOptionPane.showMessageDialog(null, "Option two selected ");
String sum = null;
BufferedReader br = null;
br = new BufferedReader(new FileReader("file1.txt"));
for (int i = 1; i <= 6; i++){
String line1 = br.readLine();
//JOptionPane.showMessageDialog(null, line1);
sum = sum + line1;
}
if (br != null)br.close();
String log = sum;
JOptionPane.showMessageDialog(null, log);
}
Use StringBuilder instead of String which is initialized as null. You could do whatever you want with following code:
StringBuilder stringBuilder = new StringBuilder();
String newLineCharacter = System.getProperty("line.separator");
for (int i = 1; i <= 6; i++){
stringBuilder.append(br.readLine());
stringBuilder.append(newLineCharacter);//note: will add new line at end as well..
}
Just insert break each time in your String :
for (int i = 1; i <= 6; i++) {
String line1 = br.readLine();
sum += line1 + "\n";
}
You can just add "\n" between the lines.
String sum = "";
for (int i = 1; i <= 6; i++){
String line1 = br.readLine();
sum += line1 + "\n";
}
Or more appropriately, use a StringBuilder.
StringBuilder sb = new StringBuilder();
for (int i = 1; i <= 6; i++){
String line1 = br.readLine();
if (sb.length() > 0) {
sb.append('\n');
}
sb.append(line1);
}
String sum = sb.toString();
This is more efficient :
JOptionPane.showMessageDialog(null, "Option two selected ");
StringBuilder build = new StringBuilder();
BufferedReader br = null;
br = new BufferedReader(new FileReader("file1.txt"));
for (int i = 1; i <= 6; i++){
String line1 = br.readLine();
//JOptionPane.showMessageDialog(null, line1);
build.append(sum).append("\n");
}
if (br != null)br.close();
System.out.println(build.toString());
i have the file which has data stored as " Integer-> \t(tab)-> String ->couple of space-> ".
Am I doing Wrong?
What I am doing is.
Trie t = new Trie();
BufferedReader bReader = new BufferedReader(new FileReader(
"H:\\100kfound.txt"));
String line;
String[] s = null;
while ((line = bReader.readLine()) != null) {
s = line.split("\t");
}
int i;
for (i = 0; i < s.length; i++) {
System.out.println(s[i]);
if (!(s[i].matches("\\d+"))) {
t.addWord(s[i]);
System.out.println(s[i]);
}
}
What I can see by debugging it is going properly till while loop but in for loop it just stores two strings and prints the same.
You might want to and a ^[0-9]+$ for the expressions so you just get complete integers. Without the ^ and $ you could be matching other characters like tt55gh would match.
if (!(s[i].matches("^[0-9]+$"))) {
}
Per the comment above you need to move the for loop inside the while loop.
while ((line = bReader.readLine()) != null) {
s = line.split("\t");
for (int i = 0; i < s.length; i++) {
System.out.println("Value "+i+": "+s[i]);
if (!(s[i].matches("^[0-9]+$"))) {
t.addWord(s[i]);
System.out.println("Integer "+i+": "+s[i]);
}
}
}