Results 21 to 28 of 28
- 09-23-2010, 05:37 AM #21
Member
- Join Date
- Sep 2010
- Posts
- 16
- Rep Power
- 0
Norm,
I think I fixed the incompatability issue...
Now I am having trouble w/doing a constant divide. I am trying to use the divideAndRemainder() function, save the remainder, and display it as a character. I have attached the code, and output of where I am stuck.
import java.io.*;
import java.math.*;
import java.util.*;
public class Homework1 {
static BigInteger[] great;
static BigInteger[] key;
static char[] plaintext = new char[1000];
static int count;
static int count1;
static int count2;
static long base = 128;
static BigInteger base2 = BigInteger.valueOf(base);
public static BigInteger readKey() throws IOException
{
FileReader fr1 = new FileReader("key.txt");
BufferedReader br1 = new BufferedReader(fr1);
String line;
count1 = 0;
while((line = br1.readLine()) != null)
{
key[count1] = new BigInteger(line);
//System.out.println("Line... " +line);
count1++;
}
//System.out.println("Let's see... "+ key[0]);
fr1.close();
return key[0];
}
public static void readPlainText() throws IOException
{
FileReader fr2 = new FileReader("plaintext.txt");
BufferedReader br2= new BufferedReader(fr2);
String line1;
String temp;
int temp2[] = new int[10000];
StringBuilder sb = new StringBuilder();
BigInteger code;
count2 = 0;
while((line1 = br2.readLine()) != null)
{
line1.getChars(0, line1.length(), plaintext, 0);
for(int k = 0; k < line1.length(); k++)
{
temp2[count2] = (int)plaintext[k] * 128;
sb.append(temp2[count2]);
count++;
}
}
fr2.close();
BigInteger temp3 = new BigInteger(sb.toString());
code = temp3.multiply(key[0]);
try{
PrintStream out = new PrintStream(new FileOutputStream("encryption.txt"));
out.println(code);
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public static void readBigNumbers(BigInteger temp3) throws IOException
{ FileReader fr = new FileReader("encryption.txt");
BufferedReader br = new BufferedReader(fr);
String line;
//System.out.println("New base from 128, to big integer... "+ base2);
count = 0;
while ((line = br.readLine()) != null)
{ great[count] = new BigInteger(line);
//System.out.println(great[count]);
count++;
}
fr.close();
System.out.println("Test 1... "+great[0]); //Encryption file
System.out.println("Test 2... "+temp3); //Key
BigInteger storage = new BigInteger(great[0].toString());
BigInteger k_temp = new BigInteger(temp3.toString());
//System.out.println("Prelim test..." +storage);
//System.out.println("New prelim test..." +k_temp);
BigInteger storage1[] = storage.divideAndRemainder(k_temp);
BigInteger storage2[] = storage1[0].divideAndRemainder(base2);
System.out.println("This is being shown...");
System.out.println("What is this? "+storage2[1].toString());
*while(storage2[1] != BigInteger.valueOf(0))*
*{*
*System.out.println(storage2[1]);*
*System.out.println("Is this going to show?");*
*storage1[0].divideAndRemainder(base2);*
*}*
System.out.println("Or what about this?");
System.out.println("Quotient... "+storage.divide(k_temp));
/*System.out.println("Integral part... "+storage[1]);
System.out.println("Remainder... "+storage[1]);*/
}
public static void main(String argv[])throws IOException
{ great = new BigInteger[100];
key = new BigInteger[5];
String line;
int selection = 0;
BigInteger temp2;
System.out.println("Please make your selection.\n 1. Encryption \n 2. Decryption\n");
try{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
line = in.readLine();
selection = Integer.parseInt(line);
}
catch (IOException e) {
System.out.println("Unexpected IO Error: " + e);
}
switch(selection) {
case 1:
System.out.println("You have selected Encryption.");
temp2 = readKey();
readPlainText();
System.out.println("The encryption.txt file has been created.");
break;
case 2:
System.out.println("You have selected Decryption.");
temp2 = readKey();
readBigNumbers(temp2);
/* System.out.println("\n Printing from great[] \n");
for (int i = 0; i < count; i++)
System.out.println(great[i].toString());*/
break;
}
System.out.println("\n *** Program Finished *** ");
}
}
Output that I am getting:
Please make your selection.
1. Encryption
2. Decryption
2
You have selected Decryption.
Encryption... 20436095433186426617233657404817259315569049355510 36645458759259730927
3543083039960605160641280
Key... 21574986861247560388341
This is being shown...
What is this? 0 <== This is SUPPOSED to be the division, showing zero
Or what about this?
Quotient... 94721241614720139521344014080129284096947212416126 721369614720142081
4080
*** Program Finished ***
Any assistance would be great.
Thanks!
- 09-23-2010, 05:38 AM #22
Member
- Join Date
- Sep 2010
- Posts
- 16
- Rep Power
- 0
Java Code:import java.io.*; import java.math.*; import java.util.*; public class Homework1 { static BigInteger[] great; static BigInteger[] key; static char[] plaintext = new char[1000]; static int count; static int count1; static int count2; static long base = 128; static BigInteger base2 = BigInteger.valueOf(base); public static BigInteger readKey() throws IOException { FileReader fr1 = new FileReader("key.txt"); BufferedReader br1 = new BufferedReader(fr1); String line; count1 = 0; while((line = br1.readLine()) != null) { key[count1] = new BigInteger(line); //System.out.println("Line... " +line); count1++; } //System.out.println("Let's see... "+ key[0]); fr1.close(); return key[0]; } public static void readPlainText() throws IOException { FileReader fr2 = new FileReader("plaintext.txt"); BufferedReader br2= new BufferedReader(fr2); String line1; String temp; int temp2[] = new int[10000]; StringBuilder sb = new StringBuilder(); BigInteger code; count2 = 0; while((line1 = br2.readLine()) != null) { line1.getChars(0, line1.length(), plaintext, 0); for(int k = 0; k < line1.length(); k++) { temp2[count2] = (int)plaintext[k] * 128; sb.append(temp2[count2]); count++; } } fr2.close(); BigInteger temp3 = new BigInteger(sb.toString()); code = temp3.multiply(key[0]); try{ PrintStream out = new PrintStream(new FileOutputStream("encryption.txt")); out.println(code); out.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } } public static void readBigNumbers(BigInteger temp3) throws IOException { FileReader fr = new FileReader("encryption.txt"); BufferedReader br = new BufferedReader(fr); String line; //System.out.println("New base from 128, to big integer... "+ base2); count = 0; while ((line = br.readLine()) != null) { great[count] = new BigInteger(line); //System.out.println(great[count]); count++; } fr.close(); System.out.println("Test 1... "+great[0]); //Encryption file System.out.println("Test 2... "+temp3); //Key BigInteger storage = new BigInteger(great[0].toString()); BigInteger k_temp = new BigInteger(temp3.toString()); //System.out.println("Prelim test..." +storage); //System.out.println("New prelim test..." +k_temp); BigInteger storage1[] = storage.divideAndRemainder(k_temp); BigInteger storage2[] = storage1[0].divideAndRemainder(base2); System.out.println("This is being shown..."); System.out.println("What is this? "+storage2[1].toString()); *while(storage2[1] != BigInteger.valueOf(0))* *{* *System.out.println(storage2[1]);* *System.out.println("Is this going to show?");* *storage1[0].divideAndRemainder(base2);* *}* System.out.println("Or what about this?"); System.out.println("Quotient... "+storage.divide(k_temp)); /*System.out.println("Integral part... "+storage[1]); System.out.println("Remainder... "+storage[1]);*/ } public static void main(String argv[])throws IOException { great = new BigInteger[100]; key = new BigInteger[5]; String line; int selection = 0; BigInteger temp2; System.out.println("Please make your selection.\n 1. Encryption \n 2. Decryption\n"); try{ BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); line = in.readLine(); selection = Integer.parseInt(line); } catch (IOException e) { System.out.println("Unexpected IO Error: " + e); } switch(selection) { case 1: System.out.println("You have selected Encryption."); temp2 = readKey(); readPlainText(); System.out.println("The encryption.txt file has been created."); break; case 2: System.out.println("You have selected Decryption."); temp2 = readKey(); readBigNumbers(temp2); /* System.out.println("\n Printing from great[] \n"); for (int i = 0; i < count; i++) System.out.println(great[i].toString());*/ break; } System.out.println("\n *** Program Finished *** "); } }Java Code:Output that I am getting: Please make your selection. 1. Encryption 2. Decryption 2 You have selected Decryption. Encryption... 20436095433186426617233657404817259315569049355510 36645458759259730927 3543083039960605160641280 Key... 21574986861247560388341 This is being shown... What is this? 0 <== This is SUPPOSED to be the division, showing zero Or what about this? Quotient... 94721241614720139521344014080129284096947212416126 721369614720142081 4080 *** Program Finished ***
- 09-23-2010, 05:39 AM #23
Member
- Join Date
- Sep 2010
- Posts
- 16
- Rep Power
- 0
Java Code:Output that I am getting: Please make your selection. 1. Encryption 2. Decryption 2 You have selected Decryption. Encryption... 20436095433186426617233657404817259315569049355510 36645458759259730927 3543083039960605160641280 Key... 21574986861247560388341 This is being shown... What is this? 0 <== This is SUPPOSED to be the remainder, showing zero Or what about this? Quotient... 94721241614720139521344014080129284096947212416126 721369614720142081 4080 *** Program Finished ***
- 09-23-2010, 06:00 AM #24
Now also on another forum
Java Programming - Problems w/BigIntegers
db
-
Please do not cross-post without informing us. Keep doing this and folks might decide not to help you in the future (here or in other forums). Best of luck.
- 09-23-2010, 03:47 PM #26
Member
- Join Date
- Sep 2010
- Posts
- 16
- Rep Power
- 0
Darrl.Burke and Fubarable,
I am very sorry about the cross-post. I didn't know these two sites were related, or the same.
It will not happen again.
Thank you for your help.
-mzjazzygirl
- 09-23-2010, 04:10 PM #27
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
They're not related, but it is annoying to answer a question in a forum only to discover that it has already been answered on another forum. Never mind being something of a waste of time.
By the way, is your code normally unformatted like that?
- 09-23-2010, 06:37 PM #28
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Similar Threads
-
BigInteger
By windows.login in forum New To JavaReplies: 8Last Post: 07-13-2010, 01:10 PM -
convert unsigned integer to signed integer in java?
By diskhub in forum New To JavaReplies: 6Last Post: 05-17-2010, 12:50 AM -
My own BigInteger class. Need help.
By Dinde in forum New To JavaReplies: 2Last Post: 01-27-2010, 08:49 PM -
[SOLVED] [newbie] converting Integer to java.lang.Enum.Modifier
By jon80 in forum New To JavaReplies: 24Last Post: 05-18-2009, 10:23 AM -
how to convert from BigInteger to Hex
By nanaji in forum Advanced JavaReplies: 10Last Post: 05-22-2008, 12:44 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks