7 Replies - 1657 Views - Last Post: 08 April 2012 - 01:39 AM Rate Topic: -----

#1 ILoveJava   User is offline

  • D.I.C Regular

Reputation: 29
  • View blog
  • Posts: 389
  • Joined: 12-March 12

Input Stream problems.

Posted 05 April 2012 - 01:56 AM

Hey guys, I'm trying to read in System input for encryption in my current project, but I can't get the input stream working, as the encrypt key is alphanumeric, and I'm not experienced on using readers. Whenever I try compile, I get an error.
[email protected]:~/Desktop/Programming/Java/Encryption$ javac *.java
Encrypt.java:10: incompatible types
found   : int
required: java.lang.String
			String str = br.read();
			                    ^
1 error

This happens no matter what I set the data type to. My code is below if anyone is able to assist me on this.
import java.util.*;
import java.io.*;

public class Encrypt {
	
	public static void main(String[] args) throws Exception {
  	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    
		System.out.print("Enter string to be encrypted: ");
			String str = br.read();
	}
	
	public static void readFile() throws Exception {
		String text = "";
	
		ArrayList<Key> encryptKey = new ArrayList<Key>();
		Scanner read = new Scanner(new File("encrytionKey.txt"));
		
		while(read.hasNext()) {
			String character = read.next();
			String endKey = read.next();
			
			encryptKey.add(new Key(character, endKey));
		}
		
		for(int i = 0; i < encryptKey.size(); i++) {
			encryptKey.get(i).print();
		}
	}
}


Is This A Good Question/Topic? 0
  • +

Replies To: Input Stream problems.

#2 karabasf   User is offline

  • D.I.C Regular
  • member icon

Reputation: 202
  • View blog
  • Posts: 417
  • Joined: 29-August 10

Re: Input Stream problems.

Posted 05 April 2012 - 02:03 AM

Exactly what I expected...

The BufferedReader.read() method returns an integer, according to the documentation:

Quote

http://docs.oracle.c...html#read%28%29

read
public int read()
throws IOException

Read a single character.


So, this is not going to work. However, you want to read a console input, and store it in a string variable. So instead of using read(), use readLine() instead. ;)

Hope this helps you out ^^

PS
Actually been awhile that I used BufferedReaders =p

This post has been edited by karabasf: 05 April 2012 - 02:04 AM

Was This Post Helpful? 0
  • +
  • -

#3 ILoveJava   User is offline

  • D.I.C Regular

Reputation: 29
  • View blog
  • Posts: 389
  • Joined: 12-March 12

Re: Input Stream problems.

Posted 05 April 2012 - 03:25 AM

But then how do I break it down to each character?
Was This Post Helpful? 0
  • +
  • -

#4 karabasf   User is offline

  • D.I.C Regular
  • member icon

Reputation: 202
  • View blog
  • Posts: 417
  • Joined: 29-August 10

Re: Input Stream problems.

Posted 05 April 2012 - 03:34 AM

Well, there are multiple ways to do. Either you obtain a string and convert it to a character array.

Or you read character for character (not sure how this would work though).

The first approach would look something like:
//This converts an user input string into a character array
char[] characters = br.readLine().toCharArray();



The second approach, as the read() method works as following: It reads a character and converts it into an integer. This means that in order to obtain a character, you have to cast it back into a character.

//Get the character (which is represented by an integer)
int characterRepresentation = br.read();

//Cast the integer into a character to obtain the actual character, instead of an integer
char character = (char)characterRepresentation;



For the latter, you will need a loop in order to get all the characters of a string (that is, if I am not mistaken)

Hope this helps you out ^^

This post has been edited by karabasf: 05 April 2012 - 03:35 AM

Was This Post Helpful? 1
  • +
  • -

#5 ILoveJava   User is offline

  • D.I.C Regular

Reputation: 29
  • View blog
  • Posts: 389
  • Joined: 12-March 12

Re: Input Stream problems.

Posted 05 April 2012 - 04:11 AM

The first approach worked perfect! Thank you good sir! Now I just need to figure out how to reference the different characters against the ArrayList the encryptionKey.txt data is stored in.
Was This Post Helpful? 0
  • +
  • -

#6 karabasf   User is offline

  • D.I.C Regular
  • member icon

Reputation: 202
  • View blog
  • Posts: 417
  • Joined: 29-August 10

Re: Input Stream problems.

Posted 05 April 2012 - 04:35 AM

Glad I could help. ^^

