arrays

Sorting Strings and numbers from files arrays

Page 1 of 1

6 Replies - 1042 Views - Last Post: 03 December 2008 - 03:19 PM Rate Topic: -----

#1 anonymous18  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 11-November 08

arrays

Posted 03 December 2008 - 10:17 AM

import java.io.*;
import java.util.Scanner;
/**
 *
 * @author Raven
 */
public class RosetteAsg5 {

   
	public static void main(String[] args) throws FileNotFoundException {
	  final int SIZE = 10;
	  
	  File file = new File("C:/Users/Raven/DocumentsColeAsg.txt");
	  Scanner inputFile = new Scanner(file);
	  
	  
	}

}



Ok I don't even know where to start.I created the text file and everything, I just basically need to know how would i sort strings using arrays
Here is what I have to do

Create a text file (using Notepad or another text editor) with at least 10 lines that contain a last name, a comma, and a double-precision number, like this:
Cole,11371.50
Andrews,22731.40

2. Read each line and put the name into an array of strings and the number into an array of doubles.

3. Sort the array of names.

4. Print the sorted list of names and the associated number next to it, in columns. This means you will have to keep track of which name and number go together. Thus for the above input, you would print:
Andrews 22731.40
Cole 11371.50


Is This A Good Question/Topic? 0
  • +

Replies To: arrays

#2 markhazlett9  Icon User is offline

  • Coding is a lifestyle
  • member icon

Reputation: 60
  • View blog
  • Posts: 1,666
  • Joined: 12-July 08

Re: arrays

Posted 03 December 2008 - 10:19 AM

Well if you're looking to sort an array, you will first have to create one and initialize it with values... next you will have to use either a binary search algorithm or a sequential search algorithm to sort them. Both of these things can be found in the tutorials section. Cheers
Was This Post Helpful? 0
  • +
  • -

#3 anonymous18  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 11-November 08

Re: arrays

Posted 03 December 2008 - 10:34 AM

  */
	public static void main(String[] args) throws FileNotFoundException {
	  final int SIZE = 10;
	  String[] names = new String [SIZE]; 
	  String index;
	 Double[] no = new Double[SIZE];
	  double i = 0;
	  
	  File file = new File("C:/Users/Raven/DocumentsColeAsg.txt");
	  Scanner inputFile = new Scanner(file);
	  
	  while (inputFile.hasNext())
	  {
		  
		 //This where I need to put the array? 
	  }
	  
	  
	}

} 

Was This Post Helpful? 0
  • +
  • -

#4 NeoTifa  Icon User is offline

  • Whorediot
  • member icon





Reputation: 2777
  • View blog
  • Posts: 15,878
  • Joined: 24-September 08

Re: arrays

Posted 03 December 2008 - 10:35 AM

Using a for loop, use the subsrtring functions to use the comma as a delimiter to make the name and the double for each line 2 seperate substrings, and then assign them to corresponding elements to 2 seperate arrays. Then create an algorithm to put them in alphabetical order. After you've posted the code for this I might be able to help some more. ^____^ Hope this helped

This post has been edited by NeoTifa: 03 December 2008 - 10:36 AM

Was This Post Helpful? 0
  • +
  • -

#5 anonymous18  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 11-November 08

Re: arrays

Posted 03 December 2008 - 01:38 PM

View PostNeoTifa, on 3 Dec, 2008 - 09:35 AM, said:

Using a for loop, use the subsrtring functions to use the comma as a delimiter to make the name and the double for each line 2 seperate substrings, and then assign them to corresponding elements to 2 seperate arrays. Then create an algorithm to put them in alphabetical order. After you've posted the code for this I might be able to help some more. ^____^ Hope this helped


I see a lot of help forums, on how to create sub string function with created functions, but none on how to do one with a file.
Was This Post Helpful? 0
  • +
  • -

#6 markhazlett9  Icon User is offline

  • Coding is a lifestyle
  • member icon

Reputation: 60
  • View blog
  • Posts: 1,666
  • Joined: 12-July 08

Re: arrays

Posted 03 December 2008 - 02:54 PM

It's the same type of thing to print to the screen as to a file. Both are done with Scanner. So instead of printing to the screen just set up a scanner for file output and print to the file. Cheers
Was This Post Helpful? 0
  • +
  • -

#7 NeoTifa  Icon User is offline

  • Whorediot
  • member icon





Reputation: 2777
  • View blog
  • Posts: 15,878
  • Joined: 24-September 08

Re: arrays

Posted 03 December 2008 - 03:19 PM

Okay, what you need to do is scan the file, so like


import java.util.Scanner;

    public static void main(String[] args) throws FileNotFoundException {
      final int SIZE = 10;
      String[] names = new String [SIZE]; 
      String index;
     Double[] no = new Double[SIZE];
      double i = 0;
     int i = 0; //FOR COUNTER VARIABLE
      
      File file = new File("C:/Users/Raven/DocumentsColeAsg.txt");
      Scanner inputFile = new Scanner(file);
      
      while (inputFile.hasNext())
      {
          
          String tempString = inputFile.nextLine();
          int index = tempString.indexOf( ',');
          //ASSIGN THE SUBSTRINGS TO ARRAY AT no[i] AND names[i]
          i = i + 1; 
 
      }





}

}
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1