Hey,

I have a task to do a program that can transfer Latin word to Morse and I need to read the connection between the letters from a txt file and save both of them to differnet lists (soned and morse) and later return an ArrayList that contains them both. But the problem is that the stuff is not in the lists after the while loop. Could somebody find the solution? Thanks a lot!

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Scanner;
public class MainTest {

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		StringBuffer morseSone = new StringBuffer();
		ArrayList b = Luger("Latin_Morse.txt");
		System.out.println(((String[])b.get(0))[1]); // Output is "null".
	}

	
	static ArrayList Luger(String failinimi) throws IOException{
		ArrayList tagastus = new ArrayList();
		java.io.File file = new java.io.File(failinimi);
		String[] soned = new String[26];
		String[] morse = new String[26];
		try{
			Scanner input = new Scanner(file);
			int i = 0;
			while(input.hasNext()){
				String num = input.nextLine();
				String[] d = num.split(" ");
				soned[i] = d[0];
				morse[i] = d[1];
				System.out.println(morse[i]);// Here everything seems to be OK.
				System.out.println(soned[i]);// Here as well.
				}
			System.out.println(morse[5]);
		}
		catch(FileNotFoundException e){
			System.out.println("File does not exist!");
		}
		System.out.println(morse[5]); //Prints out "null".
		tagastus.add(soned);
		tagastus.add(morse);
		return tagastus;
	}

}

.

This is the txt file.

A .-
B -...
C -.-.
D -..
E .
F ..-.
G --.
H ....
I ..
J .---
K -.-
L .-..
M --
N -.
O ---
P .--.
Q --.-
R .-.
S ...
T -
U ..-
V ...-
W .--
X -..-
Y -.--
Z --..

Are you certain that the code you posted is the latest version that you are executing (no old .class files hanging around anywhere?).
Are you saying that line 39 outputs a "." but line 44 outputs "null".

If both those are yes then I'm baffled...

Oh... I forgot to add "i++" in the while-cycle :P. That was the problem it seems because everything is working at the moment.

This question has already been answered. Start a new discussion instead.