Results 1 to 4 of 4
  1. #1
    koreylegend is offline Member
    Join Date
    Dec 2011
    Posts
    8
    Rep Power
    0

    Post How to store information from a file (system.in) into an array?

    any help at all will be very much appreciated I know this will test even advance programmers but again any help will be nice
    Java Code:
    //imports the scanner
    import java.io.*;
    import java.util.Scanner;
    
    public class test 
    {
            String event;
    	String gold;
    	
        public static void main(String[] args) throws IOException
        {
            int invalid = 0;  // valid variable
            int valid = 0;// invalid variable
            boolean generatemedals = false;
            
            // new start
            Scanner scanner = new Scanner(System.in);
            Scanner scan = new Scanner(new File("results.txt"));	// create a scanner which scans from a file and creates a new variable called scan.
            String line = "";	// stores the each line of text read from the file.
    		System.out.println("What country do you want to search (type all to show all country codes)!");
    		String input = scanner.nextLine();
    		
    		String usercountry = input;
    		
    		System.out.println(usercountry + "\r" + "-----------");
    		
    		if ( usercountry.equalsIgnoreCase(usercountry) ) 
    		{
    			generatemedals = true;
    		}
    		
    		while (scan.hasNext()) // this determines whether the next line exists or not and will keep reading until there are no more lines to read and the files will then terminate.
    		{
    				boolean Valid = true; // a boolean that states the variable Valid2 equals to true.
    		        line = scan.nextLine();		// read the next line of text from the file separately.
    		        String[] splitupText = line.split(",");	// split the text into multiple elements from each comma symbol.
    		        if (splitupText.length == 7) // this splits the data in this case into 7 parts of data
    		        {
    				    for (int i = 0; i < splitupText.length; i++) // if i is less than splitupText (7) it adds one to the variable i and  loops over each element
    				    {
    					    splitupText[i] = splitupText[i].trim();
    						                    
    					    if (splitupText[i].length() == 0)// if the length of the line equals 0 this becomes invalid
    						{ 
    						    invalid++;
    						    Valid = false; //in this case the variable Valid2 equals to false
    						    break;
    						}
    				    }
    				    
    				    
    				    if (Valid) // this adds the valid count by one if the file/row equals valid/true
    				    {	
    						valid ++;
    					
    				    }
    				       
    		        } // End if statement
    		        
    		        
    		
    	         else 
    	         {
    	        	 invalid++; // if the process above is false the else statement adds invalid count by one
    	         }
    		        
            } // end of while statement 
    		
    		
        } // end of public static void main
    } //end of public class part 1
    the file to be read:
    High Jump, NIG, PAK, TUR, Aleron Finian, Finnegan Firth, Finlay Firdos
    Pole Vault, ZAM, PAK, TUR, Alger Lech, Kaushik Kegan, Keleman Thorald
    100m Sprint, CAN, FIJ, TUR,Fionn Alejandro, Fisk Leighton, Thor Thorburn
    5000m Jog, ZAM, TUR, GRE,Fisk Lemuel, Jo Kellen, Nick Free
    110m Hurdles, VIE, IND, VIE,Aled Alexas, Lemuel Keled, Takoda Duller
    Javelin, ARG, ARG, SLO,Finley Finn, Len Leith, Katherine Tait
    Shot Put, NIG, LUX, USA,Alfred Alfons, Huge Arms, Bill Smith
    Discus, PAK, PAK, TUR,Leland Kelso, Karel Sharpe, Tajo Takita
    Hammer, ISL, TUR, USA,Lel Kelly, Simon Fish, Josh England
    BMXing, ZAM, AUS, GRE,Alfred Aleron, Lehel, Taksa Solume

    I want to store the bold text which is a country into an array on the program above what is the best way
    to get it to look like this format

    What country do you want to search (type all to show all country codes)!:
    GBR
    ----------
    medals: and then how many medals GRB
    Last edited by Norm; 12-11-2011 at 06:15 PM. Reason: added code tags

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

    Default Re: How to store information from a file (system.in) into an array?

    I want to store the bold text which is a country into an array
    Can you explain what your problem is reading the file and saving the second word in each line to an array?

  3. #3
    koreylegend is offline Member
    Join Date
    Dec 2011
    Posts
    8
    Rep Power
    0

    Default Re: How to store information from a file (system.in) into an array?

    yes i can read the file but i dont know how to then store just the second word in an array

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

    Default Re: How to store information from a file (system.in) into an array?

    You need to parse the line to get the word/String you want so you can store that word in the array.
    The String class has many methods for doing that: indexOf, substring, split for example
    Read the API doc for the String class to see which would be best for you.

Similar Threads

  1. Read in File and Store it in an Array. Please Help!
    By PurpleDemon666 in forum New To Java
    Replies: 1
    Last Post: 10-21-2011, 08:50 PM
  2. need to read information from file into array
    By sadeem in forum New To Java
    Replies: 8
    Last Post: 05-15-2011, 10:00 PM
  3. Store Text-file Into 2D Array
    By benn22 in forum New To Java
    Replies: 20
    Last Post: 03-09-2011, 09:17 PM
  4. Creating Virutal File System from given information
    By hugsheals in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 09-27-2009, 02:32 AM
  5. Replies: 3
    Last Post: 04-07-2008, 06:02 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
  •