i'm tring to run argument from ubuntu console.
./myTool -h
and all i get is only the print of "1".
someone can help please ?
thanks !
public static void main(String[] argv) throws Exception
{
System.out.println("1");
for(int i=0;i<argv.length;i++)
{
if (argv.equals("-h"))
{
System.out.println("-ip target ip address\n");
System.out.println("-t time interval between each scan in milliseconds\n");
System.out.println("-p protocol type [UDP/TCP/ICMP]\n");
System.out.println("-type scan type [full,stealth,fin,ack]\n");
System.out.println("-b bannerGrabber status\n");
}
}
argv is an entire array. What you are trying to match, is the entire content of the array with the string -h. Try doing this:
public static void main(String[] argv) throws Exception
{
System.out.println("1");
for(int i=0;i<argv.length;i++)
{
if (argv[i].equals("-h"))
{
System.out.println("-ip target ip address\n");
System.out.println("-t time interval between each scan in milliseconds\n");
System.out.println("-p protocol type [UDP/TCP/ICMP]\n");
System.out.println("-type scan type [full,stealth,fin,ack]\n");
System.out.println("-b bannerGrabber status\n");
}
}
}
Side Note: This previous SO post might also be worth going through.
You miss the array index in the if condition:
argv[i].equals("-h")
You are comparing an array with a string. Change it to:
if (argv[i].equals("-h"))
You try to compare String[] with String.
Try instead:
if (argv[i].equals("-h"))
Related
This code is supposed to count from an inputted parameter from a user, pass it to a function and it counts it down to one. I believe the program acts as it should, the problem is that it doesn't pass Mooc.fi's tests
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int number = Integer.valueOf(scanner.nextLine());
printFromNumbertoOne(number);
}
public static void printFromNumbertoOne(int number) {
for (int i = number; i > 0; i--) {
System.out.println(i);
}
The error I'm receiving is:
Method printFromNumberToOne(int) of class FromParameterToOne missing
What am I missing? And is there a way to check the test cases? Mooc.fi seems like it's very picky on what it takes for answers.
Thanks!
CAMELCASE of the method!
After beating my head against this problem all day it came to me after taking a break.
It should be printFromNumberToOne
I want to print the length of the first argument(args[0]) but getting ArrayOutOfBountException :
public class Main {
public static void main(String[] args){
args[0] = "Hello";
System.out.println(args[0].length());
}
}
Exception:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
at Main.main(Main.java:3)
When you write the code,
public class Main {
public static void main(String[] args){
args[0] = "Hello";
System.out.println(args[0].length());
}
}
At this point args[0]="Hello";, If your args a String array is not initialized then, while execute I'm supposed to think that you may have used the command in such a way java Main to execute your basic program.
Which cause the error, You have not passed any argument through command line so your String[] args is not initialized yet and it is not able to store your String "Hello" inside array args[0] and you are trying to print an empty array and throw the Exception
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
at Main.main(Main.java:3)
Update Answer:
Now Yes, You can use that to verify the String args length before print.
public class Main {
public static void main(String[] args){
if(args.length !=0){
System.out.println(args[0].length());
}else{
args = new String[1]; //Initilize first
args[0] = "Hello"; //Store value in array element
System.out.println(args[0].length()); //Print it.
}
}
}
First, check if there is an argument. Then print the length. It's not a good idea to change the values in the argument array either. Something like
if (args.length > 0) {
System.out.println(args[0].length);
} else {
System.out.println(0);
}
should do it.
Here array of String does not have any initialized objects and args has 0 element. That's why it is recommended to check whether does args have any element or not. Then, proceed further accordingly. This is how code looks like.
public class Main {
public static void main(String[] args){
if(args.length !=0){
// do something
}else{
// args doesn't have element.
return ;
}
}
}
You need to check first if an argument is even present.If there is no argument passed and you try to access any element, it will throw ArrayIndexOutOfBoundsException. Also, you should avoid assign any hardcoded value to the elements in array. The code to access 1st element can be something like below:-
if(args.length>0){
System.out.println(args[0].length());
}
I was wondering how I could check args.length within a method.
For example:
public static void commandLineCheck (int first, int second){
if (args.length==0){
//do something with first and second
}
}
public static void main(String[] args) {
int first = Integer.parseInt(args[0]);
int second = Integer.parseInt(args[1]);
commandLineCheck(first, second);
}
I get a "cannot find symbol: args" error when I do this. Right now, I'm thinking I need to pass args[] through the method as well. I've tried this but it then gives me an "" error. Is there a beginner-friendly solution to this?
EDIT: Thank you so much for the quick response guys! It worked!
Change your code like this (You need to pass the array's parameter to your check method)
public static void commandLineCheck (int first, int second, String[] args){
if (args.length==0){
//do something with first and second
}
}
public static void main(String[] args) {
int first = Integer.parseInt(args[0]);
int second = Integer.parseInt(args[1]);
commandLineCheck(first, second, args);
}
And it will work. However the following test (args.length==0)does not make much sense since you have already assumed that args.length is greater or equal to 2 by extracting two values from it inside the main method. Therefore when you get to your commandLineCheck method, this test will always be false.
You need to pass the String [] args to your commandLineCheck method. This is written the same way as you declare the array for your main method.
public static void commandLineCheck (String[] args){
if (args.length==0){
//do something with first and second
}
}
Also you probably want to change your main method and commandLineCheck method a bit.
public static void commandLineCheck(String [] args) {
/* make sure there are arguments, check that length >= 2*/
if (args.length >= 2){
//do something with first and second
int first = Integer.parseInt(args[0]);
int second = Integer.parseInt(args[1]);
}
}
public static void main(String[] args) {
commandLineCheck(args);
}
I am new at programming and currently in our classes we are learning java. I am trying to create a routine in which I need to use String variables only. Below it is the code in which I am working with:
public static void main(String[] args) throws java.io.IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System. in ));
PrintStream out = System.out;
String hair.equals("damagedHair");
cutHair(marvin);
cleanHair(michelle);
for (int i = 0; i < 2; i++) {
static void cutHair(String marvin) {
String cabello;
marvin.equals(hair);
if (marvin.equals("damagedHair")) {
cabello.equals("newHaircut");
result(hair);
}
static void cleanHair(String michelle) {
String hair;
michelle.equals(hair);
if (michelle.equals(newHaircut)) {
hair.equals("putShampooAndConditioner");
result(hair);
}
static void result(String pHair) {;
PrintStream out = System.out;
out.println("=============");
out.println(pHair);
out.println("=============");
}
}
Jcreator is giving me an error that says Illegal start of expression and also java 50 error ';' expected.
I am not sure why is this coming up and I am a little confused as to whether I am doing something I am not supposed to and how to correct it. Sorry about the double posting, this is the right message. Need some help from you guys to figure this out.
Thanks in advanced!
This is in your main:
for(int i=0; i<2; i++)
{
static void cutHair(String marvin)
{
String cabello;
marvin.equals(hair);
if(marvin.equals("damagedHair"))
{
cabello.equals("newHaircut");
result(hair);
}
}
You cannot define methods inside of main. Also, hair is not in scope here, ie it's in your main, not your method. Additionally, you're constantly only declaring variables, and then using them without them ever having been initialized. For example, in the above method, you have:
cabello.equals("newHairCut")
but cabello was never initialized, this should give you a might not have been initialized warning. Or earlier in your code, you have:
String hair.equals("damagedHair");
Again, this doesn't make any sense. You just declared hair here, you cannot call methods on it until you initialize it. I suggest that you review some tutorials.
The following simple Java program appears to display the string Hello World through the statement System.out.println("Hello World"); but it doesn't. It simply replaces this with another string which is in this case, Good Day !! and displays it on the console. The string Hello World is not displayed at all. Let's look at the following simple code snippet in Java.
package goodday;
import java.lang.reflect.Field;
final public class Main
{
public static void main(String[] args)
{
System.out.println("Hello World");
}
static
{
try
{
Field value = String.class.getDeclaredField("value");
value.setAccessible(true);
value.set("Hello World", value.get("Good Day !!"));
}
catch (Exception e)
{
throw new AssertionError(e);
}
}
}
Just one question about this code here. It works exactly as expected but I can not reduce the length of the string Good Day !!. If an attempt is made to do so, it causes a java.lang.ArrayIndexOutOfBoudsException. If the length is increased, the program runs well but the rest of the characters in the displaying string are truncated means that the length of the both of the strings should somewhat be the same. Why? This is the thing that I couldn't understand.
The value field is a char[] which internally stores the character array that the string uses as its backing store. Other fields indicate the initial offset into the character array, and the length of the string. (So to take a substring, it just creates a new String object which refers to the same char[] but with a different starting offset and length.)
If you modify those fields as well, you can do pretty much whatever you like with the string. Sample:
import java.lang.reflect.Field;
public class Test
{
// Obviously in real code you wouldn't use Exception like this...
// Although hacking string values with reflection is worse, of course.
public static void main(String[] args) throws Exception
{
System.out.println("Hello World");
replaceHelloWorld("Goodbye!");
System.out.println("Hello World");
replaceHelloWorld("Hello again!");
System.out.println("Hello World");
}
static void replaceHelloWorld(String text) throws Exception
{
// Note: would probably want to do hash as well...
copyField(text, "Hello World", "value");
copyField(text, "Hello World", "offset");
copyField(text, "Hello World", "count");
}
static void copyField(String source, String target, String name)
throws Exception
{
Field field = String.class.getDeclaredField(name);
field.setAccessible(true);
field.set(target, field.get(source));
}
}