I have a problem inputing from a file to an array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Icarus27
    New Member
    • Nov 2006
    • 5

    I have a problem inputing from a file to an array

    I am having another problem. I got the first problem fixed and it is working almost right but when I import the information it doesn't pick up the information for the fourth array.

    Here is the code:

    Code:
    //
    import java.io.*;
    import java.util.*;
    import java.text.*;
    import java.util.Arrays;
    import java.util.Formatter;
    import java.util.Scanner;
    import javax.swing.JOptionPane;
     
    public class Methods 
    {
    	
    	Scanner infile = new Scanner( "RobotModels.txt" );
    	Formatter outfile = new Formatter();
    	int count;
    	
        String[] robotApp1 = new String[50]; 
    	   
        public void InputInfo()
        {
        	
        	int i = 0;
        	int count = 0;
        	String[] robotName = new String[50];
        	String[] robotModel = new String[50];
        	double[] robotPrice = new double[50];
        	String[] robotApp = new String[50];
        	
        	try
    		{
    			infile = new Scanner ( new File ("RobotModels.txt") );
    			outfile = new Formatter ( "Robots.txt" );
    			System.out.println( "File Opened" );
    		}
    		catch ( FileNotFoundException fnfe )
    		{
    			System.out.println( "File Not Found!" );
    		}
        	
        	while ( infile.hasNextLine() )
        	{
        		robotName[i] = infile.nextLine();
        		robotModel[i] = infile.nextLine();
        		robotPrice[i] = infile.nextDouble();
        		robotApp[i] = infile.nextLine();
        		String dummy = infile.nextLine();
        		i++;
        		count++;
        	}
        	
        	for( int k = 0; k < count; k++ )
        	{
        		System.out.println( robotName[k] );
        		System.out.println( robotModel[k] );
        		System.out.println( robotPrice[k] );
        		System.out.println( robotApp[k].substring( 0,1 ) );
        		System.out.println();
        	}
        }
    }

    and here is an example of what it is supposed to be importing:

    Roomba
    Discovery
    149.95
    H
    LegoMindstorms
    RCX
    179.95
    T
    LegoMindstorms
    NCT
    249.95
    T
    Viper Lift and Reach
    1700
    15000
    I
    Binford Drillerator
    6000
    945.88
    H
    SpyBot
    EX
    29000
    M

    The fourth line with the single letter is the problem I am having. When I print the last array I only get a blank space printed out.

    Please help.

    Thanks
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by Icarus27
    I am having another problem. I got the first problem fixed and it is working almost right but when I import the information it doesn't pick up the information for the fourth array.

    Here is the code:

    Code:
    //
    import java.io.*;
    import java.util.*;
    import java.text.*;
    import java.util.Arrays;
    import java.util.Formatter;
    import java.util.Scanner;
    import javax.swing.JOptionPane;
     
    public class Methods 
    {
     
    	Scanner infile = new Scanner( "RobotModels.txt" );
    	Formatter outfile = new Formatter();
    	int count;
     
    String[] robotApp1 = new String[50]; 
     
    public void InputInfo()
    {
     
    	int i = 0;
    	int count = 0;
    	String[] robotName = new String[50];
    	String[] robotModel = new String[50];
    	double[] robotPrice = new double[50];
    	String[] robotApp = new String[50];
     
    	try
    		{
    			infile = new Scanner ( new File ("RobotModels.txt") );
    			outfile = new Formatter ( "Robots.txt" );
    			System.out.println( "File Opened" );
    		}
    		catch ( FileNotFoundException fnfe )
    		{
    			System.out.println( "File Not Found!" );
    		}
     
    	while ( infile.hasNextLine() )
    	{
    		robotName[i] = infile.nextLine();
    		robotModel[i] = infile.nextLine();
    		robotPrice[i] = infile.nextDouble();
    		robotApp[i] = infile.nextLine();
    		String dummy = infile.nextLine();
    		i++;
    		count++;
    	}
     
    	for( int k = 0; k < count; k++ )
    	{
    		System.out.println( robotName[k] );
    		System.out.println( robotModel[k] );
    		System.out.println( robotPrice[k] );
    		System.out.println( robotApp[k].substring( 0,1 ) );
    		System.out.println();
    	}
    }
    }

    and here is an example of what it is supposed to be importing:

    Roomba
    Discovery
    149.95
    H
    LegoMindstorms
    RCX
    179.95
    T
    LegoMindstorms
    NCT
    249.95
    T
    Viper Lift and Reach
    1700
    15000
    I
    Binford Drillerator
    6000
    945.88
    H
    SpyBot
    EX
    29000
    M

    The fourth line with the single letter is the problem I am having. When I print the last array I only get a blank space printed out.

    Please help.

    Thanks
    Maybe you have leading spaces in your lines. Try trimming the lines first before doing the substring.

    Comment

    Working...