d2jsp
d2jsp Forums > Off-Topic > Programmer's Haven > Java, Visual Basic, VBScript > How To Fix This?
Add Reply New Topic New Poll
ferf
#1 Jan 12 2021 05:43pm
Group: Member
Posts: 30,488
Joined: Mar 25 2009
Gold: 0.00
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.....
ferf
#2 Jan 12 2021 05:53pm
Group: Member
Posts: 30,488
Joined: Mar 25 2009
Gold: 0.00
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
moutonguerrier
#3 Jan 13 2021 09:28pm
Group: Member
Posts: 28,048
Joined: Apr 13 2008
Gold: 29,798.00
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;
}
ferf
#4 Jan 14 2021 07:28pm
Group: Member
Posts: 30,488
Joined: Mar 25 2009
Gold: 0.00
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
ferf
#5 Jan 14 2021 07:36pm
Group: Member
Posts: 30,488
Joined: Mar 25 2009
Gold: 0.00
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
moutonguerrier
#6 Jan 14 2021 07:37pm
Group: Member
Posts: 28,048
Joined: Apr 13 2008
Gold: 29,798.00
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
moutonguerrier
#7 Jan 14 2021 07:40pm
Group: Member
Posts: 28,048
Joined: Apr 13 2008
Gold: 29,798.00
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 Java, Visual Basic, VBScript Topic List
Add Reply New Topic New Poll