Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-23-2008, 10:35 AM
Member
 
Join Date: Jun 2008
Location: Australia
Posts: 24
javanewbie is on a distinguished road
Make Java codes more simplier (Multidimensional Arrays)
Hello everyone.

Right here, I have my codes used in JC.

I'm trying to figure out if there's a possible way I could reduce the length/size of my codes, since I'm kinda new in programming in Java.

Here it is.


public class MArrays {

/**
Function Name: Main
Description: This program is designed to get the inputs of the
user and let the program multiply the numbers form
the lowest number up to the highest number, using
two arguments.
Arguments: String args[]
Return Type: none

*/


public static void main(String args[]) {

if(args.length > 2) {

System.out.println("Invalid Arguments");

} else if(args.length < 2) {

System.out.println("Invalid Arguments");

} else if(args.length == 2) {

System.out.println(args[0] + " " + args[1] + "\n");

int iArray1 = Integer.parseInt(args[0]);
int iArray2 = Integer.parseInt(args[1]);

int i1stIndex = iArray1 + 1;
int i2ndIndex = iArray2 + 1;

int aiNumbers[][] = new int[i1stIndex][i2ndIndex];

if(args[0].equals("0") || args[1].equals("0")) {
System.out.println("0");
} else {
for(int iCounter=1;iCounter < aiNumbers.length;
iCounter++) {

for(int iCounter2=1;iCounter2 <
aiNumbers[iCounter].length; iCounter2++) {

aiNumbers[iCounter][iCounter2] =
iCounter * iCounter2;
}
}


for(int iCounter=1;iCounter < aiNumbers.length;
iCounter++) {

for(int iCounter2=1;iCounter2 <
aiNumbers[iCounter].length; iCounter2++) {

System.out.print(aiNumbers[iCounter]
[iCounter2] + " ");
}
System.out.println("");
}
}
}
System.exit(1);
}
}


I'm trying to lessen my codes but I dont where should I start. Usage of multidimensional arrays should be intact and not to be removed.



Would somebody help me? Thanks.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 06-23-2008, 11:15 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,566
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Why don't you use something like this.

Code:
if(args.length != 2) { System.out.println("Invalid"); } else { // Do the processing }
You can remove around four lines from your code.

And also, here in your code you just handling commandline arguments. User can't dynamically change values.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 06-23-2008, 12:52 PM
Niveditha's Avatar
Senior Member
 
Join Date: May 2008
Posts: 299
Niveditha is on a distinguished road
Send a message via Skype™ to Niveditha
Yes i do agree with eranga.

the 2 other things what i felt can be reduced are:

Instead of using iArray1,iArray2,i1stIndex,i2ndIndex that consumes memory
"int aiNumbers[][] = new int[i1stIndex][i2ndIndex];" can be written as
int aiNumbers[][] = new int[Integer.parseInt(args[0]) + 1][Integer.parseInt(args[1]) +1];

Cant the for loops be combined instaed of using 2 for loops one for doing the logic and another for sysout's.

Both of these will help u to reduce ur code lines.
__________________
To finish sooner, take your own time....
Nivedithaaaa
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 06-23-2008, 01:27 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,566
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Ya, that's a good tip. Each additional declaration consume memory. So you can avoid as Niveditha says.

And also you no need to use separate for loops for calculation and print the result. You can do it within the same loop. So here is all the changes until now you have.

Code:
public class MArrays { /** Function Name: Main Description: This program is designed to get the inputs of the user and let the program multiply the numbers form the lowest number up to the highest number, using two arguments. Arguments: String args[] Return Type: none */ public static void main(String args[]) { if(args.length != 2) { System.out.println("Invalid Arguments"); } else { System.out.println(args[0] + " " + args[1] + "\n"); int aiNumbers[][] = new int[Integer.parseInt(args[0])+ 1] [Integer.parseInt(args[1]) + 1]; if(args[0].equals("0") || args[1].equals("0")) { System.out.println("0"); } else { for(int iCounter=1;iCounter < aiNumbers.length; iCounter++) { for(int iCounter2=1;iCounter2 < aiNumbers[iCounter].length; iCounter2++) { aiNumbers[iCounter][iCounter2] = iCounter * iCounter2; System.out.print(aiNumbers[iCounter][iCounter2] + " "); } System.out.println(""); } } } //System.exit(1); } }
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 06-23-2008, 01:31 PM
Member
 
Join Date: Jun 2008
Location: Australia
Posts: 24
javanewbie is on a distinguished road
Geee. Thanks a lot. Such a big help. ^^
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 06-23-2008, 01:38 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,566
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Help is on your way. Best thing is learn something from it.

Still you can reduce a lot within your code.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 06-23-2008, 01:58 PM
Member
 
Join Date: Jun 2008
Location: Australia
Posts: 24
javanewbie is on a distinguished road
I was thinking how can I make statements out of those 'revised' codes. I was thinking about the 'algorithm' used.

Would someone tell me the algorithm in a statement form? It would be a great help for my documentation in this program. Thanks.
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 06-23-2008, 02:22 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,566
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Just think about the logic in the code use. That's the algorithm(actually this is not a ordinary algorithm) here used. Not much difficult at all.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 06-24-2008, 05:02 PM
Member
 
Join Date: Jun 2008
Location: Australia
Posts: 24
javanewbie is on a distinguished road
Still. I can't think of any algorithm statements here. ='(
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 06-25-2008, 06:48 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 4,566
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
There is no algorithm there, just a simple logic. The logic is what you have done in your code. It should know yourself.

Code:
Check the number of command line parameters. If 2 command line parameters not found, notify it to the user, Else display command line parameters. Convert parameters to integer values and add one to each. Set the array size equal to calculated values. If any parameter is zero, display the value zero. Else, for each array element add values of multiplication of each index. Exit.
This is the very simple logic in the code.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

Has someone helped you? Then you can
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
their helpful post.

Want to make your IDE the best?
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to get error codes using java program kasipandian Web Frameworks 10 05-25-2008 07:00 PM
Arrays Problem (Advanced Java...Need Help) Zebra New To Java 9 05-02-2008 03:26 AM
Help needed with java arrays code d24706 New To Java 2 03-07-2008 03:11 AM
Multidimensional arrays Java Tip Java Tips 0 11-05-2007 07:07 PM
Arrays in Java hiranya New To Java 3 07-30-2007 11:10 AM


All times are GMT +3. The time now is 12:10 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org