Question:
Why isn't my Java program reading a file correctly?
?
2014-02-19 15:30:00 UTC
import java.io.*;
import java.util.Scanner;
import java.io.IOException;

public class MorseCodeTester
{
public static void main(String [] args)throws IOException
{
File file = new File("MorseCodeList.txt");
Scanner inFile = new Scanner(file);
String [] morseCode = new String[36];
String [] letters = {"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",
"1", "2", "3", "4", "5", "6", "7", "8", "9", "0", " "};
int x = 0;
while(inFile.hasNextLine())
{
morseCode[x] = inFile.nextLine();
x++;
}

for(int i = 0; i < morseCode.length; i++)
{
System.out.println(morseCode[i]);
}


Scanner userInput = new Scanner(System.in);
int i = 0;
System.out.println("Please enter input with no punctuation");
String userString = userInput.nextLine();
System.out.println("");
MorseCode.convert(userString, morseCode, letters);
}

}


import java.util.Arrays;
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
public class MorseCode
{
public MorseCode()
{
}

public static void convert (String toEncode, String [] letters, String [] morseCode)
{
String morse = toEncode;
int len = morse.length();
String [] convertedText = new String[len];
for(int y = 0; y < len; y++)
{
convertedText[y] = (morse.substring(y,y+1));
}
String [] morseCodeText = new String[len];
for(int i = 0; i < convertedText.length; i++)
{
for(int x = 0; x < 36; x++)
{
if(convertedText[i].equals (letters[x]))
{
morseCodeText[i] = morseCode[x];
}
else
{
x++;
}
}
i++;
}

for(int z = 0; z < morseCodeText.length; z++)
{
System.out.print(morseCodeText[z]);
}
}
}



The program is translating English to morse code. I am only printing the entire text file because I wanted to test it.

This is what the output looks like:
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
null
Please enter input with no punctuation
hello

nullnullnullnullnull




This is the text file:
.-
-…
-.-.
-..
.
..-.
—.
….
..
.—
-.-
.-..
--
-.
——
.—-.
—-.-
.-.
...
-
..-
...-
.—-
-..-
-.--
--..
.----
..---
...--
....-
.....
-....
--...
---..
----.
-----
(these are in order from a-z, followed by 0-9)
Four answers:
2016-12-25 00:34:23 UTC
1
?
2016-04-29 13:43:25 UTC
If you intend to train your child easily how to learn then Children Learning Reading from here https://tr.im/MOdQb can help you.

Children Learning Reading is made by short instructions, enough to carry the eye course of a tiny child but can be powerful enough to instruct the child to read — actually at an extremely early age.

This system is based about a idea named phonemes, which are (in very simple terms), the sounds that produce up words we use within our daily language. This program attempts to show your youngster to see by first gathering your child's capacity to read and understand the phonemes that produce up daily words. After your son or daughter may try this then they've all the equipment they should start creating feeling of new phrases, that may subsequently make their examining abilities tougher and stronger.
Bob
2014-02-19 16:13:01 UTC
It looks like it's not reading the morse code text file correctly. The first thing I would do is add an extra debug statement in your reading loop:



int x = 0;

while(inFile.hasNextLine())

{

morseCode[x] = inFile.nextLine();

System.out.println ("x=" + x + " Morse: " + morseCode[x]);

x++;

}



This way you'll see if it's reading the file.



BTW, I've had problems with file specs like this: new File("MorseCodeList.txt");



I've found it's best to write the whole file spec, starting from the top level folder:

C:\\folder\\folder\\ .... \\ MorseCodeList.txt



Note that \ is a special char in Java strings, so you need to double them up.
2014-09-18 01:30:48 UTC
Well

The ability to read is vital for success. It helps your child succeed in school, helps them build self-confidence, and helps to motivate your child. Being able to read will help your child learn more about the world, understand directions on signs and posters, allow them to find reading as an entertainment, and help them gather information.



Here you can find a step-by-step online program that can help your child learn to read: http://readingprogram.toptips.org



Learning to read is very different from learning to speak, and it does not happen all at once. There is a steady progression in the development of reading ability over time. The best time for children to start learning to read is at a very young age - even before they enter pre-school. Once a child is able to speak, they can begin developing basic reading skills. Very young children have a natural curiosity to learn about everything, and they are naturally intrigued by the printed texts they see, and are eager to learn about the sounds made by those letters. You will likely notice that your young child likes to look at books and thoroughly enjoys being read to. They will even pretend to behave like a reader by holding books and pretend to read them.



For more info visit http://readingprogram.toptips.org

Bye Bye


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...