6 Replies - 588 Views - Last Post: 12 December 2009 - 01:56 PM Rate Topic: -----

#1 NoChoiceMr  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 12-December 09

Splitting a file up and putting into Array

Post icon  Posted 12 December 2009 - 01:08 PM

Hello everyone.
I want to read a file into an array - we will call it myArray1. I want to split the file at a certain point, and make the rest of the file go to myArray2. I have tried using while loop inside a for loop to do this, my attempt is show below. Please do note that I am new to java. Thankyou for your help.


Note: There are 35 numbers in the file

import java.util.*;
import java.math.*;
import java.io.*;


class Test{
	public static void main(String[] args) throws Exception{
	Integer[] myArray1 = new Integer[20];
	Integer[] myArray2 = new Integer[15];
	
	//Reading Rectangle and Triangle address from file 3.....
		Scanner reader = new Scanner(System.in);
		System.out.println("Please specify file name:");
		String fileName = reader.nextLine();
		File inputFile = new File(fileName);
		reader = new Scanner(inputFile);
		
		
		/*  
		IMPORTANT: We are splitting up File .  The first 20 numbers of file will go to myArray1, and the next 15 goes to myArray2*/
		for(int i = 0; i<35; i ++){
			while (i<20){
				myArray1[i] = reader.nextInt();
				System.out.println("Rectangle is" +myArray1[i]);
				i++;
			}
			while (i>19){
				myArray2[i] = reader.nextInt();
				System.out.println("TTTTTTTTTT is" +myArray2[i]);
				i++;
			}
		}
		
		
	}
}



The first bit works - putting the first 20 numbers to myArray1, but the second bit doesn't. I can't work out why.

Is This A Good Question/Topic? 0
  • +

Replies To: Splitting a file up and putting into Array

#2 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2876
  • View blog
  • Posts: 11,051
  • Joined: 15-July 08

Re: Splitting a file up and putting into Array

Posted 12 December 2009 - 01:13 PM

for(int i = 0; i<35; i ++){
			if (i<20){
				myArray1[i] = reader.nextInt();
				System.out.println("Rectangle is" +myArray1[i]);
				i++;
			}
			if (i>19){
				myArray2[i] = reader.nextInt();
				System.out.println("TTTTTTTTTT is" +myArray2[i]);
				i++;
			}
		}



Make them ifs instead, otherwise they will try to read 35 characters i number of times.
Was This Post Helpful? 0
  • +
  • -

#3 NoChoiceMr  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 12-December 09

Re: Splitting a file up and putting into Array

Posted 12 December 2009 - 01:24 PM

Thanks, it makes more sense. I changed one bit

 for(int i = 0; i<35; )


Took out the i++, since i didn't want it to loop 35 times. But, still there is still "Arrayout of bounds" error, and problem points to this line:

 myArray2[i] = reader.nextInt();


Still, the first 20 gets put in, no problem. But, the next 15 does not get put in.
Was This Post Helpful? 0
  • +
  • -

#4 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2876
  • View blog
  • Posts: 11,051
  • Joined: 15-July 08

Re: Splitting a file up and putting into Array

Posted 12 December 2009 - 01:30 PM

Are you still using the if statements? Cause if so, you need to have the i++ in there?

Oh and I didnt look at your code closely enough.
for(int i = 0; i<35; i ++){
			if (i<20){
				myArray1[i] = reader.nextInt();
				System.out.println("Rectangle is" +myArray1[i]);
			}
			if (i>=20){
				myArray2[i] = reader.nextInt();
				System.out.println("TTTTTTTTTT is" +myArray2[i]);
			}
		}



Does that do you any better?
Was This Post Helpful? 0
  • +
  • -

#5 NoChoiceMr  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 12-December 09

Re: Splitting a file up and putting into Array

Posted 12 December 2009 - 01:38 PM

Still the same problem. I really don't understand why this does not work. :crazy:
Was This Post Helpful? 0
  • +
  • -

#6 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2876
  • View blog
  • Posts: 11,051
  • Joined: 15-July 08

Re: Splitting a file up and putting into Array

Posted 12 December 2009 - 01:53 PM

for(int i = 0; i<35; i ++){
			if (i<20){
				myArray1[i] = reader.nextInt();
			}
			if (i>19){
				myArray2[i-20] = reader.nextInt();
			}
		}

		System.out.println("\nArray 1 contains:");
		for (int i = 0; i < myArray1.length; i++)
			System.out.print(myArray1[i] + " ");
		System.out.println("\nArray 2 contains:");
		for (int i = 0; i < myArray2.length; i++)
			System.out.print(myArray2[i] + " ");



Works for me.

You have to do [i-20] because the first index of myArray2 begins at 0 not 20. what might actually be better is:
myArray2[i-myArray1.length] = reader.nextInt()


Was This Post Helpful? 1
  • +
  • -

#7 NoChoiceMr  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 12-December 09

Re: Splitting a file up and putting into Array

Posted 12 December 2009 - 01:56 PM

Thanks. :^:

It was easy to miss! :D atleast for me
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1