Results 1 to 6 of 6
  1. #1
    YeeP is offline Member
    Join Date
    Jul 2010
    Posts
    10
    Rep Power
    0

    Default Count number of integers left in a file, hard with rules allowed!

    Hello, I could really use some suggestions here if anyone has some. I have tried to type up what I feel is required, without all the extra crap. Thank you for any help you can give. The eventual output will go to an outfile, I have only inserted the System.out.println for test purposes, and it will provide things that you may find obvious. Eventually, I will sort the srt array, but this is not where I am stuck.

    Requirement that I am stuck on:
    Array can be assumed integers of size 15, which I will need to eventually sort by using a parallel array.

    Professor states; “The main method should only include method calls, and variable declares”
    Professor states; “The input should be done by a method and the method should return the number of elements in the partially filled array”.
    No problem.

    Professor states; “After the array is filled, use a method to print the list of numbers – include an appropriate heading. Then print on the same page the number of numbers in the array and the number of numbers that do not fit in the array (with appropriate messages).”

    I am trying to use the int variable “more” for the extra integers in the file, but I cannot bring its’ value back to main, to then send to the next method. "elements" works just fine as it is the return, and I can provide her requirement in this method. Based on her examples in class, my guess is she expects me to collect the amount of “extra” integers in the file that cannot fit in the array using: while(inFile.hasNextInt()). This would have to be done in the same method as the one that is filling the array. The only thing I can think of is to make some other method and fill a bogus, non used array, and return the amount of non used integers using that.

    The “S” array will be used to show what the original position of the srt value was before it was sorted in ascending order, later in the program. That is why I have it filled with the data I do.
    -----------------------------------------------------------------------
    Source code:
    Java Code:
    //Notes
    
    import java.io.*;
    import java.util.*;
    
    public class Program10 {
    
     public static void main (String [] args) throws Exception
     {
      int max = 15; //maximum size of array
      int[] srt = new int[max];
      int[] S = new int[max];
      int elements; //number of elements in the array
      int more=0;    //number of elements more that what array has room for
      String filename = "10in.dat";
      Scanner inFile = new Scanner(new FileReader (filename));
      PrintWriter outFile =
                         new PrintWriter("10out.dat");
    
      elements = fillArray(srt, S, more, inFile);
     System.out.println("Main says more is: " +more);
      arrayInfo(elements, srt, more, inFile);
    
      System.out.println("Array Test");
        System.out.println("elements in the partially filled array: " + elements);
      for(int j=0;j<srt.length;j++)
      {
          System.out.println("srt index: " + j + "| srt value: " +srt[j] +
                  "|S value: " + S[j]);
      }  
    
     }//end of void main
    //******************************************************************************
     public static int fillArray(int[] srt, int[] S, int more, Scanner inFile)
     {
      int i=0;     //number of elements in the array
      int overHigh=0; //a "thow away" integer used to move through what is
                         //left in the file
    
         while(i<srt.length && inFile.hasNext())
         {
             srt[i]=inFile.nextInt();
             S[i]= i+1;
             i++;
         }//end while(i<srt.length && inFile.hasNext())
         while(inFile.hasNextInt())
         {
             more++;
             overHigh = inFile.nextInt();
             System.out.println("Value after array: " + overHigh);
         }
      System.out.println("fillArray method more = " +more);
      return i;
     }//end of fillArray(int[] srt, int[] S, Scanner inFile)
    //******************************************************************************
     public static void arrayInfo(int n, int[] srt, int more, Scanner inFile)
     {
     }
    //******************************************************************************
    //******************************************************************************
    //******************************************************************************
    
    
    }//end of Program10
    Data input file:
    ----------------
    Java Code:
    88
    90
    56
    99
    11
    78
    82
    100
    27
    39
    100
    99
    46
    31
    73
    98
    
    21
    7
    100
    ---------------------------------------------------------------------
    Here is my output.
    Java Code:
    ----------------------------
    Value after array: 98
    Value after array: 21
    Value after array: 7
    Value after array: 100
    fillArray method more = 4
    Main says more is: 0
    Array Test
    elements in the partially filled array: 15
    srt index: 0| srt value: 88|S value: 1
    srt index: 1| srt value: 90|S value: 2
    srt index: 2| srt value: 56|S value: 3
    srt index: 3| srt value: 99|S value: 4
    srt index: 4| srt value: 11|S value: 5
    srt index: 5| srt value: 78|S value: 6
    srt index: 6| srt value: 82|S value: 7
    srt index: 7| srt value: 100|S value: 8
    srt index: 8| srt value: 27|S value: 9
    srt index: 9| srt value: 39|S value: 10
    srt index: 10| srt value: 100|S value: 11
    srt index: 11| srt value: 99|S value: 12
    srt index: 12| srt value: 46|S value: 13
    srt index: 13| srt value: 31|S value: 14
    srt index: 14| srt value: 73|S value: 15

  2. #2
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    4

    Default

    I believe the part where you are going wrong is assuming that the integer is passed by reference when in fact it is passed by primitive. That is to say:
    Java Code:
    public int SomeFunction(int a, int b) {
        a = 5;
        b = 6;
        return a;
    }
    public void AnotherFunction() {
        int x = 7;
        int y = 8;
        int Q = SomeFunction(x,y);
        // x == 7, y == 8, Q == 5
    }
    Hopefully this code snippet helps you understand the passing the variables a little better.

    Perhaps what you could do to pass the "more" variable back to the original function is make it the return value. The "elements" variable can easily be figured out in the main method by looping through "srt" and counting the amount of valid elements.

    I'm not sure exactly what the restrictions/rules on your methods are as given by your professor but this should be at least a place to start.

  3. #3
    YeeP is offline Member
    Join Date
    Jul 2010
    Posts
    10
    Rep Power
    0

    Default

    I understand what you are saying, and that is definitely my problem. I am required to return the value of the element variable in the main method.

  4. #4
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    4

    Default

    That seems stupid to me. Not sure why your prof is making you do that.

    That means that you have two options:
    1. Make the "more" variable a class member variable. Then you can set that variable from fillArray.
    2. Your original idea: "The only thing I can think of is to make some other method and fill a bogus, non used array, and return the amount of non used integers using that."

    Depending on the restrictions of your assignment, you have to pick one of those routes to go. If you're required to return the element count from fillArray, then you have no other way to pass a primitive to the original method.

  5. #5
    fourhands is offline Member
    Join Date
    Dec 2010
    Posts
    1
    Rep Power
    0

    Default Did you ever solve this?

    Hi there, I am working on this same assignment for my CS class. Did you ever find a solution to this? could you post it? I'm struggling with it BIG time. Thanks

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    fourhands, welcome to the forum, but I'd like to make some requests:

    *) Please let's not re-animate long dead threads.
    *) If you have your own question, by all means post it, but please do it in a new thread.
    *) Feel free to refer to this thread via a link if desired.
    *) We frown on folks asking for "the code", but enthusiastically help those who show the fruits of their efforts and ask specific questions.

    Again, welcome. I'm now locking this old thread.

    Best of luck.

Similar Threads

  1. how to count number of pages printed?
    By absmarty in forum New To Java
    Replies: 10
    Last Post: 01-31-2012, 06:20 PM
  2. Serial number of exernal hard disk/Thumbdrive
    By manucng in forum Networking
    Replies: 13
    Last Post: 11-14-2011, 08:09 AM
  3. Count number of digits in string using scanner
    By wendysbiggy in forum New To Java
    Replies: 35
    Last Post: 01-20-2010, 05:11 AM
  4. Array count number Occurances
    By gwithey in forum New To Java
    Replies: 2
    Last Post: 04-17-2009, 08:34 PM
  5. Replies: 8
    Last Post: 02-04-2009, 08:55 PM

Posting Permissions

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