Results 1 to 8 of 8
Like Tree1Likes
  • 1 Post By Norm

Thread: Input from text file put into array

  1. #1
    DMarsh12 is offline Member
    Join Date
    Mar 2011
    Posts
    43
    Rep Power
    0

    Default Input from text file put into array

    Hello everyone. I have had assistance from you all before and I have decided to reenter the world of Java and I was wondering if there was anyone here that would be able to assist me. I am trying to take text from a text file, input the different columns into an array, then do a binary search and sort to organize it. I am able to read from the file, but for some reason I can't put it into an array. I have declared all the rows and columns to be one variable using the .hasNext() method. I want to try and do the binary search and sort on my own, but I am having trouble just putting the input into an array. I am going nuts over this so any help would be greatly appreciated. Here is what I have so far.

    Java Code:
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.Scanner;
    
    
    public class readFile 
    {
    	
    	private Scanner inputStream;
    	
    	public void openFile()
    	{
    		 File textFile = new File("text file"); //locates the textfile location
    
    		    inputStream = null; //creates a scanner to read the file
    
    		    try //looks for the file
    		    {
    		        inputStream = new Scanner(textFile);
    		    }
    		    catch (FileNotFoundException e) //if the file isn't located
    		    {
    		        System.out.println("File not found");
    		    }
    	}
    	
    	public void readFiles()
    	{
    		while(inputStream.hasNext())
    		{
    			String ID=inputStream.next(); //puts each column into designated variables
    			String name=inputStream.next();
    			String age=inputStream.next();
    			String GPA=inputStream.next();
    			
    		
    			int newID=Integer.parseInt(ID); //parses strings of ID to integers
    			
    			
    			System.out.println(newID+ "\t\t" + name+ "\t\t" + age+ "\t\t" + GPA);
    						
    			
    		}
    	}
    	
    	public void closeFile()
    	{
    		inputStream.close();
    	}
    	
    	
    
    }
    My text file is as follows:


    735491 Daniel 23 3.01
    829032 Robert 19 2.75
    841490 Nick 22 2.86
    854281 Robert 19 3.99
    998293 Jessica 20 3.02
    874624 Sam 20 3.32
    243921 Michele 34 1.23
    653201 Michael 22 3.08
    990823 Adam 21 3.18
    887512 Alex 21 3.24


    The goal is to put all of the numbers on the left (student IDs) from the text file into an integer array. If I need to completely redo what I have done, I am opening to hearing alternative methods. Thanks again

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,397
    Rep Power
    25

    Default Re: Input from text file put into array

    Where is the array you are trying to put the int data into?
    You need to define the array and a variable to use as index into the array. After you put data into the array at the current index, you need to increment the index to the next slot for the next piece of data.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    DMarsh12 is offline Member
    Join Date
    Mar 2011
    Posts
    43
    Rep Power
    0

    Default Re: Input from text file put into array

    Quote Originally Posted by Norm View Post
    Where is the array you are trying to put the int data into?
    You need to define the array and a variable to use as index into the array. After you put data into the array at the current index, you need to increment the index to the next slot for the next piece of data.
    I have tried implementing the array but the issue is that it says I can't put it into a string array whenever I try to...on top of that all the numbers are being stored to one variable...would that affect the array in some way? thanks for the reply

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,397
    Rep Power
    25

    Default Re: Input from text file put into array

    Where are the arrays you are trying to put data in?
    says I can't put it into a string array
    That's right, you can't assign an int value to a slot in a String array. Or a String value to an int array's slot.
    all the numbers are being stored to one variable
    Use a variable for indexing the array. Change the variables value to point to the next slot.
    Take a look at the tutorial: Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics)
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    DMarsh12 is offline Member
    Join Date
    Mar 2011
    Posts
    43
    Rep Power
    0

    Default Re: Input from text file put into array

    Quote Originally Posted by Norm View Post
    Where are the arrays you are trying to put data in?

    That's right, you can't assign an int value to a slot in a String array. Or a String value to an int array's slot.

    Use a variable for indexing the array. Change the variables value to point to the next slot.
    Take a look at the tutorial: Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics)
    thank you for the link I looked into it and have got myself moving in the right direction... I know my algorithm is bad right now and can be condensed some more but for now I am happy I am at least able to get the numbers into an array...now I just have to figure out how I can compare all of them even though they are in the same variable! :P

    Java Code:
    	public void sortFile()
    	{
    		
    		System.out.println("The sorted file is as follows:");
    		while(inputStream.hasNext())
    			{
    				String ID=inputStream.next();
    				String name=inputStream.next();
    				String age=inputStream.next();
    				String GPA=inputStream.next();
    				String list[]={ID};
    				
    				
    				
    				for (String sort:list)
    				{
    					int[] anArray = new int[9];
    					int newID=Integer.parseInt(sort);
    					{
    						anArray[0]=newID;
    						anArray[1]=newID;
    						System.out.println(anArray[0]);
    						System.out.println();
    						System.out.println(anArray[1]);
    					}
    				//System.out.println(ID+ "\t\t" + name+ "\t\t" + age+ "\t\t" + GPA);
    							
    				}
    			}
    		}

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,397
    Rep Power
    25

    Default Re: Input from text file put into array

    Look at the API doc for the Scanner class. It has methods that will read Strings and convert them it int or double values.
    That might be easier than reading Strings and doing the conversion in your code.

    When using arrays, you need to define them large enough to hold all the data that will be stored in them. Does the program know how many items it will store in the array? Or at least an upperbound that could be used to define an array that is large enough to hold all the possible data to be read.

    The code should use variables to hold the index values. It should not hardcode numbers as indexes:
    anArray[0]=newID;
    vs
    anArray[theIndex]=newID;
    DMarsh12 likes this.
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    DMarsh12 is offline Member
    Join Date
    Mar 2011
    Posts
    43
    Rep Power
    0

    Default Re: Input from text file put into array

    Quote Originally Posted by Norm View Post
    Look at the API doc for the Scanner class. It has methods that will read Strings and convert them it int or double values.
    That might be easier than reading Strings and doing the conversion in your code.

    When using arrays, you need to define them large enough to hold all the data that will be stored in them. Does the program know how many items it will store in the array? Or at least an upperbound that could be used to define an array that is large enough to hold all the possible data to be read.

    The code should use variables to hold the index values. It should not hardcode numbers as indexes:
    anArray[0]=newID;
    vs
    anArray[theIndex]=newID;
    thank you so much for the help and the recommendations. I can not say enough at how helpful you all have been in getting me in the right direction. I appreciate all of your help +rep

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,397
    Rep Power
    25

    Default Re: Input from text file put into array

    You're welcome.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. Replies: 2
    Last Post: 09-29-2013, 12:26 PM
  2. Please help reading input from text file...
    By kkmoslehpour in forum New To Java
    Replies: 11
    Last Post: 10-26-2012, 03:50 AM
  3. Replies: 14
    Last Post: 01-22-2011, 02:36 PM
  4. GUI text display input to list array
    By berndog in forum New To Java
    Replies: 1
    Last Post: 08-08-2010, 06:35 PM
  5. count character in text file as input file
    By aNNuur in forum New To Java
    Replies: 7
    Last Post: 03-25-2010, 04:01 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •