473,320 Members | 2,202 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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:

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
1 1503
r035198x
13,262 8TB
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

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

Similar topics

2
by: Pmb | last post by:
How are character strings input from the user. For example: Suppose I have the code ____________________________________ #include <iostream.h> int main() { char nameString; cout << "Enter...
10
by: Kelly | last post by:
EmployeeTable<Employee> employees;//where EmployeeTable is the array that stores the employees PositionIndex<EmployeeString>employee_nameIndex;//index for the array by last name and first name...
1
by: Estela Peque?o via .NET 247 | last post by:
I'm writing a program that has text boxes where I will be typing information into them and then I have to see if a match is found from a file. How do I check what was typed in to what is in the file?...
1
by: Ramper | last post by:
Have a program that is able to take the numbers out of the .txt if it is just a list of numbers. Problem is, I have a file with Names before the grades. I believe i need to set up another array...
13
by: JJ | last post by:
I have a need to input a large tab delimited text file, which I will parse to check it has the expected columns, before allowing the user to submit it to the database. The user may paste the file...
0
by: ehcy | last post by:
hi.. can you plz help me... i want to put an "==" in my opened text files but i don't know how.. i will show you the original output and my own program.. ...
7
by: chazzy69 | last post by:
Hi, im just trying to figure out a fairly simple way to input fractions into my program, they have to be in the format of 7/2. I thought that mabey i could scan the string and then split it at...
4
by: Keith Hughitt | last post by:
Hi all, I've been looking around on the web for a way to do this, but so far have not come across anything for this particular application. I have found some ways to enable tab completion for...
7
by: kenrocks | last post by:
i have 2 classes 1 main with methods and a gui file on the mainfile I have an aDD method. public void aDD(JTextField a, String r) { int x=0; while(x<r.length) { ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.