Problem readingText File

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

33 Replies - 1461 Views - Last Post: 09 March 2014 - 02:11 PM Rate Topic: ***-- 2 Votes

#1 incredibleX   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 212
  • Joined: 01-November 13

Problem readingText File

Posted 09 March 2014 - 11:14 AM

I'm trying to Read File but ending up with some garbage.
Here is my code
import java.io.*;
import java.util.*;

public class billions {
	
	private int[] array =new int[32763];
	private Scanner sc;
	private int counter=0;
	
	public void openFile(){
		try{
		sc = new Scanner(new File("Records.txt"));
		}catch(Exception e){
			System.out.println("Could not found File");
		}
	}
	public int[] readRecords(){
		while(sc.hasNextInt()){
			array[counter++] = sc.nextInt();
		}		
	return array;
}
	public void printArray(){
             for(int i=0;i<array.length;i++)
		System.out.println(array[i]);
	}
	public void closeFile(){
		sc.close();
	}
	public static void main(String[] args){
		billions obj=new billions();
		obj.openFile();
		obj.readRecords();
		obj.printArray();
		obj.closeFile();
	}

}



Whats the matter i'm stuck.
Array size is the range of integers.

This post has been edited by incredibleX: 09 March 2014 - 11:42 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Problem readingText File

#2 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12556
  • View blog
  • Posts: 45,682
  • Joined: 27-December 08

Re: Problem readingText File

Posted 09 March 2014 - 11:21 AM

Quote

I'm trying to Read File but ending up with some garbage.

What does this mean? Please explain.
Was This Post Helpful? 0
  • +
  • -

#3 incredibleX   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 212
  • Joined: 01-November 13

Re: Problem readingText File

Posted 09 March 2014 - 11:43 AM

View Postmacosxnerd101, on 09 March 2014 - 06:21 PM, said:

Quote

I'm trying to Read File but ending up with some garbage.

What does this mean? Please explain.


Print Array method is returning zero's .
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12556
  • View blog
  • Posts: 45,682
  • Joined: 27-December 08

Re: Problem readingText File

Posted 09 March 2014 - 11:48 AM

I ran your code, replacing the File with System.in. I entered the input: 1 2 3 4 5 6 7 8 9 10 and got the output:

Quote

1
2
3
4
5
6
7
8
9
10
0
0
0
...


Quote

Print Array method is returning zero's .

It's not returning anything. It's printing to the console. Are you sure your File is being read in correctly? Are you sure it has valid ints in it?
Was This Post Helpful? 0
  • +
  • -

#5 CasiOo   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1577
  • View blog
  • Posts: 3,551
  • Joined: 05-April 11

Re: Problem readingText File

Posted 09 March 2014 - 11:48 AM

How many int's are in the text file?
Remember that your array is of size 32763, which means you will be printing 32763 lines
The array is filled with 0's by default
Was This Post Helpful? 0
  • +
  • -

#6 incredibleX   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 212
  • Joined: 01-November 13

Re: Problem readingText File

Posted 09 March 2014 - 12:08 PM

View PostCasiOo, on 09 March 2014 - 06:48 PM, said:

How many int's are in the text file?
Remember that your array is of size 32763, which means you will be printing 32763 lines
The array is filled with 0's by default


My Text File contains 30 integers [1-30]
yes it is reading it as integers, but printArray is returning only zero's
if it is the matter of array size it should display those 30 values first and then those zero's.
Was This Post Helpful? 0
  • +
  • -

#7 CasiOo   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1577
  • View blog
  • Posts: 3,551
  • Joined: 05-April 11

Re: Problem readingText File

Posted 09 March 2014 - 12:11 PM

Well the program works as expected on my end

Maybe your ide removes some of the first lines printed to the console when you are printing thousands

This post has been edited by CasiOo: 09 March 2014 - 12:13 PM

Was This Post Helpful? 0
  • +
  • -

#8 incredibleX   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 212
  • Joined: 01-November 13

Re: Problem readingText File

Posted 09 March 2014 - 12:11 PM

i'm trying to count total even numbers in the file.
But its returning garbage.
import java.io.*;
import java.util.*;

public class billions {
	
	private int[] array =new int[32763];
	private Scanner sc;
	private int counter=0,even;
	
