Scanner

Tyring to figure out how to read in a file to my array properly

Page 1 of 1

8 Replies - 778 Views - Last Post: 14 September 2010 - 07:40 PM Rate Topic: -----

#1 LumberJack10  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 26
  • Joined: 27-February 10

Scanner

Posted 14 September 2010 - 06:03 PM

Im trying to read a file into the scanner class. I am also trying to assign values to the file using in.next...Im having a problem with it.

import java.io.*;
import java.util.*;

public class prog1 {

    public static void main(String[] args) {
    	Scanner in = new Scanner(new File("prog0.dat"));
    	int[] list = in.nextInt;

		java.util.Arrays.sort(list);
		System.out.print("Sorted list: " );
		for (int i=0; i<list.length; i++)
			System.out.print(list[i] + " ");
    	System.out.println("\nThe lowest grade is " + minIndex(list));
    	System.out.println("The highest grade is " + maxIndex(list));
    	System.out.println("The mean is " + mean(list));
    	System.out.println("The standard deviation is " + standardDeviation(list));
    	System.out.println("The mode is " + mode(list));
    	System.out.println("The range is " + range(list));
    	System.out.println("The median is " + median(list));
    }

    public static int minIndex(int[] list) {
    	int min = list[0];
    	int minIndex = 0;

    	for (int i = 1; i < list.length; i++)
      		if (min > list[i]) {
        	min = list[i];
        	minIndex = i;
         }

    return min;
    }
    public static int maxIndex(int[] list) {
    	int max = list[0];
    	int maxIndex = 0;

    	for (int i = 1; i < list.length; i++)
      		if (max < list[i]) {
        	max = list[i];
        	maxIndex = i;
      		}

    return max;
    }
   public static int mean(int[] list) {
    int sum = 0;
    for (int i=0; i<list.length; i++) {
        sum += list[i];
    }
    return sum / list.length;
}

  public static double standardDeviation (int[] list) {
   double[] arr = new double[list.length];
	double av = mean(list);
  	for (int i = 0; i <list.length; i++) {
    	arr[i] = (list[i] - av) * (list[i] - av);
 	}
 	double d = 0;
  	for (int i = 0; i < list.length; i++) {
    	d = d + arr[i];
	 }
 	return Math.sqrt(d / (arr.length - 1));
}


    public static int mode(int[] list) {
		int maxValue = 0;
		int maxCount = 0;


		for (int i = 0; i < list.length; ++i) {
		int count = 0;
		for (int j = 0; j < list.length; ++j) {
		if (list[j] == list[i]) ++count;
		}
		if (count > maxCount) {
		maxCount = count;
		maxValue = list[i];
		}
		}

		return maxValue;
		}

     public static int range(int[] list) {
     	int max, min, range;
     	max = list[list.length - 1];
     	min = list[0];
     	range = max - min;
    	return range;
    }

    	private static int median(int[] list) {
			  int med;
			  if ((list.length % 2) == 0) {
			      med = (list[list.length/2] + list[list.length/2 - 1]) / 2;
			  } else {
			      med = list[list.length / 2];
			  }
			  return med;
	  }


  }





Is This A Good Question/Topic? 0
  • +

Replies To: Scanner

#2 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10773
  • View blog
  • Posts: 40,122
  • Joined: 27-December 08

Re: Scanner

Posted 14 September 2010 - 06:14 PM

Remember, all methods have parentheses, and nextInt() is a method. Also, nextInt() returns a single int, not an array. Once you declare and initialize the array, you can assign the int you read in to list[someIndex].

Beyond that, what specifically are your problems or errors?
Was This Post Helpful? 1
  • +
  • -

#3 Codebug  Icon User is offline

  • D.I.C Head

Reputation: 31
  • View blog
  • Posts: 244
  • Joined: 11-October 09

Re: Scanner

Posted 14 September 2010 - 06:19 PM

I'm not trying to hijack the thread but I noticed that the OP was trying to read from a dat file and it made me wonder what all types of files the Scanner can handle. Anyone got a link with a list of valid file types?

Thanks

This post has been edited by Codebug: 14 September 2010 - 06:19 PM

Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10773
  • View blog
  • Posts: 40,122
  • Joined: 27-December 08

Re: Scanner

Posted 14 September 2010 - 06:21 PM

File type doesn't matter for any of the Java file reading tools, including Scanner. At the root, all files have some sort of String data that can be parsed, which is the key.
Was This Post Helpful? 0
  • +
  • -

#5 LumberJack10  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 26
  • Joined: 27-February 10

Re: Scanner

Posted 14 September 2010 - 06:31 PM

This is the only error Im getting

cannot find symbol variable nextInt
Was This Post Helpful? 0
  • +
  • -

#6 Kilorn  Icon User is offline

  • XNArchitect
  • member icon



Reputation: 1356
  • View blog
  • Posts: 3,528
  • Joined: 03-May 10

Re: Scanner

Posted 14 September 2010 - 06:32 PM

As our good friend the macosxnerd pointed out in his first reply, nextInt is a method, and when you use it, you need to include the parenthesis at the end of it. nextInt()
Was This Post Helpful? 0
  • +
  • -

#7 LumberJack10  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 26
  • Joined: 27-February 10

Re: Scanner

Posted 14 September 2010 - 06:42 PM

What about this

G:\CSC 202\prog1.java:14: incompatible types
found : int
required: int[]
int[] list = in.nextInt();
^
1 error

Process completed.
Was This Post Helpful? -1
  • +
  • -

#8 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10773
  • View blog
  • Posts: 40,122
  • Joined: 27-December 08

Re: Scanner

Posted 14 September 2010 - 06:46 PM

View Postmacosxnerd101, on 14 September 2010 - 09:14 PM, said:

Remember, all methods have parentheses, and nextInt() is a method. Also, nextInt() returns a single int, not an array. Once you declare and initialize the array, you can assign the int you read in to list[someIndex].

Beyond that, what specifically are your problems or errors?

I covered that as well. Did you read my post?
Was This Post Helpful? 0
  • +
  • -

#9 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2876
  • View blog
  • Posts: 11,050
  • Joined: 15-July 08

Re: Scanner

Posted 14 September 2010 - 07:40 PM

If you want help, please consider reading our members' posts. We're all here to try to help you. If you don't understand ANYTHING that was said, anything at all, feel free to ask and we will clarify further. There's no shame in it and I do it all the time.

*hint* Look at post #2
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1