5 Replies - 4781 Views - Last Post: 28 March 2011 - 06:05 PM Rate Topic: -----

#1 Eltoro  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 28-March 11

Problem reading from a file into 2 string arrays

Posted 28 March 2011 - 04:07 PM

import java.util.Scanner;
import java.text.DecimalFormat;
import java.io.*;
public class CommissionsReportFileDemo
{
    
    public static void main(String[] args)throws IOException
    {
                                
                                            
        final double[] rate = {0.01, 0.02, 0.03, 0.05, 0.08}; //Commission Rates
        final double[] salesRanges = {0, 10001, 20001, 40001, 50001}; //Sales Ranges
        final int SALE_ID = 12;
        final int NAME = 12;
             
        String[][]arrayID = new String[SALE_ID][NAME];
             
             
        int index = 0;
        File f = new File("c:\\salesAssociates.txt");
        Scanner inputFile = new Scanner(f);
        while(inputFile.hasNext()&& index < arrayID.length)
             {
                arrayID[index][index]= inputFile.nextLine();
                index++;
             }
                                                          
             inputFile.close();  
             System.out.println(arrayID);
            }
        }
             


I have included the txt file that needs to be read into separate 1 dimensional string arrays. I have managed to complete the rest of my assignment by making a 2 dimensional array within the code that works, but this is where the problem lies. I need to be able to read it from the file into into 2 separate 1 dimensional string arrays.

Attached File(s)


This post has been edited by macosxnerd101: 28 March 2011 - 05:56 PM
Reason for edit:: Please use code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Problem reading from a file into 2 string arrays

#2 Greltam  Icon User is offline

  • D.I.C Head

Reputation: 90
  • View blog
  • Posts: 225
  • Joined: 29-January 09

Re: Problem reading from a file into 2 string arrays

Posted 28 March 2011 - 04:23 PM

parallel Arrays? is the number/name combination connected? If so, create an object that holds the the number and name.
Second,
String[] array1, array2;
array1 = new String[numberOfItems];
array2 = new String[numberOfItems];


voila, 2 separate 1 dimensional arrays;

This post has been edited by Greltam: 28 March 2011 - 04:23 PM

Was This Post Helpful? 0
  • +
  • -

#3 Eltoro  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 28-March 11

Re: Problem reading from a file into 2 string arrays

Posted 28 March 2011 - 04:34 PM

Thank you for the help but my big problem is getting those values from the txt file into the 2, 1dimensional arrays. That what I've been struggling with. Its really frustrating. This is what the txt file looks like.

575
Gene Poole
200
Amanda Linn
150
Anita Bath
535
Faye Tality
375
Ben Dover
600
Sid Down
500
Stan Dupp
300
Sam Manila
610
Ivanna Kiss
400
Val Veeta
100
Daryl Lect
550
Bjorn Free
Was This Post Helpful? 0
  • +
  • -

#4 Greltam  Icon User is offline

  • D.I.C Head

Reputation: 90
  • View blog
  • Posts: 225
  • Joined: 29-January 09

Re: Problem reading from a file into 2 string arrays

Posted 28 March 2011 - 05:34 PM

well, use the scanner to get nextLine(); if you want the numbers in one array, and the names in the other, in your while loop, switch between putting the input from one array to the other every loop.

The way I would do it, is put the number and name on the same line separated by : or some other unused character in the file, use nextline() and then split on whatever i separated with. String[] is returned, which I can do String[0] for the first(number?) and String[1] for the second(name?). Then I would create a new Object of Account(or whatever the data is for), which would accept the parameters of number and name(will have to parse the number for an integer or double). Then I would add that object to a Vector<Account> or ArrayList<Account> and have my Accounts at a fingertips access.
Was This Post Helpful? 0
  • +
  • -

#5 Greltam  Icon User is offline

  • D.I.C Head

Reputation: 90
  • View blog
  • Posts: 225
  • Joined: 29-January 09

Re: Problem reading from a file into 2 string arrays

Posted 28 March 2011 - 05:40 PM

Also, You should have a look at MacOSXNerd101's tutorial on Moving Away From Parallel Arrays

This post has been edited by Greltam: 28 March 2011 - 05:40 PM

Was This Post Helpful? 2
  • +
  • -

#6 Eltoro  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 51
  • Joined: 28-March 11

Re: Problem reading from a file into 2 string arrays

Posted 28 March 2011 - 06:05 PM

Awesome, thank you so much it works
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1