	public void openFile(){
		try{
		sc = new Scanner(new File("Records.txt"));
		}catch(Exception e){
			System.out.println("Could not found File");
		}
	}
	public int[] readRecords(){ 
		while(sc.hasNextInt()){
			array[counter++] = sc.nextInt(); 
		} 
		return array;
	}
	public void performOperations(){
		evenCount();
	}
	public void evenCount(){
		for(int i=0;i<array.length;i++)
			if(array[i]%2==0){
				even++;
			}
	}
	public void printEven(){
		System.out.println(even);
	}
	public void printArray(int Array[]){
		for(int i=0;i<Array.length;i++)
		System.out.println(Array[i]);
	}
	public void closeFile(){
		sc.close();
	}
	public static void main(String[] args){
		billions obj=new billions();
		obj.openFile();
		obj.readRecords();
		obj.performOperations();
		obj.printEven();
		obj.closeFile();
	}

}



even if i remove everything from text file it still shows a garbage value as number of integers.
Was This Post Helpful? 0
  • +
  • -

#9 CasiOo   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1577
  • View blog
  • Posts: 3,551
  • Joined: 05-April 11

Re: Problem readingText File

Posted 09 March 2014 - 12:12 PM

<Removed, was for earlier post>

This post has been edited by CasiOo: 09 March 2014 - 12:13 PM

Was This Post Helpful? 0
  • +
  • -

#10 incredibleX   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 212
  • Joined: 01-November 13

Re: Problem readingText File

Posted 09 March 2014 - 12:15 PM

This here code works fine :
public int[] readRecords(){ 
		while(sc.hasNextInt()){
			array[counter++] = sc.nextInt(); 
			System.out.println(array[counter-1]);
		} 
		return array;
	}


Was This Post Helpful? 0
  • +
  • -

#11 CasiOo   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1577
  • View blog
  • Posts: 3,551
  • Joined: 05-April 11

Re: Problem readingText File

Posted 09 March 2014 - 12:17 PM

Well your text file only contains 30 integers, the rest of the indexes in the array will have the value 0
0 mod 2 equals 0, so it will increment your even counter
Was This Post Helpful? 1
  • +
  • -

#12 incredibleX   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 212
  • Joined: 01-November 13

Re: Problem readingText File

Posted 09 March 2014 - 12:23 PM

View PostCasiOo, on 09 March 2014 - 07:17 PM, said:

Well your text file only contains 30 integers, the rest of the indexes in the array will have the value 0
0 mod 2 equals 0, so it will increment your even counter


OOhhh yes you are right, that was the issue actually.
Actually i took array size equal to the size of integer because thats what i'm doing:

"Suppose a text file with 10 billion records Integer Numbers. There Range of integer is 0 to 32763.
Write an elegant java program that calculate
* Total number of even Numbers in the file
..... etc "
Is it right to take the size equal to the size of integer, my records are 10 billion?
Was This Post Helpful? 0
  • +
  • -

#13 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12556
  • View blog
  • Posts: 45,682
  • Joined: 27-December 08

Re: Problem readingText File

Posted 09 March 2014 - 12:28 PM

Quote

"Suppose a text file with 10 billion records Integer Numbers. There Range of integer is 0 to 32763.
Write an elegant java program that calculate
* Total number of even Numbers in the file
..... etc "
Is it right to take the size equal to the size of integer, my records are 10 billion?

If the array stores 32763 elements, it cannot store 10 billion integers. Also, an int can store values much larger than 32763.

If you need to read up to 10 billion records, storing them all is probably a bad way to go about it. It's easy enough to calculate the number of even integers in the file without storing them in an array:
while(scan.hasNextInt()){
   int temp = scan.nextInt();
   if(temp % 2 == 0){
      evenCount++;
   }
}


Was This Post Helpful? 1
  • +
  • -

#14 CasiOo   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1577
  • View blog
  • Posts: 3,551
  • Joined: 05-April 11

Re: Problem readingText File

Posted 09 March 2014 - 12:34 PM

I think he wrote "10 billion records" because he doesn't want you to declare a big integer array ;)
So try and come up with a solution where you don't save every integer in the file in an array
Try iterating the integers one at a time from the file, and do statistics before reading in the next
Was This Post Helpful? 1
  • +
  • -

#15 incredibleX   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 212
  • Joined: 01-November 13

Re: Problem readingText File

Posted 09 March 2014 - 12:39 PM

with respect to what i have told you so far you're 1000% right.
But things are not that simple here.
I also have to calculate:
* The frequency of each number in the file.
* The Numbers which are not present within Text File.
* Maximum Number in the File.
* Minimum Number in the File.

is still array not important?
What other data structure would be more efficient?

View PostCasiOo, on 09 March 2014 - 07:34 PM, said:

I think he wrote "10 billion records" because he doesn't want you to declare a big integer array ;)/>
So try and come up with a solution where you don't save every integer in the file in an array
Try iterating the integers one at a time from the file, and do statistics before reading in the next

hehehe But he also pointed to use a data structure :) ;)

This post has been edited by incredibleX: 09 March 2014 - 12:38 PM

Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3