Results 1 to 3 of 3
  1. #1
    jwl
    jwl is offline Member
    Join Date
    Jul 2012
    Posts
    93
    Rep Power
    0

    Default max and min scanner problem

    My problem is when I use the the readInputFile with read.nextInt I can only call that method once withch will not work with the max and min methods that I have.

    Java Code:
    package test;
    
    import java.io.File;
    import java.io.IOException;
    import java.util.Scanner;
    
    public class Test {
        /*private Scanner read;
       private int numData;
        
        // Open File
       private void openFile() {
           try {
               read = new Scanner(new File("File.txt")); // reading integers from a file
           }
           catch (IOException e) {
               System.out.println("Could not open file input");
           }
       }
       
       //Close File
       private void closeFile() {
           read.close();
       }
      
       //Read File and Store it in an array of size numData
       private int[] readInputFile() {
           numData = read.nextInt(); // Creating size of array from file
           read.nextLine();
           int[] data = new int[numData]; //Storing integers in an array
            for (int i = 0; i < numData; i++) {
                data[i] = read.nextInt();
                //System.out.printf("%s\n", data[i] + " array item: " +  i); //testing
           }
           return data;
       }*/
       
       private int[] readInputFile() {
           int[] data = {13, 84, 42, 57, 8};
           return data;
       }
       
       private int getMinValue() {
           int[] data = readInputFile();
           int min = data[0];
           for (int i = 0; i < data.length; i++) {
               if (data[i] < data[0])
                   min = data[i];
           }
           return min;
       }
       
       private int getMaxValue() {
           int[] data = readInputFile();
           int max = data[0];
           for (int i = 0; i < data.length; i++) {
               if (data[i] > max)
                   max = data[i];
           }
           return max;
      }
        
       private void test() {
           int min = getMinValue();
           int max = getMaxValue();
           System.out.println("minimum; " + min);
           System.out.println("Maximum: " + max);
       }
       
        public static void main(String[] args) {
            new Test();
        }
        
        public Test() {
           //openFile();
           test();
           //closeFile(); // make sure close file is last
       }
    }

  2. #2
    SJF
    SJF is offline Senior Member
    Join Date
    Oct 2012
    Posts
    108
    Rep Power
    0

    Default Re: max and min scanner problem

    You should change the scope of your data array. Instead of min, and max each having a local variable data, it should be part of the Test object.

    Java Code:
       private Scanner read;
       private int numData;
       private int[] data;
    Then you only need to read the file once. Otherwise close the Scanner, and create a new one (not really efficient).

  3. #3
    jwl
    jwl is offline Member
    Join Date
    Jul 2012
    Posts
    93
    Rep Power
    0

    Default Re: max and min scanner problem

    Quote Originally Posted by SJF View Post
    You should change the scope of your data array. Instead of min, and max each having a local variable data, it should be part of the Test object.

    Java Code:
       private Scanner read;
       private int numData;
       private int[] data;
    Then you only need to read the file once. Otherwise close the Scanner, and create a new one (not really efficient).
    I'm not sure what you mean. This is what I got after trying to change it up a little.
    Java Code:
    package test;
    
    import java.io.File;
    import java.io.IOException;
    import java.util.Scanner;
    
    public class Test {
       private Scanner read;
       private int numData;
       int[] data;
        
        // Open File
       private void openFile() {
           try {
               read = new Scanner(new File("File.txt")); // reading integers from a file
           }
           catch (IOException e) {
               System.out.println("Could not open file input");
           }
       }
       
       //Close File
       private void closeFile() {
           read.close();
       }
      
       //Read File and Store it in an array of size numData
       private int[] readInputFile() {
           numData = read.nextInt(); // Creating size of array from file
           read.nextLine();
           data = new int[numData]; //Storing integers in an array
            for (int i = 0; i < numData; i++) {
                data[i] = read.nextInt();
                //System.out.printf("%s\n", data[i] + " array item: " +  i); //testing
            }
           return data;
            
       }
       
       private int[] data() {
           data = readInputFile();
           data = new int[numData];
           return data;
       }
       
       private int getMinValue() {
            data = data();
           System.out.println(data[9]);
           int min = data[0];
           for (int i = 0; i < data.length; i++) {
               if (data[i] < data[0])
                   min = data[i];
           }
           return min;
       }
       
       private int getMaxValue() {
            data = data();
           System.out.println(data[9]);
           int max = data[0];
           for (int i = 0; i < data.length; i++) {
               if (data[i] > max)
                   max = data[i];
           }
           return max;
      }
        
       private void test() {
           int min = getMinValue();
           int max = getMaxValue();
           System.out.println("minimum; " + min);
           System.out.println("Maximum: " + max);
       }
       
        public static void main(String[] args) {
            new Test();
        }
        
        public Test() {
           openFile();
            //data();
           test();
           closeFile(); // make sure close file is last
       }
    }
    This is not working either. What I want to do is crate the array and then use that array and not have deal with the scanner anymore. I don't know if I even need that data method. I am starting to confuse myself here. Can someone help me get back on track?
    Last edited by jwl; 10-28-2012 at 09:07 PM.

Similar Threads

  1. Problem with the Scanner
    By Maretaga in forum New To Java
    Replies: 6
    Last Post: 07-14-2011, 10:14 AM
  2. Scanner problem.
    By keo in forum New To Java
    Replies: 6
    Last Post: 04-07-2011, 09:30 AM
  3. Scanner problem
    By mkay in forum New To Java
    Replies: 2
    Last Post: 10-28-2010, 01:09 AM
  4. Problem with scanner
    By JavaJ in forum New To Java
    Replies: 6
    Last Post: 03-16-2010, 09:51 PM
  5. Problem with scanner
    By Kangaroo128 in forum New To Java
    Replies: 11
    Last Post: 09-01-2009, 09:07 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
  •