473,417 Members | 1,459 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,417 software developers and data experts.

Java command passing args, help?

Expand|Select|Wrap|Line Numbers
  1. public class Echo {
  2.     public static void main (String[] args) {
  3.         for (String s: args) {
  4.             System.out.println(s);
  5.         if (args[2] == "help"){
  6.             System.out.println("help");
  7.             }
  8.         }
  9.     }
  10. }
  11.  
im just simply testing to see if the first argument is equal to "help" your help is much appreciated.
Aug 2 '10 #1

✓ answered by Bret Little

Hi Jimmy,

Because a String is an object, not a primitive type, you need to use the equals() method instead of ==

In short, your code should be:
Expand|Select|Wrap|Line Numbers
  1. public class Echo {
  2.     public static void main (String[] args) {
  3.         for (String s: args) {
  4.             System.out.println(s);
  5.         if (args[2].equals("help")) {
  6.             System.out.println("help");
  7.             }
  8.         }
  9.     }
  10. }
  11.  

7 2145
Hi Jimmy,

Because a String is an object, not a primitive type, you need to use the equals() method instead of ==

In short, your code should be:
Expand|Select|Wrap|Line Numbers
  1. public class Echo {
  2.     public static void main (String[] args) {
  3.         for (String s: args) {
  4.             System.out.println(s);
  5.         if (args[2].equals("help")) {
  6.             System.out.println("help");
  7.             }
  8.         }
  9.     }
  10. }
  11.  
Aug 2 '10 #2
@Bret Little
thank you for the help and it means a lot because im trying to better myself in java hoping to get some kind of job in the field of but im only 16 so there are some year to go.
Aug 2 '10 #3
now i get an error
and got rid of the for statement
Expand|Select|Wrap|Line Numbers
  1. Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
  2.         at Echo.main(Echo.java:5)
  3.  
Aug 2 '10 #4
Jimmy,

The point of your for loop is to check each element of args to see if it is equal to "help". Args is an array of Strings that you pass into your program when you run it. It could have a length of 2,3, or 20. Because of this, you need to test if s.equals("help") rather than args[2].equals("help"). If you just test for args[2], then you are only checking for the 3rd element of the array, and in the case of the error you posted, it does not have three elements and therefore throws an exception.
Aug 2 '10 #5
@Bret Little
ok so i figured it but, for the long term. how do i suppress
a thrown exception in the console
Aug 2 '10 #6
Oralloy
988 Expert 512MB
Use a try/catch/finally block to catch exceptions.

Still, exceptions exist for a reason. Just suppressing them is usually a bad idea. Instead, your code should behave in a reasonable way.

If you think about it, the most reasonable behaviour is to tell you, the user/developer, exactly what happened, which is what your exception in post #5 does.
Aug 3 '10 #7
Thank you for all who helped me on this post but i gave up on java and started learning python.
Aug 11 '10 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Shawn | last post by:
I am getting the following error with a Java Applet being served out by IIS over HTTPS/SSL using a Verisign certificate: java.lang.NoClassDefFoundError: javax/help/HelpSetException at...
4
by: Laura P | last post by:
Hi, I wasn't sure whether this should be posted in the Java are or in a Solaris thread, so I shall post it in both. Sorry for the duplication. I am new to Solaris and am having trouble...
3
by: Michael | last post by:
What's the perfered method of passing command-line args throughout a Python program given that globals don't seem to really exist? I thought of writing them out to a python file and then importing...
2
by: Ernesto | last post by:
I'm trying to use Popen to do some command line work Windows XP style. I have devcon.exe which I would use on a Windows command line like so: devcon disable...
6
by: Edd Dawson | last post by:
Hi. I have a strange problem involving the passing of command line arguments to a C program I'm writing. I tried posting this in comp.programming yesterday but someone kindly suggested that I'd...
3
by: | last post by:
Hello, I am trying to get add a product to a cart from a gridview control when a button in the gridview is clicked. I have a book on how to do this in asp.net 2.0 but it is done by specifying...
2
by: paul | last post by:
I have a file type that is going to be associated with my visual basic application and i want the user to be able to double click on a file of said type and have it launch the program and load the...
3
by: Robert Brinson | last post by:
I'm writing one of my first WinForm apps; everything else has been WebForms up to this point. However, having come from a background of Java, C++, and Perl, I'm having trouble understanding how...
3
by: =?Utf-8?B?VGltIEE=?= | last post by:
I want the user to be able to select a group of files in windows explorer, right click, open with - my application and have my application put all of the file names onto the clipboard. It seems...
2
by: lilyumestar | last post by:
This project is due by Tuesday and I haven't even gotten half of it done. Can anyone please help me with this Exception error? I've been trying to figure it out for several hours Error Message ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.