Hello, i'm new to java and i need to read a file of strings into an array and then sort them but i can't figure out how to do either. i can do it with ints and doubles but i can't with strings. if anyone could help me i'd be very gratful

import java.io.*;
import java.util.*;
public class SortStringsFromFile
{
	public static void main(String[] args)throws IOException
	{
	// O = Orginal
	// S = Sorted
	String[] Ostring = new String[100];
	String[] Sstring = new String[100];
	
	int sizeN = ReadFile(Ostring, Sstring);
	Sorter(Sstring);
	PrintResults(sizeN, Ostring, Sstring);
	
	}
 	
	public static int ReadFile( String[] Ostring, String[] Sstring)throws IOException
	{
		Scanner fileScan = new Scanner(new File( "String.txt"));
		int count = 0;
		while( fileScan.hasNext())
			{
					String word = " ";
					Ostring[count] = word;
					Sstring[count] = word;
					
					
					count++;
			}
			fileScan.close();
			
		return count;
	}
 
 
 	public static void Sorter(String[] Sstring)throws IOException 
	{
	
	java.util.Arrays.sort(Sstring[]);

	
	}

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

Edited 6 Years Ago by Nick Evan: Added code-tags

Where do you consider one String to end and the next to begin? At a newline character? At a comma? For some other delimiting character or regular expression?

For newlines (\n) you can use Scanner and use the nextLine() method, along with a while loop and the hasNextLine() method to read in each line of your input. A simple counter would allow you to add the Strings you read in into the correct part of your String array.

For commas, other characters, or regular expressions, there are a variety of different ways you can do the same thing (some using Scanner, some not) but I'll refrain from mentioning any of them yet because I assume you mean to break at newlines.

As for sorting, you have the right idea by using the Arrays.sort method, but you can't pass in something like that. An example usage of Arrays.sort would be this:

String[] arr = new String[3];
arr[0] = "blah!";
arr[1] = "haha";
arr[2] = "aaa";
Arrays.sort(arr);
for (String s: arr) System.out.println(s);

Edited 6 Years Ago by BestJewSinceJC: n/a

import java.io.*;
import java.util.*;
public class SortStringsFromFile
{
public static void main(String[] args)throws IOException
{
// O = Orginal
// S = Sorted
String[] Ostring = new String[100];
String[] Sstring = new String[100];

int sizeN = ReadFile(Ostring, Sstring);
sizeN = sizeN>=100 ? 100:sizeN;
String[] Ostring1 = new String[sizeN];
String[] Sstring1 = new String[sizeN];
System.arraycopy(Ostring, 0, Ostring1, 0, Ostring1.length);
System.arraycopy(Sstring, 0, Sstring1, 0, Sstring1.length);

Sorter(Sstring1);
PrintResults(sizeN, Ostring1, Sstring1);

}

public static int ReadFile( String[] Ostring, String[] Sstring)throws IOException
{
Scanner fileScan = new Scanner(new File( "C:\\test\\abc.txt"));
int count = 0;
while( fileScan.hasNext())
{
String word = fileScan.next();
Ostring[count] = word;
Sstring[count] = word;
count++;
}
fileScan.close();

return count;
}


public static void Sorter(String[] Sstring)throws IOException 
{
try{
Arrays.sort(Sstring);
}catch (Exception e) {
	e.printStackTrace();
}


}

public static void PrintResults( int sizeN, String[] Ostring, String[] Sstring)throws IOException

{

int i = 0;
System.out.println("Orginal List");
while(i < sizeN)
{
System.out.println(Ostring[i]);
i++;
}
System.out.println(" ");
i = 0;
System.out.println("Sorted List");
while(i < sizeN)
{
System.out.println(Sstring[i]);
i++;
}
}
}

everything was fine, only small change.. u need to copy ur array, as it will store null if file doenst have 100 elements

This article has been dead for over six months. Start a new discussion instead.