Wednesday, 15 January 2014

java - User input determining the length of an array -



java - User input determining the length of an array -

i trying create code, take input user in list1, , depending on how long create list1, determine length of list2. phone call method , compare 2 strings , see if identical or not.

i know code limits both list1 , list2 6 ints, because don't know how other way yet, , wondering if guys can help?

//a programme prompts user come in 2 list of integers //and displays whether 2 identical import java.util.scanner; import java.util.arrays; public class identicalarrays { public static void main(string[] args) { //initialize scanner scanner input = new scanner(system.in); //start loop while (true) { //arrays int[] list1 = new int[6]; int[] list2 = new int[6]; //user input system.out.println("enter list1: "); (int = 0; < list1.length; i++) list1[i] = input.nextint(); system.out.println("enter list2: "); (int = 0; < list2.length; i++) list2[i] = input.nextint(); //call method if (equals(list1, list2)) system.out.println("two lists identical"); else system.out.println("two list not identical"); //continuation of loop system.out.println("do want go on (y/n)?"); string s = input.nextline(); s = input.nextline(); s = s.tolowercase(); if (s.charat(0) == 'n') break; } } public static boolean equals(int[] list1, int[] list2) { if (list1.length == list2.length) { arrays.sort(list1); arrays.sort(list2); } else homecoming false; (int = 0; < list1.length; i++) { if (list1[i] != list2[i]) homecoming false; } homecoming true; } }

you have couple of choices. one, inquire user front end how many items want come in , utilize reply set both array lengths start. another, utilize dynamic construction list take data, , create array list , proceed before. or create big array , hope user gets bored before end of it. (this not right way...)

however, don't need sec list, since can compare each input user appropriate entry in initial input set.

java arrays

No comments:

Post a Comment