• 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 Arrays

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello all,
I am currently trying to write a loop that fills an array with 12 elements and then compares the length of that array with another array. If both arrays have equal length it prints them. If not an error code comes up. Here is my code

import java.util.*;

class myArrays
{

int allMonths;
String months;
String numbers;

int numberslength;



public void printArray(){

String[] months = new String[13];
months [1] = "January";
months [2] = "Feb";
months [3] = "Mar";
months [4] = "April";
months [5] = "May";
months [6] = "June";
months [7] = "July";
months [8] = "Aug";
months [9] = "Sept";

months [10] = "Oct";
months [11] = "Nov";
months [12] = "Dec";

///allMonths=months.length;
int array[] = new int[14];
int p=0;
//
// Arrays.fill(array, x);
// for (int x=0; x<array.length; x++) {
// Arrays.fill(array, p++);
//
// }
//
// //}
for(int i=0; i<months.length;i++){
Arrays.fill(array, p++);
if (array.length==months.length){
System.out.println("they are same length");
}
System.out.println(array[p]+"."+ months[i]);
}
 
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch.

Please use the code tag - makes it easier to read.

I'm not sure what your question is, but this code:


will never evaluate true, since array.length=14 and months.length=13.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic