• Post Reply Bookmark Topic Watch Topic
  • New Topic

Comparing Arrays  RSS feed

 
Adam Kjems
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • 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]);
}
 
Ådne Brunborg
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!