Hi!

I need help with a class project. It is basically an encoder/decoder program, with two parts.

Part 1: Take text entered by a user, convert each character to a number, and save the result to a file.

Part 2: Read each number from the file, convert it to a character, and display the result in a message box.

Here is what I have so far:

public class Oliver_Final_v2a
{

static Scanner console = new Scanner(System.in);

public static void main (String[] args) throws FileNotFoundException
{

	String msg="";
	String outputStr ="";
	
	PrintWriter outFile =  new PrintWriter("Oliver_Encoded.txt");
	
	
	msg = JOptionPane.showInputDialog("Enter (or paste) the text: ");

	for (int i = 0; i < msg.length(); i++)
	{
	 outFile.println((int)(msg.charAt(i)-1));	
		
	}
	
	outFile.close();

}

The first part was easy, but I don't know how to get the numbers from the file and convert them into characters. A few websites I found while researching this mentioned a tokenizer, but that's not covered in my textbook, and I have no idea how to implement it.

Thanks in advance for any help! :)

You can use the Scanner to read from files as well. Take a look at the documention.

I would agree, A scanner would probably be best in this case, if you look at the documentation, there is a nextChar() method that you could use to read the numbers from the file as characters, I have seen this approach work with other programs I have done, try that and see where it goes.....

You only need to do a few things:

Read in the characters from the file. As you go, assign a number to each character (make sure you don't assign the same number to two different characters). Keep these number-character pairs in a list. Then read the numbers from the file, looking at your list to see what character that number corresponds to. Print out the character.


From looking at your code, it doesn't seem that you've stored the number/character pairs anywhere.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.