aspose file tools*
The moose likes Java in General and the fly likes Files from the Command line, converting to ints problems. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Files from the Command line, converting to ints problems. " Watch "Files from the Command line, converting to ints problems. " New topic
Author

Files from the Command line, converting to ints problems.

Vincent Mule
Greenhorn

Joined: Oct 04, 2012
Posts: 14
Hello.
For my Java class we have to take a file from the command line (I.E. \java Program input.txt 30) and an int then print the numbers back out, find the average, smallest value, biggest value, and find all the numbers that go above the average and below the average.
(I.e. output:
68 32 54 12 37 29 50
min: 12
max: 68
ave: whatever the average is
all vals above the ave: some numbers
all vals below the ave: some numbers)
I am having trouble with the int at the end of line and what it is used for, and I keep getting "an array out of bounds exception" when I try to run the compiler.
We are using methods to put our code into.Any Help is appreciated.

This is the code he supplied us with.


The code I wrote to repeat numbers in the output is

This works, but after the numbers from the input file are printed, it takes up the rest of the space with 0's.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Hi, and welcome to the Ranch!

Vincent Mule wrote:
I am having trouble with the int at the end of line and what it is used for


Sorry, it's not clear what you mean here.

, and I keep getting "an array out of bounds exception" when I try to run the compiler.


Copy/paste the exact, complete error message, and indicate clearly which line is causing it. Also, it's not the compiler that's causing that error. That's happening at runtime. Compilation is already complete by that poitn.


The code I wrote to repeat numbers in the output is

This works, but after the numbers from the input file are printed, it takes up the rest of the space with 0's.


Ir you're saying that printArray() method is giving extra zeros, then it's because the array has zeros at the end of it, and you're telling it to print them out.
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

Welcome to the Ranch!

I am having trouble with the int at the end of line and what it is used for, and I keep getting "an array out of bounds exception" when I try to run the compiler.

At what line of your code does the exception occur? It would be ok if you get an exception to also provide us with the message you get.

Until then, I must notice few things that I would change in the design of your class/methods.
As for your methods:
  • No need to declare i variable outside the for loop in printArray method; you can as well declare and initialize it in the for loop since that's the only place where you use it.
  • You are passing count to all your methods, but there is no need for it. You can always get number of elements of the array with arr.length as you do in the method you implemented. The only thing you should pass is the array, unless you declare it as class field. In that case, methods will have no arguments.
  • You are also passing aveVal to two of your methods. Why do that, when you already have a method that calculates that value? Just invoke it in method(s) block to get the value you need, and then use it. Also, those method names (printAllBelow and printAllAbove) really don't say that you're printing values below/above the average value.


  • Edit: And the reason you are getting those zeros at output is because you initialize int array of the size specified at args[1], but you set only as many values as you have in your text file. In your case, you initialize int array to hold 30 values, but there are only 7 in your file; other values are 0.


    The quieter you are, the more you are able to hear.
    Vincent Mule
    Greenhorn

    Joined: Oct 04, 2012
    Posts: 14
    Jeff Verdegan wrote:
    Vincent Mule wrote:
    I am having trouble with the int at the end of line and what it is used for


    Sorry, it's not clear what you mean here.

    , and I keep getting "an array out of bounds exception" when I try to run the compiler.


    I am referring to the input on the command line.
    Vincent Mule wrote:
    take a file from the command line (I.E. \java Program input.txt 30)
    The 30 is the int I am referring to.

    The compiler is now printing out the numbers from the input file.
    The error message is
    Exception in thread "main" java.lang.arrayIndexOutOfBounds Exception: 30
    at Program4.findMin(Program4.java:135)
    at Program4.main(Program4.java:37)


    The code from that section with line 137 is

    and its place where the minVal method is operating in the main class.

    So clearly its an error with finding my minimum value code.
    Kemal Sokolovic
    Bartender

    Joined: Jun 19, 2010
    Posts: 825
        
        5

    You get that exception because at one point (array[w] = array[y];) you are trying to access value at index 30, and the last index of your array is 29. Note that you are still working with just 7 values read from the file, others are zeros.

    Do you really need that much code (rather complicated) for finding a minimum value? Why not try to follow the basic algorithm for it:
  • Suppose the first value from array is the min you are looking for, and assign it to some temporary variable (e.g. min).
  • Iterate over the rest of array; if any value is less than the current min value, assign that value to min.
  • Return min.
  • Vincent Mule
    Greenhorn

    Joined: Oct 04, 2012
    Posts: 14
    Okay.

    I now see all of the setups and I am no longer getting any runtime errors. However, I am having problems with my min, averages sections of my code.

    The find min section of my code.


    For some reason the program keeps returning the max number in the array.

    The average section

    It is just returning my average as 0.00.
    Never Mind fixed the averages.
    Jeff Verdegan
    Bartender

    Joined: Jan 03, 2004
    Posts: 6109
        
        6

    Vincent Mule wrote:Okay.

    I now see all of the setups and I am no longer getting any runtime errors. However, I am having problems with my min, averages sections of my code.

    The find min section of my code.


    For some reason the program keeps returning the max number in the array.


    That array is meant to find the minimum value in the array right? What's cnt for? I assume it's because you created the array ahead of time without knowing how much of it you'd actually use, and you need to tell the method how many of the array's elements to look at. If that's not the case, then what's it for.

    If you're passed an array, and you're going to find the minimum value in that array, you should be looping over that array. There's no loop there.

    But, as already pointed out, you don't need the whole array to find the minimum value that's been input. Just keep track of minimum so far as you're reading, and as you read each value, if it's less than the minimum, update the min to be that value.

    (On the other hand, it is a perfectly valid separation of concerns to do the reading in one step, with no knowledge of the fact that we'll be looking for the min, and the min in another step with no knowledge of where the data came from. If you want to take that approach, then passing an array to a findMin() method is perfectly appropriate. Just make sure you understand the difference between the two approaches, and pick one or the other conclusively.)

    I'm not even going to look at your other method, and neither should you. You should be worrying about one thing at at time, and only move on to the next thing once you get the first thing working.
    Vincent Mule
    Greenhorn

    Joined: Oct 04, 2012
    Posts: 14
    Ok I finally figured it all out.
    the min section


    The thing that was causing me trouble was the nested loop. I tried using a "for" loop and an "if" statement, but it never occurred for me to try both.
    In addition, you need to set the min and max values the opposite you would perceive. I was thinking, since min was going to be the smaller value, it should be min < arr[q]. However, because you are checking to see that if the value of the array at q is LESS than your current minimum value, it needs to be on the other side. (arr[q] < min) In that case, if the "if" statement is false, it returns to the "for" loop statement and increments the value of q by one. Similarly, you need to set your max as if ( max < arr[u]) to set the value at arr[u] as the new max
    I appreciate the help guys, I'm slowly starting to get my head wrapped around this stuff.
    Jeff Verdegan
    Bartender

    Joined: Jan 03, 2004
    Posts: 6109
        
        6

    Vincent Mule wrote:n


    In addition, you need to set the min and max values the opposite you would perceive. I was thinking, since min was going to be the smaller value, it should be min < arr[q].


    I read that code as "for each element in the array prior to index 'cnt', if the current element is less than the min found so far, then update the min found so far to the current element." Seems perfectly natural and intuitive to me. Lines up exactly with how I think of doing it "manually". Keep writing code and it will for you too.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Files from the Command line, converting to ints problems.