7 Replies - 367 Views - Last Post: 18 August 2012 - 08:44 AM Rate Topic: -----

#1 gunu19  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 18-August 12

I need a code that accepts a text file name and then sort the numbers

Posted 18 August 2012 - 01:08 AM

I need a code that accepts a text file name and then sort the numbers present in txt file.
The following code only sorts the numbers from a file name wch has been passes as parameter in the code. When I tried to modify it for input it gives horrible errors... Please help me to modify the code or may be some other approach which you think is easier...


import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class IntegersFromFile {
	public static void main(String[] args) {
		Scanner file = null;
//Scanner file1=SalesData.txt;
		ArrayList<Integer> list = new ArrayList<Integer>();
		
		try {
			file = new Scanner(new File("numbers.txt"));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		
		while(file.hasNext()){

                
			if (file.hasNextInt()) 

                         list.add(file.nextInt());

			else file.next();
		}
		
		Collections.sort(list);
		
		for (Integer i: list) System.out.print(i);
		
	}
}


Is This A Good Question/Topic? 0
  • +

Replies To: I need a code that accepts a text file name and then sort the numbers

#2 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2553
  • View blog
  • Posts: 10,637
  • Joined: 20-September 08

Re: I need a code that accepts a text file name and then sort the numbers

Posted 18 August 2012 - 03:51 AM

Please attach an example file. By the way you're reading it, it looks as if it could be rather strange
import java.io.File;
import java.io.FileNotFoundException;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;


public class IntegersFromFile {
    public static void main(String[] args) {
        Scanner file = null;

        //Scanner file1=SalesData.txt;
        ArrayList<Integer> list = new ArrayList<Integer>();

        try {
            file = new Scanner(new File("numbers.txt"));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        while (file.hasNextInt()) {
            list.add(file.nextInt());
        }

        Collections.sort(list);

        for (Integer i : list)
            System.out.print(i);
	file.close();
    }
}


Will work fine for a properly formatted int file

This post has been edited by g00se: 18 August 2012 - 03:52 AM
Reason for edit:: e.g.

Was This Post Helpful? 1
  • +
  • -

#3 gunu19  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 18-August 12

Re: I need a code that accepts a text file name and then sort the numbers

Posted 18 August 2012 - 06:24 AM

Example of what? I was trying a snippet that could accept the filename on one hand. But could not merge the 2 things==>Acceptinf file name from user+sorting the contents of the file... here's the code that only asks the user to input the filename.. it runs good!
import java.io.*;
import java.util.*;
import java.lang.*;
class CreateFile
{
  public static void main ( String[] args ) 
  {
String fileName=""; 
    FileWriter writer = null;
    BufferedReader user = new BufferedReader(
        new InputStreamReader( System.in ) );
   
    System.out.print("Enter Filename-->"); 
System.out.flush();
   try
    {
      fileName = user.readLine();
     
    }
    catch ( IOException iox )
    {
      System.out.println("Error in creating file"+iox);
      return;
    }
catch( NullPointerException e1)
{
 System.out.println("Error in creating"+ e1);
      return;
}
  
 
  }
}



This post has been edited by g00se: 18 August 2012 - 06:51 AM
Reason for edit:: fixed code tags

Was This Post Helpful? 0
  • +
  • -

#4 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2553
  • View blog
  • Posts: 10,637
  • Joined: 20-September 08

Re: I need a code that accepts a text file name and then sort the numbers

Posted 18 August 2012 - 06:54 AM

Quote

Example of what?

Of an input file

Well it would be more normal (and easier) simply to pass the filename as a parameter to the application
Was This Post Helpful? 0
  • +
  • -

#5 gunu19  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 18-August 12

Re: I need a code that accepts a text file name and then sort the numbers

Posted 18 August 2012 - 06:59 AM

Yes I know... But the question demands the same... n in real life applications, the user who does not know to code, will want to be prompted to input a file name... So is there a way out?? :(
Was This Post Helpful? 0
  • +
  • -

#6 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2553
  • View blog
  • Posts: 10,637
  • Joined: 20-September 08

Re: I need a code that accepts a text file name and then sort the numbers

Posted 18 August 2012 - 07:10 AM

Just use 2 scanners - one for stdin and one for the file
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: I need a code that accepts a text file name and then sort the numbers

Posted 18 August 2012 - 07:53 AM

Scanner keyboard = new Scanner(System.in);
System.out.print("Enter filename: ");
String fileName = keyboard.nextLine();

try {
   Scanner file = new Scanner(new File(fileName));
   ...


Was This Post Helpful? 0
  • +
  • -

#8 gunu19  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 18-August 12

Re: I need a code that accepts a text file name and then sort the numbers

Posted 18 August 2012 - 08:44 AM

View Postpbl, on 18 August 2012 - 07:53 AM, said:

Scanner keyboard = new Scanner(System.in);
System.out.print("Enter filename: ");
String fileName = keyboard.nextLine();

try {
   Scanner file = new Scanner(new File(fileName));
   ...




Thank u a lot:) It worked... really grateful to u.. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1