aspose file tools*
The moose likes Beginning Java and the fly likes From 60 lines to 1 line? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "From 60 lines to 1 line?" Watch "From 60 lines to 1 line?" New topic
Author

From 60 lines to 1 line?

Matt Wilde
Ranch Hand

Joined: Feb 27, 2012
Posts: 34
How to reduce this? I don't want exact answers just a tip. Create an array and scan through it? Use the "?" ternary operator in some way? I'm stumped....


Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60072
    
  65

Map


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Koen Aerts
Ranch Hand

Joined: Feb 07, 2012
Posts: 344

Or depending on what variable "i" stands for, a Collection or even a simple int[] ...
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Matt Wilde wrote:How to reduce this? I don't want exact answers just a tip. Create an array and scan through it? Use the "?" ternary operator in some way? I'm stumped....







Is there some mathematical relationship between the left hand side and the right hand side? If you weren't using Java, is there some equation or set of rules you can use to describe how to get from one to the other without listing them all out?

If so, then you can figure out how to express the equation or rules in Java, and you'll probably end up with something significantly more compact.

If not, then you'll still have to explicitly describe each mapping, but there are ways you can do it that are more compact than that.

If the input side is in the range 0..N with no holes or few holes, you could use an array. If there are a lot of holes, you could use a Map.

Orthogonally to the above, if the mappings will rarely or never change, you can hardcode the values. Otherwise, you can read them from a config file.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18136
    
  39


I once converted a large group of if/then/else statements to one expression loaded with ternary operators.

I really liked it. I understood it perfectly. Everyone else hated it...... and years later. I liked it a lot less.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4244
    
    7

There's also a switch statement, which would be an improvement. But my first choice would be an algorithmic approach like Jeff suggests, or an appropriate data structure if that's not possible.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7081
    
  16

Matt Wilde wrote:Create an array and scan through it?

Sounds reasonable; although from your if statement, I don't think you even need to scan.
Use the "?" ternary operator in some way? I'm stumped....

UGH...although Henry says he's tried it.

None of the them, apart from a pre-initialized version of Bear's suggestion, will do it in 1 line though.

But, while we're in the realm of the absurd,
return i < 20 ? i + Integer.parseInt("722150849790...".substring(4*i, 4)) : -1;
would.

Winston



Isn't it funny how there's always time and money enough to do it WRONG?
Artlicles by Winston can be found here
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19556
    
  16

Since the indexes are incremental I'd go with a simple array combined with a ternary operator for the "else" clause, when the index is not valid for the array. Unless there is some logic between these numbers and indexes we don't know about of course, in which case I'd use that logic.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Matt Wilde
Ranch Hand

Joined: Feb 27, 2012
Posts: 34
@Bear: I keep hearing "Map" but hardly understand the extent of that class in Java. I'll look into it.
@Koen: makes sense to me...I end up doing something like that. Still not sure if it is the best...
@Jeff: As far as I know I cannot see any realtionship between them. As I said...I don't understand the "mapping". I end up using an array. Config file?
@Henry: I asked my professor about the ternary operators, its possible but he said thats not the best way.
@Matthew: Switch statement? Data structure? sounds useful I will look into it.
@Winston: Brilliant! didn't think of that. I will try that. Obviously I need to learn more about Mapping....
@Rob: Ya don't think there is a pattern to any of it...And yes I did an array. I think it works.

I did:

int[] a = {7221,5084,9790,7601,....}
i = (i>=0&&i<=19) ? a[i] : -1;


Good?
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7081
    
  16

Matt Wilde wrote:Brilliant! didn't think of that. I will try that...

Oh God, please don't. It was meant as a joke.

I did:
int[] a = {7221,5084,9790,7601,....}
i = (i>=0&&i<=19) ? a[i] : -1;
Good?

Two lines as far as I can make out. You could do it that way in one though. My suggestion (if you absolutely feel you have to do this) would be
return i = i < 0 || i > 19 ? -1 : [whatever horrible construct gets you what you want];

Winston
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19556
    
  16

I'd fix it just a little:
By checking against the length of the array, you can add or remove values without breaking your code.

And I'd make the array a private static final field, so you don't need to recreate it for each method call.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Matt Wilde wrote:
@Jeff: As far as I know I cannot see any realtionship between them. As I said...I don't understand the "mapping". I end up using an array. Config file?


