Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 24 of 24

Thread: From txt file to array.

  1. #1
    Member MrLowBot's Avatar
    Join Date
    Nov 2018
    Location
    Sweden
    Posts
    130
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default From txt file to array.

    Hello there.

    I have an assignmet where i have too make it so that i can read a txt file and turn it into an array and then print out the numbers.

    I have numbers between 1 and 100, all random numbers, within a txt file.
    The problem that i have atm is that i cannot print out anything from my arraylist.
    I am doing something wrong but cannot tell what exactly, any ideas?
    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.util.ArrayList;
     
    public interface Histrogram {
    	// throws Exception is there to check if the file actually exists and if it
    	// doesn't then it will tell us that by an error.
    	public static void main(String[] args) throws Exception {
     
    		// read();
     
    	}
     
    	static void read() throws Exception {
     
    		ArrayList<Integer> list = new ArrayList<Integer>();
    		// This method just reads a file.
     
    		// The path that we want to read from.
    		FileReader file = new FileReader("C:\\Users\\mrLowBot\\Desktop\\heltal.txt");
    		// Reading it by BufferedReader.
    		BufferedReader read = new BufferedReader(file);
    		int[] data = new int[100];
    		String text = "";
    		int data1 = 0;
    		int i = 0;
    		// Actually reading it here.
    		String line = read.readLine();
    		while (line != null) {
    			text += line;
    			// line = read.readLine();
    			data1 = Integer.valueOf(read.readLine());
    			list.add(data1);
     
    		}
    		// System.out.println(text);
    		System.out.println();
    		for (int k : list) {
    			System.out.println(k);
    		}
    	}
     
    }

    PS: Some code is under the "//" lines but that's only because i have tried to only print the numbers as they are and without trying to add them into an array and that worked fine.

  2. #2
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: From txt file to array.

    Did you see Norm's comment on your previous thread -> http://www.javaprogrammingforums.com...ere-begin.html

    Regards,
    Jim

  3. #3
    Member MrLowBot's Avatar
    Join Date
    Nov 2018
    Location
    Sweden
    Posts
    130
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: From txt file to array.

    Quote Originally Posted by jim829 View Post
    Did you see Norm's comment on your previous thread -> http://www.javaprogrammingforums.com...ere-begin.html

    Regards,
    Jim
    Just saw it. Trying to do it in that way.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: From txt file to array.

    I am doing something wrong but cannot tell what exactly
    Please explain what happens when you compile and execute the program.
    If there are error messages, copy the full text and paste it here.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member MrLowBot's Avatar
    Join Date
    Nov 2018
    Location
    Sweden
    Posts
    130
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: From txt file to array.

    Quote Originally Posted by Norm View Post
    Please explain what happens when you compile and execute the program.
    If there are error messages, copy the full text and paste it here.
    Well that's part of the problem because i don't even get any error messages.
    So i get nothing from the console.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: From txt file to array.

    i get nothing from the console.
    Add some print statements in all of the methods and loops to show where the program is executing.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member MrLowBot's Avatar
    Join Date
    Nov 2018
    Location
    Sweden
    Posts
    130
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: From txt file to array.

    Hey again.

    Been sick and been with family so have not been able to write any code.
    But i now i have found the problem.

    	static void read() throws Exception {
     
    		ArrayList<Integer> list = new ArrayList<Integer>();
    		// This method just reads a file.
     
    		// The path that we want to read from.
    		FileReader file = new FileReader("C:\\Users\\karwa\\Desktop\\heltal.txt");
    		// Reading it by BufferedReader.
    		BufferedReader read = new BufferedReader(file);
    		int[] data = new int[100];
    		String text = "";
    		int data1 = 0;
    		int i = 0;
    		// Actually reading it here.
    		String line = read.readLine();
    		while (line != null) {
    			text += line;
    			// line = read.readLine();
    			data1 = Integer.valueOf(read.readLine());
    			list.add(data1);
     
    		}
    		// System.out.println(text);
    		for (int k : list) {
    			System.out.println(k);
    		}
    	}

    This is the problem and i get an error from it
    data1 = Integer.valueOf(read.readLine());

    Any ideas? Thanks.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: From txt file to array.

    i get an error from it
    Please copy the full text of the error message and paste it here. It has important info about the error.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member MrLowBot's Avatar
    Join Date
    Nov 2018
    Location
    Sweden
    Posts
    130
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: From txt file to array.

    Quote Originally Posted by Norm View Post
    Please copy the full text of the error message and paste it here. It has important info about the error.
    Exception in thread "main" java.lang.NumberFormatException: For input string: " 55"
    	at java.lang.NumberFormatException.forInputString(Unknown Source)
    	at java.lang.Integer.parseInt(Unknown Source)
    	at java.lang.Integer.valueOf(Unknown Source)
    	at km222nb_lab4.Histrogram.read(Histrogram.java:36)
    	at km222nb_lab4.Histrogram.main(Histrogram.java:14)

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: From txt file to array.

    java.lang.NumberFormatException: For input string: " 55"
    That String has a leading space which is not a valid numeric digit. Use the String class's trim method to strip off leading/trailing spaces.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Member MrLowBot's Avatar
    Join Date
    Nov 2018
    Location
    Sweden
    Posts
    130
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: From txt file to array.

    Quote Originally Posted by Norm View Post
    That String has a leading space which is not a valid numeric digit. Use the String class's trim method to strip off leading/trailing spaces.
    I see.
    But, as usual, i get an error...
    	                 String x = read.readLine();
    			 data1 = Integer.valueOf(x.trim()); 
    			 list.add(data1);

    Error :
    Exception in thread "main" java.lang.NullPointerException
    	at km222nb_lab4.Histrogram.read(Histrogram.java:35)
    	at km222nb_lab4.Histrogram.main(Histrogram.java:14)

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: From txt file to array.

    What variable has the null value? How did the variable get the null value?
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Member MrLowBot's Avatar
    Join Date
    Nov 2018
    Location
    Sweden
    Posts
    130
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: From txt file to array.

    Quote Originally Posted by Norm View Post
    What variable has the null value? How did the variable get the null value?
    public interface Histrogram {
    	// throws Exception is there to check if the file actually exists and if it
    	// doesn't then it will tell us that by an error.
    	public static void main(String[] args) throws Exception {
     
    		read();
     
    	}
     
    	static void read() throws Exception {
     
    		ArrayList<Integer> list = new ArrayList<Integer>();
    		// This method just reads a file.
     
    		// The path that we want to read from.
    		FileReader file = new FileReader("C:\\Users\\karwa\\Desktop\\heltal.txt");
    		// Reading it by BufferedReader.
    		BufferedReader read = new BufferedReader(file);
    		String text = "";
    		int data1 = 0;
    		// Actually reading it here.
    		String line = read.readLine();
    		while (line != null) {
    			text += line;
    			// line = read.readLine();
    			String x = read.readLine();
    			data1 = Integer.valueOf(x.trim()); 
    			list.add(data1);
     
    		}
    		// System.out.println(text);
    		for (int k : list) {
    			System.out.println(k);
    		}
    	}
     
    }

    Data1 has 0 but everything else does't have a value that is 0.

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: From txt file to array.

    Did you find the variable with the null value?
    To find it, Add some print statements to print out the value of all the variables used on line 35 where the NPE happened.


    data1 has 0
    A value of 0 is not a null. 0 is a valid value for an int variable.
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: From txt file to array.

    When you get an error like the following:

    Exception in thread "main" java.lang.NullPointerException
    at km222nb_lab4.Histrogram.read(Histrogram.java:35)
    at km222nb_lab4.Histrogram.main(Histrogram.java:14)

    It is helpful to include the code which contained the first line mentioned (line 35 in this case). If you didn't know, the lines are the backtrace of where the error occurred. So line 14 made a call to something that referenced line 35. If you print out some of the fields used in line 35, you may find one of them is null.

    Regards,
    Jim

  16. #16
    Member MrLowBot's Avatar
    Join Date
    Nov 2018
    Location
    Sweden
    Posts
    130
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: From txt file to array.

    I see what you guys mean but i don't get any values back at all.
    Rather stuck atm.

  17. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: From txt file to array.

    i don't get any values back at all.
    What does that mean?
    What is printed when you print the values of all the variables used on the line where the NPE happens?
    Something must print if the statement is executed. Be sure to put the print statement before the statement where the NPE happens or it will not be executed.
    If you don't understand my answer, don't ignore it, ask a question.

  18. #18
    Member
    Join Date
    Sep 2018
    Location
    Virginia
    Posts
    284
    My Mood
    Cool
    Thanks
    0
    Thanked 38 Times in 36 Posts

    Default Re: From txt file to array.

    Here is a contrived example that throws an npe. If you paste the code exactly as it exists here and then compile and run it, the stack trace of the npe should match the lines commented in the code.

    import java.util.List;
     
    public class TraceBackExample {
     
       List<Integer> list;
     
       public static void main(String[] args) {
    	  new TraceBackExample().start(); // line 8
       }
     
       public void start() {
    	  add(20, 30); // line 12
       }
     
       public void add(int a, int b) {
    	  addSumToList(a + b); // line 16
       }
     
       public void addSumToList(int a) {
    	  System.out.println(list + "  " + a);
    	  list.add(a); // line 21
       }
     
    }

    Regards,
    Jim
    Last edited by jim829; December 30th, 2018 at 06:21 PM.

  19. #19
    Member MrLowBot's Avatar
    Join Date
    Nov 2018
    Location
    Sweden
    Posts
    130
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: From txt file to array.

    Quote Originally Posted by jim829 View Post
    Here is a contrived example that throws an npe. If you paste the code exactly as it exists here and then compile and run it, the stack trace of the npe should match the lines commented in the code.

    import java.util.List;
     
    public class TraceBackExample {
     
       List<Integer> list;
     
       public static void main(String[] args) {
    	  new TraceBackExample().start(); // line 8
       }
     
       public void start() {
    	  add(20, 30); // line 12
       }
     
       public void add(int a, int b) {
    	  addSumToList(a + b); // line 16
       }
     
       public void addSumToList(int a) {
    	  System.out.println(list + "  " + a);
    	  list.add(a); // line 21
       }
     
    }

    Regards,
    Jim
    Quote Originally Posted by Norm View Post
    What does that mean?
    What is printed when you print the values of all the variables used on the line where the NPE happens?
    Something must print if the statement is executed. Be sure to put the print statement before the statement where the NPE happens or it will not be executed.
    Thanks guys, i think that i got it!

    	static void read() throws Exception {
     
    		ArrayList<Integer> list = new ArrayList<Integer>();
    		// This method just reads a file.
     
    		// The path that we want to read from.
    		FileReader file = new FileReader("C:\\Users\\karwa\\Desktop\\heltal.txt");
    		// Reading it by BufferedReader.
    		BufferedReader read = new BufferedReader(file);
     
    		String text = "";
    		int data1 = 0;
     
    		// Actually reading it here.
     
    		String line;
    		while ((line = read.readLine()) != null) {
    			text += line;
    			String g = read.readLine();
    			data1 = Integer.valueOf(g.trim());
    			list.add(data1);
     
    		}
    		System.out.println();
    		for (int k : list) {
    			System.out.println(k);
    		}
    		read.close();
    	}

    I believe that the problem was that "line" read null instantly and then nothing worked.
    But by telling it to read from the while loop instead of outside of the while loop it got working.

    I just dropped "line = read.readLine()" down one step into the while.
    String line;
    		while ((line = read.readLine()) != null)


    --- Update ---

    Yeah no nvm it is not working as it should be...

    I still get null pointer exception when i try to print negative numbers or when there are more than one whole number at one
    line such as "18 43" etc.
    And i cannot print the first numbers from the first line.. they just show up a a blank space and then print the rest of the one after another.
    Well we are only reading for each line so that should be ok. But negative numbers is something that i would like to have in the program but
    without making it show an error.

  20. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: From txt file to array.

    Work on one problem at a time
    I still get null pointer exception
    Please copy the full text of the error message and paste it here. It has important info about the error.
    If you don't understand my answer, don't ignore it, ask a question.

  21. #21
    Junior Member tonya's Avatar
    Join Date
    Feb 2018
    Posts
    16
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: From txt file to array.

    Did you mean to read two lines of your text file per while loop? you do a read.readLine() twice for each loop

    while ((line = read.readLine()) != null) {
    	text += line;
    	String g = read.readLine[();
    	data1 = Integer.valueOf(g.trim());
    	list.add(data1);
    }
    ]

  22. #22
    Member MrLowBot's Avatar
    Join Date
    Nov 2018
    Location
    Sweden
    Posts
    130
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: From txt file to array.

    Quote Originally Posted by tonya View Post
    Did you mean to read two lines of your text file per while loop? you do a read.readLine() twice for each loop

    while ((line = read.readLine()) != null) {
    	text += line;
    	String g = read.readLine[();
    	data1 = Integer.valueOf(g.trim());
    	list.add(data1);
    }
    ]
    Yeah, i switched it to only use the trim to "line.trim()" and now i got all the numbers within my array.

    Quote Originally Posted by Norm View Post
    Work on one problem at a time

    Please copy the full text of the error message and paste it here. It has important info about the error.
    No need my friend. Works fine now. Seeing as how i am getting the numbers line per line and the fact is that
    it doesn't include negative values but it won't matter in this case.

    The next step is to somehow make a histogram out of the numbers within the array.

    --- Update ---

    So here i have a method that should be able to count numbers between 0 and 100 within the array that has 100 numbers from -50 to around 150.

    This method does nothing other than to count to 100 and print the total. Which is 100..
    But i want to see how many values there are from 0 to 100 within my array.

    Any ideas? Tried to use the list.size() but that really doesn't give me what i want.
    static void count() {
    		int c = 0;
    		for (int i = 1; i <= 100; i++) {
    			c++;
    		}
    		System.out.println("Between the interval of [0, 100] there are : " + c);
                    System.out.println("And the rest are : " +  150 - c); 
     
    	}

  23. #23
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: From txt file to array.

    i want to see how many values there are from 0 to 100 within my array.
    Use an if statement to detect the desired values
    and a counter to count them.
    If you don't understand my answer, don't ignore it, ask a question.

  24. #24
    Member MrLowBot's Avatar
    Join Date
    Nov 2018
    Location
    Sweden
    Posts
    130
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Default Re: From txt file to array.

    Quote Originally Posted by Norm View Post
    Use an if statement to detect the desired values
    and a counter to count them.
    Got it. Thanks

Similar Threads

  1. [SOLVED] Loading an array from a .txt file
    By NEPALII in forum Java Theory & Questions
    Replies: 10
    Last Post: December 10th, 2013, 06:14 PM
  2. Txt file to array
    By cywfong in forum What's Wrong With My Code?
    Replies: 2
    Last Post: September 25th, 2013, 02:46 PM
  3. [SOLVED] Array Population via .txt File
    By Blackrabbitjack in forum What's Wrong With My Code?
    Replies: 18
    Last Post: March 19th, 2012, 10:07 AM
  4. Building an array from .txt file.
    By brudley5 in forum Object Oriented Programming
    Replies: 3
    Last Post: December 4th, 2011, 09:14 PM
  5. Replies: 2
    Last Post: May 13th, 2011, 03:08 AM