• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

A Small Automation Needed

 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,
To print the name 1000 times in every year , I used the below logic.
Just wondering if anybody could suggest a better and a much simpler way.
Is there a way I can automate not to have so many "if" statements?

Thanks for your help,
Divya
[ February 04, 2004: Message edited by: divya madala ]
[ February 04, 2004: Message edited by: divya madala ]
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, you can often replace a switch over small integers with a lookup in an array. Put the strings into an array at the correct indices, and then just use one println, using "Name" as the index. Note that either the array will need a dummy entry at element 0, or you'll need to subtract 1 from "Name" to get the index to use.
No idea what on earth the point of this program is!
 
Ranch Hand
Posts: 456
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Hey divya,
1) you can replace the innermost loop with
System.out.println(Name1);
...
System.out.println(Name5);
which has absolutely the same output then your version, but saves a whole loop.
2) As a suggestion: it is a better coding style if you reserve words starting with a capital for class names. Your code gets more understandable if you name your variables like "seq" or "name".

Greets,
Jan
 
Ranch Hand
Posts: 382
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by divya madala:
Hi All,
To print the name 1000 times in every year , I used the below logic.
Just wondering if anybody could suggest a better and a much simpler way.
Is there a way I can automate not to have so many "if" statements?

Thanks for your help,
Divya
[ February 04, 2004: Message edited by: divya madala ]
[ February 04, 2004: Message edited by: divya madala ]


Use a String array to store the names & then loop thru the array. Just remember that arrays start with 0 subscript. BTW, what the heck is this program doing? And why? Is this just an example/tutorial/assignment or is this for some real-world project?
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic