how to read a line via char array?
im supposed to output a couple words via a char array..for some reason, its outputting a letter a line..can someone help me? thx
System.out.println((char)position);
the position is an int value as the char is from an array..can someone just modify this so it reads a word a line instead of a letter a line
Message was edited by:
kenvin100
[371 byte] By [
kenvin100a] at [2007-10-3 10:48:24]

any help would be most appreciated
can someone here help me..if u need clarification just ask
RTFMSystem.out.print (versus println)
RTFM can u clarify..i tried print instead of println, it works but i have multiple words..e.g..bedcareits doing thisbedcareget it..?
what does rtfm stand for?
> if u need clarification just askShow us some code.Please paste your code between [code] [/code] tags with the help of the code button just above the message box.It makes the code readable.
k i post..
import javax.swing.*;
import java.io.*;
import java.util.*;
public class DecryptonPractice {
public static void main(String[] args) throws IOException {
String encString = "qwertyuiopasdfghjklzxcvbnm";
char encryptionTable [] = new char [26];
for (int i = 0; i < encString.length(); i++) {
char c = encString.charAt(i);
encryptionTable[i] = c;
}
String input = JOptionPane.showInputDialog("?");
for (int i = 0; i < input.length(); i++) {
char c = input.charAt(i);
int position = -1;
for (int j = 0; j < encryptionTable.length; j++) {
if ( encryptionTable[j] == c) {
position = j;
}
}
position = position - input.length() +96 ;
if (position < 97) {
position = position + 26;
}
System.out.println((char)position);
}
}
}
i just want the output in a word. (note: i tried println, but it doesn't work with multiple words)
Do you want:for (int i = 0; i < input.length(); i++) {
...
System.out.print((char)position);
}
System.out.println();
Regards
RTFM = Read the f..king manual.
can i use another variable besides i? because the program says its already declaredMessage was edited by: kenvin100
> can i use another variable besides i? because the> program says its already declaredWhere is it declared? Try to name it something else.
> can i use another variable besides i? because the> program says its already declaredPlease do!
i named it something else but its ouputting a letter 5 times..
for (int l= 0; l < input.length(); l++) {
System.out.print((char)position);
}
System.out.println();
}
am i missing anything here?
its declared in some other for loop
> > can i use another variable besides i? because the
> > program says its already declared
>
> Please do!
Java 7 compiler option -newbie
javac -newbie HelloWorld.java
Nope I can't compile this. There were 1 error(s) in your source.
error 1: On line 17 you redeclare the i variable which you previously declared on line 11. Please use another name for this variable or stop redeclaring it or something.
For more information see http://java.sun.com/docs/books/tutorial/java/index.html
i followed wat that other guy said with that int i example
> i named it something else but its ouputting a letter
> 5 times..
>
> for (int l= 0; l < input.length(); l++) {
>System.out.print((char)position);
>
>System.out.println();
>
> }
>
> am i missing anything here?
Well position never changes in each loop iteration so I don't know what you expect.
Probably you want to output the value of input at index i.
i will repost code..
import javax.swing.*;
import java.io.*;
import java.util.*;
public class E2 {
public static void main (String [] args) throws IOException {
final int ARRAY_SIZE = 100;
int numWords = 0;
JFileChooser jfc = new JFileChooser();
jfc.showOpenDialog (null);
String file = jfc.getSelectedFile().getAbsolutePath();
Scanner sc = new Scanner (new File(file));
String encString = sc.nextLine();
char[] table = encString.toCharArray();
String [] id = new String [ARRAY_SIZE];
String [] passWord = new String [ARRAY_SIZE];
while(sc.hasNextLine()) {
id[numWords] = sc.nextLine();
passWord[numWords] = sc.nextLine();
numWords++;
}
//cuts id down to the correct size
String[] temp = id;
id = new String[numWords];
for(int i=0; i<numWords; i++){
id[i] = temp[i];
}
//cuts passWord down to the correct size
temp = passWord;
passWord = new String[numWords];
for(int i=0; i><numWords; i++) {
passWord[i] = temp[i];
}
System.out.println("IDs:");
for(int i=0; i><id.length; i++){
System.out.println(id[i]);
}
System.out.println("Passwords:");
for(int i=0; i><id.length; i++){
System.out.println(passWord[i]);
}
//decrypted pwords process
for(int a=0; a><passWord.length; a++){
String input = passWord[a];
for (int i = 0; i >< input.length(); i++) {
char c = input.charAt(i);
int position = -1;
for (int j = 0; j < table.length; j++) {
if (table[j] == c) {
position = j;
}
}
position = position - input.length() +96 ;
if (position < 97) {
position = position + 26;
}
for (int l= 0; l < input.length(); l++) {
System.out.print((char)position);
}
System.out.println();
}
}
}
}
There are a bunch of logical errors and other strangenesses in your code.
This for example
char encryptionTable [] = new char [26];
for (int i = 0; i < encString.length(); i++) {
char c = encString.charAt(i);
encryptionTable[i] = c;
}
is whole lot of bother that could be easily replaced with
char[] encryptionTable = encString.toCharArray();
Then I guess you want to "encrypt" the String by doing that. But your logic in this seems very convoluted and flawed frankly. All you need is something like this.
StringBuffer encryptedString = new StringBuffer();
for(int i=0;i<input.length;i++){
encrypytedString.append(encrypt(input.charAt(i)));
}
String finalString = encryptedString.toString();
And somewhere else...
public static char encrypt(char c){
// turn the char into an int
return encryptionTable[charIntValue];
}
>
> is whole lot of bother that could be easily replaced
> with
>
> char[] encryptionTable =
> encString.toCharArray();
Ive already told him multiple times to do that.
> Then I guess you want to "encrypt" the String by
> doing that. But your logic in this seems very
> convoluted and flawed frankly.
He apparently has a text file with already encrypted Strings in it. He needs to decrypt them.
How many threads do you need on this anyway? http://forum.java.sun.com/thread.jspa?threadID=789321&tstart=0
>
> > Then I guess you want to "encrypt" the String by
> > doing that. But your logic in this seems very
> > convoluted and flawed frankly.
>
> He apparently has a text file with already encrypted
> Strings in it. He needs to decrypt them.
The code from earlier in the thread bears little to no resemblence to that of his last reply which I do see now as the same code from the other thread.
I think I give up.
ah man..i posted the wrong code sry..
and i reason i dont use to charArray is because i never learned that..sry..
in this example..say if i input something, it should come out in a line..but its outputting a letter per line 5 times
import javax.swing.*;
import java.io.*;
import java.util.*;
public class DecryptonPractice {
public static void main(String[] args) throws IOException {
String encString = "qwertyuiopasdfghjklzxcvbnm";
char encryptionTable [] = new char [26];
for (int i = 0; i < encString.length(); i++) {
char c = encString.charAt(i);
encryptionTable[i] = c;
}
String input = JOptionPane.showInputDialog("?");
for (int i = 0; i < input.length(); i++) {
char c = input.charAt(i);
int position = -1;
for (int j = 0; j < encryptionTable.length; j++) {
if ( encryptionTable[j] == c) {
position = j;
}
}
position = position - input.length() +96 ;
if (position < 97) {
position = position + 26;
}
for (int k = 0; k < input.length(); k++) {
System.out.print((char)position);
}
System.out.println();
}
}
}
Message was edited by:
kenvin100
all i need to kno is why its printing a letter out 5 times..e.gcccccooooowwwwlike i only want thiscow
> all i need to kno is why its printing a letter out 5
> times..
> e.g
> ccccc
> ooooo
> wwww
>
> like i only want this
> cow
man, take my advice. give up on writing code for now, go read some very basic tutorial. you're never going to understand this by just guessing what it should be and repeatedly asking the forums if it's right, nobody ever does