• 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

comparing 2 arrays elements index against index

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys, id just want to know if there is a need for a 3rd for loop in my program with variable index in order to compare 2 arrays elements index against index or do i need to change one of the loops ive already got. I kinda think i need a third loop but i just wanted to make sure. Im trying to create a lotteryprogram where the first array lotteryNumbers is supposed to randomly generate new numbers and the other array userNumbers is for user input. Then i need to compare if the numbers match in order for user to win, if not they loose. So i do know i need an if else statement later on but i just wanna know about this comparision. Id be thankful!

This is my code so far :


 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First, you don't need to assign elements of a newly created numeric array to zero; they already have that value by default.

In a single loop, you can assign the current index of lotteryNumbers to a random int and test the equality of that with an int obtained from user input, thus eliminating the need for the userNumbers array. You can break out of the loop when a mismatch is detected.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Patricia Andersen wrote:Hi guys, id just want to know if there is a need for a 3rd for loop in my program with variable index in order to compare 2 arrays elements index against index or do i need to change one of the loops ive already got.


I'd say the first, because it's more flexible.

However, I really think you're hamstringing yourself by putting eveything in main(). It's really not a good habit to get into. For an alternative approach have a look at my MainIsAPain page.

This is the second or third thread that you've posted, and I don't get any sense that you've heeded previous advice to break problems down into small chunks and methods. Your main() method still looks like a stream of consciousness - and believe me, it's a bad way to program.

Even if you don't use the methodology suggested in the above link, at least read the StopCoding (←click→) and WhatNotHow pages.

HIH

Winston
 
Patricia Andersen
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Darryl Burke wrote:First, you don't need to assign elements of a newly created numeric array to zero; they already have that value by default.

In a single loop, you can assign the current index of lotteryNumbers to a random int and test the equality of that with an int obtained from user input, thus eliminating the need for the userNumbers array. You can break out of the loop when a mismatch is detected.




Thanks a lot !!!
 
Patricia Andersen
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:

Patricia Andersen wrote:Hi guys, id just want to know if there is a need for a 3rd for loop in my program with variable index in order to compare 2 arrays elements index against index or do i need to change one of the loops ive already got.


I'd say the first, because it's more flexible.

However, I really think you're hamstringing yourself by putting eveything in main(). It's really not a good habit to get into. For an alternative approach have a look at my MainIsAPain page.

This is the second or third thread that you've posted, and I don't get any sense that you've heeded previous advice to break problems down into small chunks and methods. Your main() method still looks like a stream of consciousness - and believe me, it's a bad way to program.

Even if you don't use the methodology suggested in the above link, at least read the StopCoding (←click→) and WhatNotHow pages.

HIH

Winston




All i asked for is if there is a need for a 3rd loop. And i am trying to break the problem down into small chunks which is why i wonder if there is a need for a 3rd loop.
 
Patricia Andersen
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Patricia Andersen wrote:

Winston Gutkowski wrote:

Patricia Andersen wrote:Hi guys, id just want to know if there is a need for a 3rd for loop in my program with variable index in order to compare 2 arrays elements index against index or do i need to change one of the loops ive already got.


I'd say the first, because it's more flexible.

However, I really think you're hamstringing yourself by putting eveything in main(). It's really not a good habit to get into. For an alternative approach have a look at my MainIsAPain page.

This is the second or third thread that you've posted, and I don't get any sense that you've heeded previous advice to break problems down into small chunks and methods. Your main() method still looks like a stream of consciousness - and believe me, it's a bad way to program.

Even if you don't use the methodology suggested in the above link, at least read the StopCoding (←click→) and WhatNotHow pages.

HIH

Winston






thanks for advice though

 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Patricia Andersen wrote:All i asked for is if there is a need for a 3rd loop. And i am trying to break the problem down into small chunks which is why i wonder if there is a need for a 3rd loop.


But:
  • You still have only one method, and it's static.
  • You're thinking about things like loops, which is the how, when what you should be concentrating on is understanding what needs to be done.

  • If you just want answers, you're probably better off with a site like Stackoverflow. Here, we try to help you.

    Winston
     
    Patricia Andersen
    Ranch Hand
    Posts: 37
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Winston Gutkowski wrote:

    Patricia Andersen wrote:All i asked for is if there is a need for a 3rd loop. And i am trying to break the problem down into small chunks which is why i wonder if there is a need for a 3rd loop.


    But:
  • You still have only one method, and it's static.
  • You're thinking about things like loops, which is the how, when what you should be concentrating on is understanding what needs to be done.

  • If you just want answers, you're probably better off with a site like Stackoverflow. Here, we try to help you.

    Winston



    Ok you are right..but still even if i try to break it down into small pieces i still need to know the syntax and what should be used. Anyhow, i appriciate your help
     
    reply
      Bookmark Topic Watch Topic
    • New Topic