d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > How To Fix This?
Add Reply New Topic New Poll
Member
Posts: 31,359
Joined: Mar 25 2009
Gold: 15.00
Jan 12 2021 05:43pm
Code
package com.ferfykins.exercises;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class Main {

public static void main(String[] args) {


String path = "/home/ferfykins273/Desktop/MyJavaCode/Github exercises/EncryptDecrypt/info.txt";
readFile(path);


}


public static String[] readFile(String path) {
String line;
String[] data[] = null;
try (BufferedReader br = new BufferedReader(new FileReader(path))) {
while ((line = br.readLine()) != null) {
data = line.split(" ");
for(String stuff : data) {
System.out.print(stuff + " ");
}
System.out.println();
}


} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

return data;
}








}






So as you can see there are plenty of errors, just not sure how to fix them.
Basically what i wanna do is read a text file, and put all the contents into an array, and return that array , so i can use it in different methods.....
Member
Posts: 31,359
Joined: Mar 25 2009
Gold: 15.00
Jan 12 2021 05:53pm
I also tried:

data = line;



i get an error for that too, idk....





EDIT:
data = line.split(" ");

^I actually do not want that at all, cuz i want each line per element for array, not word per element....

This post was edited by ferf on Jan 12 2021 05:53pm
Member
Posts: 30,945
Joined: Apr 13 2008
Gold: 11,996.69
Jan 13 2021 09:28pm
Code
public static ArrayList<String> lire(String path){
ArrayList<String> res = new ArrayList<>();
try {
FileReader fileReader = new FileReader(path);
BufferedReader br = new BufferedReader(fileReader);
String ligne;

while( (ligne = br.readLine()) != null )
res.add(ligne);
br.close();
} catch (IOException e){
e.printStackTrace();
}
return res;
}
Member
Posts: 31,359
Joined: Mar 25 2009
Gold: 15.00
Jan 14 2021 07:28pm
Quote (moutonguerrier @ Jan 13 2021 11:28pm)
Code
public static ArrayList<String> lire(String path){
ArrayList<String> res = new ArrayList<>();
try {
FileReader fileReader = new FileReader(path);
BufferedReader br = new BufferedReader(fileReader);
String ligne;

while( (ligne = br.readLine()) != null )
res.add(ligne);
br.close();
} catch (IOException e){
e.printStackTrace();
}
return res;
}




Ehhh, wanna do it my way, just need help fixing my code.....




Code
package com.ferfykins.exercises;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class Test {

public static void main(String[] args) {


String path = "/home/ferfykins273/Desktop/MyJavaCode/Github exercises/EncryptDecrypt/info.txt";
readFile(path);

String[] testArray = readFile(path);


for(int i=0; i<testArray.length; i++) {
System.out.println(testArray[i]);
}


}


public static String[] readFile(String path) {
int totalLines = 0;
String line;
String[] linePerline = null;
try (BufferedReader br = new BufferedReader(new FileReader(path))) {
while ((line = br.readLine()) != null) {
totalLines++;
}

String[] linePerLine = new String[totalLines];

try (BufferedReader br = new BufferedReader(new FileReader(path))) {
while ((line = br.readLine()) != null) {
for (int i = 0; i < linePerLine.length; i++) {
linePerLine[i] = line;
}
}



} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}


} catch (IOException e) {
e.printStackTrace();
}


return linePerline;


}








}


This post was edited by ferf on Jan 14 2021 07:33pm
Member
Posts: 31,359
Joined: Mar 25 2009
Gold: 15.00
Jan 14 2021 07:36pm
Code
package com.ferfykins.exercises;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class Test {

public static void main(String[] args) {


String path = "/home/ferfykins273/Desktop/MyJavaCode/Github exercises/EncryptDecrypt/info.txt";
readFile(path);

String[] testArray = readFile(path);

for(int i=0; i<testArray.length; i++) {
System.out.println(testArray[i]);
}


}


public static String[] readFile(String path) {
int totalLines = 0;
String line;
String[] linePerline = null;
try (BufferedReader br = new BufferedReader(new FileReader(path))) {
while ((line = br.readLine()) != null) {
totalLines++;
}

String[] linePerLine = new String[totalLines];

try (BufferedReader nr = new BufferedReader(new FileReader(path))) {
while ((line = br.readLine()) != null) {
for (int i = 0; i < linePerLine.length; i++) {
linePerLine[i] = line;
}
}



} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (NullPointerException e) {
e.printStackTrace();
}


} catch (IOException e) {
e.printStackTrace();
}


return linePerline;


}








}











I think everything is correct now, but still getting a null pointer exception, how to fix?[/I][/I]

This post was edited by ferf on Jan 14 2021 07:37pm
Member
Posts: 30,945
Joined: Apr 13 2008
Gold: 11,996.69
Jan 14 2021 07:37pm
goodluck using immutable java arrays

edit: I see you already took care of that with the counter

This post was edited by moutonguerrier on Jan 14 2021 07:42pm
Member
Posts: 30,945
Joined: Apr 13 2008
Gold: 11,996.69
Jan 14 2021 07:40pm
Quote (ferf @ Jan 14 2021 08:36pm)
Code
package com.ferfykins.exercises;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class Test {

public static void main(String[] args) {


String path = "/home/ferfykins273/Desktop/MyJavaCode/Github exercises/EncryptDecrypt/info.txt";
readFile(path);

String[] testArray = readFile(path);

for(int i=0; i<testArray.length; i++) {
System.out.println(testArray[i]);
}


}


public static String[] readFile(String path) {
int totalLines = 0;
String line;
String[] linePerline = null;
try (BufferedReader br = new BufferedReader(new FileReader(path))) {
while ((line = br.readLine()) != null) {
totalLines++;
}

String[] linePerLine = new String[totalLines];

try (BufferedReader nr = new BufferedReader(new FileReader(path))) {
while ((line = br.readLine()) != null) {
for (int i = 0; i < linePerLine.length; i++) {
linePerLine[i] = line;
}
}



} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (NullPointerException e) {
e.printStackTrace();
}


} catch (IOException e) {
e.printStackTrace();
}


return linePerline;


}








}











I think everything is correct now, but still getting a null pointer exception, how to fix?[/I][/I]


use a debugger like intellij's
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll