Results 1 to 19 of 19
Like Tree1Likes
  • 1 Post By jim829

Thread: I am recieving the wrong min value and my code is not reading my file properly.

  1. #1
    sithclone3 is offline Member
    Join Date
    Jun 2014
    Posts
    89
    Rep Power
    0

    Default I am recieving the wrong min value and my code is not reading my file properly.

    My program is reading my file, but its printing out weird cryptic numbers instead of the 1-10 that is in the file. But it uses the numbers in the file to get the Max Value and Total. Also I am receiving 0 for my Minimum Value but, 0 isn't even in the file, my lowest value is 1.





    Java Code:
    /*
     * Student Name: Irrelavent 
    
     * 
     * Description: This program will read from a text file and display its contents in an array format as well as the average of the numbers
     * 					and the minimum and maximum value within the array and it will display the total of all the numbers added together.
     * 
     * Input Variables:
     * 
     * Desired Output:
     * 	Total: 55
    	Average: 5.5
    	Minimum Value: 1.0
    	Maximum Value: 10.0
    	The data being used is: [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0]
     */
    
    import java.util.*;
    import java.io.*;
    
    public class CH8_11 {
    		
    		
    	//Constants Declaration
    	//*********************
    	
    	
    	
    	
    	public static void main(String[] args) throws FileNotFoundException 
    	{
    		// TODO Auto-generated method stub
    		
    		//Variable Declaration Section
    		//****************************
    		int[] numbers;
    		final int SIZE;
    		int i;
    		int max;
    		int min;
    		int total;
    		double average;
    		List<int[]> numbersList;
    		
    		
    		
    		
    		// Variables Initialization Section
    	    //*********************************
    		File file = new File("C:\\testing1.txt");
    		Scanner scanner = new Scanner(file);
    		SIZE = 100;
    		numbers = new int[SIZE];
    		numbersList = Arrays.asList(numbers);
    		max = numbers[0];
    		min = numbers[0];
    		i = 0;
    		total = 0;
    		average = 0.0;
    		
    		
    		
    		//Keyboard
    		//********
    		
    
    	    
    	    // Code Section
    	    //*************
    		
    		while(scanner.hasNext())
    		{
    			numbers[i] = scanner.nextInt();
    			i++;
    		}
    		
    		for(int j = 1; j < numbers.length; j++)
    		{
    			if(numbers[j] > max)
    				max = numbers[j];
    		}
    		
    		for(int j = 1; j < numbers.length; j++)
    		{
    			if(numbers[j] < min)
    				min = numbers[j];
    		}
    		
    		for(int j = 0; j < numbers.length; j++)
    		{
    			total += numbers[j];
    			average = total / numbers.length;
    		}
    		
    		
    		
    		//Output Section
    		//**************
    		System.out.print("\nNumbers being used: " + numbersList);
    		System.out.print("\n---------------");
    		System.out.print("\nMax Value: " + max);
    		System.out.print("\n---------------");
    		System.out.print("\nMinimum Value: " + min);
    		System.out.print("\n---------------");
    		System.out.print("\nAverage: " + average);
    		System.out.print("\n---------------");
    		System.out.print("\nTotal: " + total);
    		
    		
    		
    		
    		//Resource Cleaning
    		//*****************
    		scanner.close();
    		
    
    		
    	}
    
    }

    Output:
    Numbers being used: [[I@55f96302]
    ---------------
    Max Value: 10
    ---------------
    Minimum Value: 0
    ---------------
    Average: 0.0
    ---------------
    Total: 55

    I want 1 for my Minimum and I want 5.5 for my average, when I make 'average = total / numbers.length;' ---> 'average = total / (double)numbers.length;' I get 0.55. Also, as its obvious, my 'Numbers being used' section is messed up and I don't know whats causing this.
    Last edited by sithclone3; 02-11-2015 at 10:22 AM.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: I am recieving the wrong min value and my code is not reading my file properly.

    You have created an array with 100 elements (all initialized to zero), but you're reading less than 100 numbers and the rest if your code assumes all 100 numbers were set ...

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  3. #3
    sithclone3 is offline Member
    Join Date
    Jun 2014
    Posts
    89
    Rep Power
    0

    Default Re: I am recieving the wrong min value and my code is not reading my file properly.

    Quote Originally Posted by JosAH View Post
    You have created an array with 100 elements (all initialized to zero), but you're reading less than 100 numbers and the rest if your code assumes all 100 numbers were set ...

    kind regards,

    Jos
    I had to create an array that adjust to the size of the file size without using arrayList. This is difficult for me since I'm used to arrayList. Any tips? I thought making a big array would suffice >.<

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: I am recieving the wrong min value and my code is not reading my file properly.

    Here is a hint: you can't create an array that adjusts its size. You can only create a new array that is larger when the current one is full.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: I am recieving the wrong min value and my code is not reading my file properly.

    One solution is to keep track of the number of values in the array and use that to control the for loop instead of the size of the array.
    Numbers being used: [[I@55f96302]
    [I@55f96302 is the String returned by the int array's toString() method.
    If you want to print the contents of an array, use the Arrays class's toString() method:
    System.out.println("an ID "+ java.util.Arrays.toString(theArrayName));
    The code will have to access the contents of the list to get a reference to the array.
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: I am recieving the wrong min value and my code is not reading my file properly.

    Did you realize you are using nextInt() to read in a series of floating point numbers. Perhaps you should use nextDouble() and change your variables accordingly. Or change your list to integers.

    Regards,
    Jim
    Norm likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: I am recieving the wrong min value and my code is not reading my file properly.

    Does the code read any of the data? What is the value of i on line 76 after the for loop?
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    sithclone3 is offline Member
    Join Date
    Jun 2014
    Posts
    89
    Rep Power
    0

    Default Re: I am recieving the wrong min value and my code is not reading my file properly.

    Quote Originally Posted by Norm View Post
    Does the code read any of the data? What is the value of i on line 76 after the for loop?
    Uhm. When I use 'java.util.Arrays.toString(theArrayName)' I recieve the data inside my file, and the extra ninety zeros.....I believe its doing what I want, except for the fact that i need to move the array into another array so it cant change size. I dislike not using arraylist. >:(

  9. #9
    sithclone3 is offline Member
    Join Date
    Jun 2014
    Posts
    89
    Rep Power
    0

    Default Re: I am recieving the wrong min value and my code is not reading my file properly.

    Here is a hint: you can't create an array that adjusts its size. You can only create a new array that is larger when the current one is full.
    (gimbal2)

    This is what I'm attempting to do...guess i did it wrong, huh?

  10. #10
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: I am recieving the wrong min value and my code is not reading my file properly.

    Quote Originally Posted by sithclone3 View Post
    Uhm. When I use 'java.util.Arrays.toString(theArrayName)' I recieve the data inside my file, and the extra ninety zeros.....I believe its doing what I want, except for the fact that i need to move the array into another array so it cant change size. I dislike not using arraylist. >:(
    Well, you could write some helper routines to transparently add elements to an array and grow it as necessary. You could keep track of the number elements verses its size. You could even have a routine the reduces the array to be sized to the current number of active elements (these are some of the things ArrayList does).

    Or use what you presently have but maintain a count of active elements. Then use that count instead of the array size.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  11. #11
    sithclone3 is offline Member
    Join Date
    Jun 2014
    Posts
    89
    Rep Power
    0

    Default Re: I am recieving the wrong min value and my code is not reading my file properly.

    Quote Originally Posted by jim829 View Post
    Well, you could write some helper routines to transparently add elements to an array and grow it as necessary. You could keep track of the number elements verses its size. You could even have a routine the reduces the array to be sized to the current number of active elements (these are some of the things ArrayList does).

    Or use what you presently have but maintain a count of active elements. Then use that count instead of the array size.

    Regards,
    Jim
    I'll try that, I have the scanner class in use, but I have to have a fixed array size to use the array. Is there a way of not using a fixed array size and using the scanner class to read me the length of the file and store that length into an array?

    Also, I'm still recieving zero for my minimum value. the counter is counting 0-10 when the file has 1-10.

  12. #12
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: I am recieving the wrong min value and my code is not reading my file properly.

    Quote Originally Posted by sithclone3 View Post
    I'll try that, I have the scanner class in use, but I have to have a fixed array size to use the array. Is there a way of not using a fixed array size and using the scanner class to read me the length of the file and store that length into an array?

    Also, I'm still recieving zero for my minimum value. the counter is counting 0-10 when the file has 1-10.
    You can certainly have the first argument of a text file be the number of following lines if you choose. Just read in the value and allocate an array. Then continue reading the file. Get that working first and it may eliminate your other problem.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  13. #13
    sithclone3 is offline Member
    Join Date
    Jun 2014
    Posts
    89
    Rep Power
    0

    Default Re: I am recieving the wrong min value and my code is not reading my file properly.

    Is there any other way to fix this issue? I'm really just lost on how my 'min' variable is getting 0 when there isn't even a 0 in my file and how the total and average and max still get the correct numbers.

  14. #14
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: I am recieving the wrong min value and my code is not reading my file properly.

    Quote Originally Posted by sithclone3 View Post
    Is there any other way to fix this issue? I'm really just lost on how my 'min' variable is getting 0 when there isn't even a 0 in my file
    (sorry, I was in meetings all day so couldn't respond earlier).

    So add System.out.println() statements and print out the value of 'min' throughout your application. See what happens to it. Solve one thing at a time.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  15. #15
    sithclone3 is offline Member
    Join Date
    Jun 2014
    Posts
    89
    Rep Power
    0

    Default Re: I am recieving the wrong min value and my code is not reading my file properly.

    Quote Originally Posted by gimbal2 View Post
    (sorry, I was in meetings all day so couldn't respond earlier).

    So add System.out.println() statements and print out the value of 'min' throughout your application. See what happens to it. Solve one thing at a time.
    No need to apologize, we all have lives outside of this forum, haha! I have actually fixed my issue. I even made it so the array's size is based off the file's size (number of integers in the file) but now I am recieving 0's for all my outputs, since I created a new array, the array is replacing everything in file with 0's :(

    Java Code:
    /*
     * Student Name: Irrelavent 
    
     * 
     * Description: This program will read from a text file and display its contents in an array format as well as the average of the numbers
     * 					and the minimum and maximum value within the array and it will display the total of all the numbers added together.
     * 
     * Input Variables:
     * 
     * Desired Output:
     * 	Total: 10
    	Average: 5.5
    	Minimum Value: 1.0
    	Maximum Value: 10.0
    	The data being used is: [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0]
     */
    
    import java.util.*;
    import java.io.*;
    
    public class CH8_11 {
    		
    		
    	//Constants Declaration
    	//*********************
    	
    	
    	
    	
    	public static void main(String[] args) throws FileNotFoundException 
    	{
    		// TODO Auto-generated method stub
    		
    		//Variable Declaration Section
    		//****************************
    		int[] numbers;
    		int SIZE;
    		int i;
    		int max;
    		int min;
    		int total;
    		double average;
    		
    		
    		
    		
    		
    		// Variables Initialization Section
    	    //*********************************
    		File file = new File("C:\\testing1.txt");
    		Scanner scanner = new Scanner(file);
    		SIZE = 0;
    		max = 0;
    		min = Integer.MAX_VALUE;
    		i = 0;
    		total = 0;
    		average = 0.0;
    		
    		
    		
    		//Keyboard
    		//********
    		
    
    	    
    	    // Code Section
    	    //*************
    		
    		while(scanner.hasNext())
    		{
    			SIZE++;
    			scanner.next();
    		}
    		
    		numbers = new int[SIZE];
    	
    		while(scanner.hasNext() && i < numbers.length)
    		{
    			numbers[i] = scanner.nextInt();
    			i++;
    		}
    		
    		for(int j = 1; j < numbers.length; j++)
    		{
    			if(numbers[j] > max)
    				max = numbers[j];
    		}
    		
    		
    		for(int j = 0; j < numbers.length; j++)
    		{
    			if(numbers[j] < min)
    				min = numbers[j];
    		}
    		
    	
    		for(int j = 0; j < numbers.length; j++)
    		{
    			total += numbers[j];
    			average = total / (double)numbers.length;
    		}
    		
    		
    		
    		
    		
    		//Output Section
    		//**************
    		System.out.println("Numbers being used: "+ java.util.Arrays.toString(numbers));
    		System.out.print("---------------------------------------------------");
    		System.out.print("\nMaximum Value: " + max);
    		System.out.print("\n-----------------");
    		System.out.print("\nMinimum Value: " + min);
    		System.out.print("\n-----------------");
    		System.out.print("\nAverage: " + average);
    		System.out.print("\n-----------------");
    		System.out.print("\nTotal: " + total);
    		
    		
    		
    		
    		//Resource Cleaning
    		//*****************
    		scanner.close();
    		
    
    		
    	
    	}
    
    }

  16. #16
    sithclone3 is offline Member
    Join Date
    Jun 2014
    Posts
    89
    Rep Power
    0

    Default Re: I am recieving the wrong min value and my code is not reading my file properly.

    UPDATE:MY PROBLEM IS SOLVED. I have fixed my issue, I am greatful for all the help you guys gave me. Thank you.

    Java Code:
    /*
     * Student Name: Sean Pratt 
    
     * 
     * Description: This program will read from a text file and display its contents in an array format as well as the average of the numbers
     * 					and the minimum and maximum value within the array and it will display the total of all the numbers added together.
     * 
     * Input Variables:
     * 
     * Desired Output:
     * 	Total: 10
    	Average: 5.5
    	Minimum Value: 1.0
    	Maximum Value: 10.0
    	The data being used is: [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0]
     */
    
    import java.util.*;
    import java.io.*;
    
    public class CH8_11 {
    		
    		
    	//Constants Declaration
    	//*********************
    	
    	
    	
    	
    	public static void main(String[] args) throws FileNotFoundException 
    	{
    		// TODO Auto-generated method stub
    		
    		//Variable Declaration Section
    		//****************************
    		int[] numbers;
    		int SIZE;
    		int i;
    		int max;
    		int min;
    		int total;
    		double average;
    		
    		
    		
    		
    		
    		// Variables Initialization Section
    	    //*********************************
    		File file = new File("C:\\testing1.txt");
    		Scanner scanner = new Scanner(file);
    		SIZE = 0;
    		max = 0;
    		min = Integer.MAX_VALUE;
    		i = 0;
    		total = 0;
    		average = 0.0;
    		
    		
    		
    		//Keyboard
    		//********
    		
    
    	    
    	    // Code Section
    	    //*************
    		
    		while(scanner.hasNext())
    		{
    			SIZE++;
    			scanner.next();
    		}
    		
    		numbers = new int[SIZE];
    		
    		scanner.close();
    		scanner = new Scanner(file);
    	
    		while(scanner.hasNext() && i < numbers.length)
    		{
    			numbers[i] = scanner.nextInt();
    			i++;
    		}
    		
    		for(int j = 1; j < numbers.length; j++)
    		{
    			if(numbers[j] > max)
    				max = numbers[j];
    		}
    		
    		
    		for(int j = 0; j < numbers.length; j++)
    		{
    			if(numbers[j] < min)
    				min = numbers[j];
    		}
    		
    	
    		for(int j = 0; j < numbers.length; j++)
    		{
    			total += numbers[j];
    			average = total / (double)numbers.length;
    		}
    		
    		
    		
    		
    		
    		//Output Section
    		//**************
    		System.out.println("Numbers being used: "+ java.util.Arrays.toString(numbers));
    		System.out.print("---------------------------------------------------");
    		System.out.print("\nMaximum Value: " + max);
    		System.out.print("\n-----------------");
    		System.out.print("\nMinimum Value: " + min);
    		System.out.print("\n-----------------");
    		System.out.print("\nAverage: " + average);
    		System.out.print("\n-----------------");
    		System.out.print("\nTotal: " + total);
    		
    		
    		
    		
    		//Resource Cleaning
    		//*****************
    		scanner.close();
    		
    
    		
    	
    	}
    
    }
    This is my finished product :D

  17. #17
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: I am recieving the wrong min value and my code is not reading my file properly.

    You know, right now your comments are really pointless if you excuse the directness. You comment things which are basically captain obvious, like "output section". That need not be explained as the following code does all outputting of data. Its self-documenting, it need not be explained.

    Yet code which actually does something is completely lacking any kind of comments. I wonder why that is?

    Java Code:
    while(scanner.hasNext())
            {
                SIZE++;
                scanner.next();
            }
    What does that bit of code do? Can you explain that in plain English in a comment?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  18. #18
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: I am recieving the wrong min value and my code is not reading my file properly.

    Quote Originally Posted by gimbal2 View Post
    You know, right now your comments are really pointless if you excuse the directness. You comment things which are basically captain obvious, like "output section". That need not be explained as the following code does all outputting of data. Its self-documenting, it need not be explained.

    Yet code which actually does something is completely lacking any kind of comments. I wonder why that is?
    To me, the existence of that comment says "this should be a method".
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  19. #19
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: I am recieving the wrong min value and my code is not reading my file properly.

    Now that you've got your code working, I would like to make a few observations. Note: your program works so I am not advocating changing anything. But this could be a learning moment.

    First, look at your loops. Do you really need three?
    Second, in your last loop where you compute average, you keep doing over and over again. Why not wait until total is calculated.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Replies: 7
    Last Post: 04-30-2014, 12:43 PM
  2. Replies: 10
    Last Post: 07-10-2013, 02:36 PM
  3. Replies: 2
    Last Post: 03-24-2012, 08:47 PM
  4. Not reading array properly and other things.
    By rokit boy in forum New To Java
    Replies: 3
    Last Post: 03-18-2012, 03:56 PM
  5. Replies: 3
    Last Post: 08-10-2011, 09:17 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •