By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,727 Members | 1,268 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,727 IT Pros & Developers. It's quick & easy.

I have a problem inputing from a file to an array

P: 5
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:

Expand|Select|Wrap|Line Numbers
  1. //
  2. import java.io.*;
  3. import java.util.*;
  4. import java.text.*;
  5. import java.util.Arrays;
  6. import java.util.Formatter;
  7. import java.util.Scanner;
  8. import javax.swing.JOptionPane;
  9.  
  10. public class Methods 
  11. {
  12.  
  13.     Scanner infile = new Scanner( "RobotModels.txt" );
  14.     Formatter outfile = new Formatter();
  15.     int count;
  16.  
  17.     String[] robotApp1 = new String[50]; 
  18.  
  19.     public void InputInfo()
  20.     {
  21.  
  22.         int i = 0;
  23.         int count = 0;
  24.         String[] robotName = new String[50];
  25.         String[] robotModel = new String[50];
  26.         double[] robotPrice = new double[50];
  27.         String[] robotApp = new String[50];
  28.  
  29.         try
  30.         {
  31.             infile = new Scanner ( new File ("RobotModels.txt") );
  32.             outfile = new Formatter ( "Robots.txt" );
  33.             System.out.println( "File Opened" );
  34.         }
  35.         catch ( FileNotFoundException fnfe )
  36.         {
  37.             System.out.println( "File Not Found!" );
  38.         }
  39.  
  40.         while ( infile.hasNextLine() )
  41.         {
  42.             robotName[i] = infile.nextLine();
  43.             robotModel[i] = infile.nextLine();
  44.             robotPrice[i] = infile.nextDouble();
  45.             robotApp[i] = infile.nextLine();
  46.             String dummy = infile.nextLine();
  47.             i++;
  48.             count++;
  49.         }
  50.  
  51.         for( int k = 0; k < count; k++ )
  52.         {
  53.             System.out.println( robotName[k] );
  54.             System.out.println( robotModel[k] );
  55.             System.out.println( robotPrice[k] );
  56.             System.out.println( robotApp[k].substring( 0,1 ) );
  57.             System.out.println();
  58.         }
  59.     }
  60. }
  61.  

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
Apr 29 '07 #1
Share this Question
Share on Google+
1 Reply


10K+
P: 13,264
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:

Expand|Select|Wrap|Line Numbers
  1. //
  2. import java.io.*;
  3. import java.util.*;
  4. import java.text.*;
  5. import java.util.Arrays;
  6. import java.util.Formatter;
  7. import java.util.Scanner;
  8. import javax.swing.JOptionPane;
  9.  
  10. public class Methods 
  11. {
  12.  
  13.     Scanner infile = new Scanner( "RobotModels.txt" );
  14.     Formatter outfile = new Formatter();
  15.     int count;
  16.  
  17. String[] robotApp1 = new String[50]; 
  18.  
  19. public void InputInfo()
  20. {
  21.  
  22.     int i = 0;
  23.     int count = 0;
  24.     String[] robotName = new String[50];
  25.     String[] robotModel = new String[50];
  26.     double[] robotPrice = new double[50];
  27.     String[] robotApp = new String[50];
  28.  
  29.     try
  30.         {
  31.             infile = new Scanner ( new File ("RobotModels.txt") );
  32.             outfile = new Formatter ( "Robots.txt" );
  33.             System.out.println( "File Opened" );
  34.         }
  35.         catch ( FileNotFoundException fnfe )
  36.         {
  37.             System.out.println( "File Not Found!" );
  38.         }
  39.  
  40.     while ( infile.hasNextLine() )
  41.     {
  42.         robotName[i] = infile.nextLine();
  43.         robotModel[i] = infile.nextLine();
  44.         robotPrice[i] = infile.nextDouble();
  45.         robotApp[i] = infile.nextLine();
  46.         String dummy = infile.nextLine();
  47.         i++;
  48.         count++;
  49.     }
  50.  
  51.     for( int k = 0; k < count; k++ )
  52.     {
  53.         System.out.println( robotName[k] );
  54.         System.out.println( robotModel[k] );
  55.         System.out.println( robotPrice[k] );
  56.         System.out.println( robotApp[k].substring( 0,1 ) );
  57.         System.out.println();
  58.     }
  59. }
  60. }
  61.  

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.
Apr 30 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.