Something you could do is (since you're making an ArrayList containing the encryption keys) is by making a procedure.

char encryptCharacter(char a)
  for each Key in encryptKey
     //If the user specified character is equal to the original key, it needs to be mapped to the encrypted key
     if(char a == Key.original) 
          return Key.Encrypted



So this procedure accepts a user key, loops through all the encrypt key elements and returns the encrypted character (which I assume to be another character)

This means that I will have to loop through each character in my obtained character array

i = 0;
for each char c in charArray
   //charEncrypt is a character array containing the encrypted characters
   //This array has the same size as my charArray
   charEncrypt[i] = encryptCharacter(c);
   i++;



This will loop through each character in the charArray, and fill the encrypted char array with the encrypted characters.

Good luck! ^^

Edit: note that posted code is not actual Java code, it's pseudocode describing what it should do

This post has been edited by karabasf: 05 April 2012 - 04:35 AM

Was This Post Helpful? 0
  • +
  • -

#7 ILoveJava   User is offline

  • D.I.C Regular

Reputation: 29
  • View blog
  • Posts: 389
  • Joined: 12-March 12

Re: Input Stream problems.

Posted 08 April 2012 - 12:30 AM

No matter what I do, for some reason, I cannot get this to work. If anyone is able to elaborate further on how the last post by Karabasf is supposed to be implemented, that would be great!
Was This Post Helpful? 0
  • +
  • -

#8 karabasf   User is offline

  • D.I.C Regular
  • member icon

Reputation: 202
  • View blog
  • Posts: 417
  • Joined: 29-August 10

Re: Input Stream problems.

Posted 08 April 2012 - 01:39 AM

Haha, well you got to rely on me this time :P

So lets consider what needs to be done:
- We need to have an Array(list) to store our Key objects. This Array(list) should be available for each method and part of our class
- We need an user enter a string
- We need to encrypt our string

So, we can already identify that we need 3 operations:
- Read a file to get the key combinations (I don't know if you made a Key object or how it looks like)
- Get our user input
- Encrypt the string

Ok, now we identified our requirements and operations, we can set up our program.

First, we want our ArrayList available to other methods. We can do this by two manners. Or we make a "global" ArrayList, or we let the readFile() method return an ArrayList instead. I'll go for the latter option as its a little bit more flexible.

So. lets start with that part. I assume that your Encryptkey file looks like:

a b
b c
. .
. .
z a


It doesn't really matter how it actually looks, but notice that I assumed there is a space between the Actual key and the EncryptKey. The method then looks

public ArrayList<Keys> readFile() throws IOException {
	//Make a bufferedreader and get an Inputstream 
	
	//Instantiate an arraylist
	ArrayList<Keys> keys = new ArrayList<Keys>()
	
	String readLine = br.readLine(); //br = bufferedreader
	while(readLine != null) {
		//Split the string in two parts (use a string tokenizer for example)
		String oriChar = readLine(1) //first part of the readline string. Figure out yourself how you'd do this
		String encryptChar = readLine(2) //second part of the readline string
		
		//Make a new KeyObject and add in the ArrayList
		keys.Add(Key(oriChar, encryptChar))
		
		readLine = br.readLine(); //Read the next line
	}
	
	return keys
}



Keep notice that you need to handle the thrown IOExceptions if there are any.

Getting your user input wasn't that hard, so we'll continue. Notice that I'll take a string and an arraylist as an input

public String encryptKey(String userInput, ArrayList<Keys> keys)
  String encryptedKey

  Outer:
  for each char c in string userInput
     for each Key k in keys
        //check if c matches original key of object k
        if(c.equals(k.getOrikey())
            //If it matches, append the encryptedKey
            encryptedKey += k.getEncryptedKey
            contiue outer; //No need to look further in keys. Continue with next char
  
  return encryptedKey; 



Finally , our main program would look like
public static void main(String[] args)
  ArrayList<Keys> myKeys = readFile();
  
  //Get the user string
  String userInput = readLine();

 //Encrypt
 String encryptedKey = encryptString(userInput, myKeys);

 //Do whatever you like



Now, what we didn't consider was (but maybe later)
- What if the encryptedKey.txt contains:

a b c
b


?

- What if an entry was not found (say a did not have an encrypted key, what then?)

Also, I think it's maybe easier to use a HashMap instead of an ArrayList with key objects. This is probably due to the performance. Say if I'd want to encrypt a string. The worst runtime would be of the order O(n*m), where n is the amount of characters in your string and m the amount of Key objects.
A Hashmap on the other hand has a performance of O(1 + n/k) on average and O(n) if you need to search something. (Lets just say it's near constant)

A lot of considerations to make, but one coder to decide ;)

Good luck and have fun ^^
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1