`java.lang.NumberFormatException` in StreamCipher - Key Parsing Issue

Why am I encountering a java.lang.NumberFormatException in my Java StreamCipher program for a simple stream cipher task? Could someone point me in the right direction? The error occurs when I try to parse the key from the command line arguments using Integer.parseInt(). Specifically, the error message says "For input string: "4294966017"". Here's the relevant snippet from my StreamCipher.java main method:
import java.io.*;
import java.util.*;

public class StreamCipher {

public static void main(String[] args) {
int counter = 0;
int seed = 0;
String input = null;
String output = null;
Random rnd = new Random(seed);

System.out.println("Command line arguments received: " + Arrays.toString(args)); // Print all arguments

while (counter < args.length){
switch (args[counter]) {
case "--key":
String key = args[counter + 1];
System.out.println("Key argument (before parsing): '" + key + "'"); // Print key before parsing
seed = Integer.parseInt(key);
System.out.println("Seed value after parsing: " + seed); // Print seed after parsing (if successful)
counter = counter + 2;
break;
case "--in":
input = args[counter + 1];
counter = counter + 2;
break;
case "--out":
output = args[counter + 1];
counter = counter + 2;
break;
default:
counter = counter + 1;
break;
}
}

// ... rest of the code (file input/output and cipher logic) ...
}

// ... cipher, decipher, xor methods ...
}
import java.io.*;
import java.util.*;

public class StreamCipher {

public static void main(String[] args) {
int counter = 0;
int seed = 0;
String input = null;
String output = null;
Random rnd = new Random(seed);

System.out.println("Command line arguments received: " + Arrays.toString(args)); // Print all arguments

while (counter < args.length){
switch (args[counter]) {
case "--key":
String key = args[counter + 1];
System.out.println("Key argument (before parsing): '" + key + "'"); // Print key before parsing
seed = Integer.parseInt(key);
System.out.println("Seed value after parsing: " + seed); // Print seed after parsing (if successful)
counter = counter + 2;
break;
case "--in":
input = args[counter + 1];
counter = counter + 2;
break;
case "--out":
output = args[counter + 1];
counter = counter + 2;
break;
default:
counter = counter + 1;
break;
}
}

// ... rest of the code (file input/output and cipher logic) ...
}

// ... cipher, decipher, xor methods ...
}
java StreamCipher --key $(<key.txt) --in plain.txt --out cipher-test.data
java StreamCipher --key $(<key.txt) --in plain.txt --out cipher-test.data
7 Replies
JavaBot
JavaBot3w ago
This post has been reserved for your question.
Hey @dghf! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically marked as dormant after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
dghf
dghfOP3w ago
However, when I run this, I get the following error:
Command line arguments received: [--key, 4294966017, --in, plain.txt, --out, cipher-test.data]
Key argument (before parsing): '4294966017'
Exception in thread "main" java.lang.NumberFormatException: For input string: "4294966017"
at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:67)
at java.base/java.lang.Integer.parseInt(Integer.java:662)
at java.base/java.lang.Integer.parseInt(Integer.java:778)
at StreamCipher.main(StreamCipher.java:20)
Command line arguments received: [--key, 4294966017, --in, plain.txt, --out, cipher-test.data]
Key argument (before parsing): '4294966017'
Exception in thread "main" java.lang.NumberFormatException: For input string: "4294966017"
at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:67)
at java.base/java.lang.Integer.parseInt(Integer.java:662)
at java.base/java.lang.Integer.parseInt(Integer.java:778)
at StreamCipher.main(StreamCipher.java:20)
ayylmao123xdd
ayylmao123xdd3w ago
try parse long instead of int
dghf
dghfOP3w ago
So I should change it from seed = Integer.parseInt(key); to seed = Long.parseLong(key);? I got
java StreamCipher --key $(<key.txt) --in plain.txt --out cipher-test.data
Command line arguments received: [--key, 4294966017, --in, plain.txt, --out, cipher-test.data]
Key argument (before parsing) with quotes: "4294966017"
Exception in thread "main" java.lang.NumberFormatException: For input string: "4294966017"
at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:67)
at java.base/java.lang.Integer.parseInt(Integer.java:662)
at java.base/java.lang.Integer.parseInt(Integer.java:778)
at StreamCipher.main(StreamCipher.java:21)
java StreamCipher --key $(<key.txt) --in plain.txt --out cipher-test.data
Command line arguments received: [--key, 4294966017, --in, plain.txt, --out, cipher-test.data]
Key argument (before parsing) with quotes: "4294966017"
Exception in thread "main" java.lang.NumberFormatException: For input string: "4294966017"
at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:67)
at java.base/java.lang.Integer.parseInt(Integer.java:662)
at java.base/java.lang.Integer.parseInt(Integer.java:778)
at StreamCipher.main(StreamCipher.java:21)
after switching to Long.parseLong(key); Oh sorry, I forgot to compile!
java StreamCipher --key $(<key.txt) --in plain.txt --out cipher-test.data
Command line arguments received: [--key, 4294966017, --in, plain.txt, --out, cipher-test.data]
Key argument (before parsing) with quotes: "4294966017"
Seed value after parsing: 4294966017
java StreamCipher --key $(<key.txt) --in plain.txt --out cipher-test.data
Command line arguments received: [--key, 4294966017, --in, plain.txt, --out, cipher-test.data]
Key argument (before parsing) with quotes: "4294966017"
Seed value after parsing: 4294966017
Is this good?
ayylmao123xdd
ayylmao123xdd3w ago
if it doesnt throw any error then i guess it works
JavaBot
JavaBot3w ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.

Did you find this page helpful?