• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

a way to get a name from an array

 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello guys, i tried to write a code which returns the longest name in an array but i have 1 problem:
1.how do i set the "max" value that the compiler will read it and won't say " variable max might not have been initialized "?




Thank you !
 
Sheriff
Posts: 24635
56
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you know what it means to "initialize" a variable?
 
rian bron
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:Do you know what it means to "initialize" a variable?


yes, but i want that my loop result will initialize its original value

lest say :
int x=0;
int y=10
for( int j=0;j<=y;j++)
    {
          x++;
    }

will i get here x=10?

int x=0;
int y=10
int returnX()
   {
for( int j=0;j<=y;j++)
    {
          x++;
    }
return x;
}

will i get here x=10?

Thank you
 
Ranch Hand
Posts: 186
1
Netbeans IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, you are close

My suggestions for you is that you are going to be going through the array in linear time (sequentially). What I suggest is that you initialize the first value of the array to be the largest item prior to iterating through the array. Once you iterate or loop through the array, check every value against the max value and if it is larger than the max value, then that value would become the new max value. Good luck!
 
rubbery bacon. rubbery tiny ad:
professionally read, modify and write PDF files from Java
https://products.aspose.com/pdf/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!