If the mappings change more frequently than you're willing to recompile, you could put them in a text file, one mapping pair per line, and then your app would read the file at startup, or however often it needed to.
Matt Wilde
Ranch Hand

Joined: Feb 27, 2012
Posts: 34
Jeff Verdegan wrote:
If the mappings change more frequently than you're willing to recompile, you could put them in a text file, one mapping pair per line, and then your app would read the file at startup, or however often it needed to.


So put in a Scanner/Formatter type thing and create a new .txt file that holds all those values and read through the values checking/returning ints?
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7081
    
  16

Matt Wilde wrote:So put in a Scanner/Formatter type thing and create a new .txt file that holds all those values and read through the values checking/returning ints?

My suggestion would be to turn that file into the array we've all been talking about and store it somewhere; unless you expect it to change while your program is running.

I have to ask: Is this a homework question, or just something you thought up?

Winston
Matt Wilde
Ranch Hand

Joined: Feb 27, 2012
Posts: 34
@ Winston: Ya that makes more sense now! And yes it is. Started at 250 lines we need it under 80 for an A which I already have it under that. I just want to get it smaller. Thus, I am not asking for explicit answers only suggestions. Thank you!
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7081
    
  16

Matt Wilde wrote:Thank you!

You're welcome. I hope you noted Rob's last post (which I gave a point).

Winston
Matt Wilde
Ranch Hand

Joined: Feb 27, 2012
Posts: 34
@Rob: thanks didn't see that.
@Winston: Yes I did and I changed it. I also noticed the sarcasm for the parseInt method ha thanks though!

Thanks for the help guys!
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Rob Spoor wrote:I'd fix it just a little:
By checking against the length of the array, you can add or remove values without breaking your code.

And I'd make the array a private static final field, so you don't need to recreate it for each method call.


Hmmm....

If we make it a String literal, then we it will be created once, at class load time.
7221 is U+1C35, LEPCHA CONSONANT SIGN KANG, ᰵ
5084 is U+13DC, CHEKROKEE LETTER DLA, Ꮬ
...


@OP: It's a joke. Please do not try this at home.
Matt Wilde
Ranch Hand

Joined: Feb 27, 2012
Posts: 34
well this is what my whole class looks like in the end. From 250 lines to 17. Tied for first in the class. However Unorthodox my method is...it works! haha

Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Matt Wilde wrote:well this is what my whole class looks like in the end. From 250 lines to 17. Tied for first in the class. However Unorthodox my method is...it works! haha


I thought you were going to go the array route. That would definitely be less code, and easier to read.

Also, if you're really going to use the ternary operator (And why would you?) then there's no need for the i < 0 || i > 20 test. Just make that the final "default/else" value.


But seriously, ick.
Matt Wilde
Ranch Hand

Joined: Feb 27, 2012
Posts: 34
Jeff Verdegan wrote:

I thought you were going to go the array route. That would definitely be less code, and easier to read.


how would i do that in one line? the rules are we cannot have Statements on the same line after an opening brace such as if (i == 0){ int j = 4;
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18136
    
  39

Matt Wilde wrote:
Jeff Verdegan wrote:

I thought you were going to go the array route. That would definitely be less code, and easier to read.


how would i do that in one line? the rules are we cannot have Statements on the same line after an opening brace such as if (i == 0){ int j = 4;


Is that the whole goal? It has to be one line? Over actual code size and readability? Well... okay....



Henry
Matt Wilde
Ranch Hand

Joined: Feb 27, 2012
Posts: 34
Duh... didn't think of that.
That's why I'm in beginner programming
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Matt Wilde wrote:
Jeff Verdegan wrote:

I thought you were going to go the array route. That would definitely be less code, and easier to read.


how would i do that in one line? the rules are we cannot have Statements on the same line after an opening brace such as if (i == 0){ int j = 4;


Are you really required to do it in one line?

Will you really get a better grade if you do it in one horribly unreadable line than in 2 or 3 lines that are easier to read and understand and maintain? If so, go for it.
 
jQuery in Action, 2nd edition
 
subject: From 60 lines to 1 line?
 
Similar Threads
Checking for No Value
Trouble with regular expressions
How to display one image per day from an array?
Creating Multiple Objects?
selectedIndex in a Select Box error