Hello, i'm having trouble getting this program to work. It works when i just directly put in the file name, but when i try to use args and i pass it threw the command line. i can't get it to work. I've been testing stuff but i can't seem to figure it out. I use jgrasp and i have been typing in the run arguments line at the top of the program box. If anyone could give me a clue as to why it won't even compile and its saying it cannot find args

// SortDoubleFromFile.java
import java.io.*;
import java.util.*;
public class SortDoublesFromFile
{
	public static void main(String[] args)throws IOException
	{
	// O = Orginal
	// S = Sorted
	double[] Onum = new double[100];
	double[] Snum = new double[100];
	
	int sizeN = ReadFile(Onum, Snum);
	Sorter(sizeN, Snum);
	PrintResults(sizeN, Onum, Snum);
	
	}
 	
	public static int ReadFile( double[] Onum, double[] Snum)throws IOException
	{
		Scanner fileScan = new Scanner(new File( args[0]));
		int count = 0;
		while( fileScan.hasNextDouble())
			{
					double number = fileScan.nextDouble();
					Onum[count] = number;
					Snum[count] = number;
					
					
					count++;
			}
			fileScan.close();
			
		return count;
	}
 
 
 	public static void Sorter( int sizeN,  double[] Snum)throws IOException 
	{
	
	int i = 0;

	
	
	while (i < sizeN)
		{
		int indexOfMin = i;
		int count = i+1;
		while(count < sizeN)	
			{
				if( Snum[count] < Snum[indexOfMin])
					indexOfMin = count;
				count++;
				
			
			}
		
		
		double min = Snum[indexOfMin];
		
		Snum[indexOfMin] = Snum[i];
		Snum[i] = min;
		
		
		i++;
		}
	

	
	}

	public static void PrintResults( int sizeN, double[] Onum, double[] Snum)throws IOException
	
	{
	
		int i = 0;
		System.out.println("Orginal List");
		while(i < sizeN)
			{
			System.out.println(Onum[i]);
			i++;
			}
			System.out.println(" ");
			 i = 0;
		System.out.println("Sorted List");
		while(i < sizeN)
			{
			System.out.println(Snum[i]);
			i++;
			}
		
	   
		
	}




}

Recommended Answers

All 2 Replies

Variable scope. Read about it. The "args" array only exists within the main method, since that is where it was declared (in the method header). If you want to use it from within your ReadFile method, then you had better declare ReadFile as taking a String[] as a parameter. But more likely what you actually want to do is this:

public static void main(String[] args)throws IOException
	{
	
        ....

	int sizeN = ReadFile(Onum, Snum, args[0]);
	
       .....
	
	}
 	
	public static int ReadFile( double[] Onum, double[] Snum, String fileName)throws IOException
	{
		Scanner fileScan = new Scanner(new File(fileName));
		....
	}

I always seem to overlook little things like that.
Thanks alot man, pretty much solves all my worries lol.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.