-2
import java.util.Scanner;

public class userInputTest {

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        for(int i = 0; i>-1; i=i+1){

            String[] giveMeAString = new String[i+1];
            String x = sc.next();

            giveMeAString[i]=x;

            if(i>=0){
                if (giveMeAString[i].length() < giveMeAString[i-1].length()){
                    System.out.println("The string is shorter than previous string!");
                    break;}
            }

        }

    }   
}

I want to loop until present user input length is less than previous user input length.

I haven't solved it for a while after many attempta.

what am I doing wrong? I want to use Array, because I also want to print the user inputs out later on.

The only problem I have now is to make the written code work.

c0der
  • 15,550
  • 6
  • 26
  • 53
adevapp
  • 1
  • 1
  • 1
  • 4

2 Answers2

0

Couple of things to note :

  1. you are resetting your array every time, as the declaration (memory allocation) is within 'for' loop
  2. array size of (i+1) will allocate lot of memory when you are on higher number of attempts
  3. giveMeAString[i-1] : i = 1 - this should be your starting point of for(). Otherwise, you will get arrayIndexOutOfBound exception since you will be accessing giveMeAString[-1] when i = 0. This is non-existent array location/element-index.
  4. If you want to compare 2 consecutive string lengths, you need to make sure that on first iteration of the loop, you at least have 2 strings populated (or check if you are on first iteration and then continue the loop to add second string without going further).

Corrected code is :

Scanner sc = new Scanner(System.in);

        //arrays are alaways static memory allocation constructs. First input should always be the array size. If this is not known
        //ahead of time, use List, ArrayList etc.
        int a;
        System.out.println("Enter Array size in number: ");
        a = sc.nextInt();
        String[] giveMeAString = new String[a];
        String x;

        //updaing the loop initialization and condition
        for(int i = 1; i>0; i=i+1){

            //should NOT be creating array every time. Strings are immutable and hence costly w.r.t. memory/responseTime.
            //Besides, i didn't understand the logic of creating array of String of size (i+1).
            //instead, consider using resizable list (ArrayList etc). Below line is resetting your previously input values in giveMeAString[]
            //String[] giveMeAString = new String[i+1];


            if (i == 1) {
                //adding first input to array's first index. Without this, we would get NPE. One time execution.
                x = sc.next();
                giveMeAString[i - 1] = x;
            }

            //adding next input to array's next index
            x = sc.next();
            giveMeAString[i]= x;

            if(i>=0){
                if (giveMeAString[i].length() < giveMeAString[i-1].length()){
                    System.out.println("The string is shorter than previous string!");
                    break;
                }
            }

        }
DevdattaK
  • 188
  • 9
  • this solves alot. but its still not what im looking for. Becuase Array size isnt supposed to be known. it should be as much as is required (until String is shorter than the previous String). that means that i have to change the size of the Array after every input. but i dont know how its done, hence the reason i tried putting it in a for loop. even though it wasnt so applicable. – adevapp Sep 07 '16 at 02:27
  • ok..so can you take array of size 2? It seems like you always compare only 2 elements in one go. So, your solution could be (though it has more data copying and more running time): populate two elements of the array and do comparison as usual. If you decide to go to next iteration (first < second), then copy second into first index of array and put the newly accepted value in 'second' and do comparison again. – DevdattaK Sep 09 '16 at 03:26
0

Similar to previous answer, but using an ArrayList implementation, if the array size should not be constrained.

    Scanner scn = new Scanner( System.in );
    List<String> strList = new ArrayList<>();

    /*
     * First clase to allow logic receive at least 1 input without performing the check.
     * Next clause to check if current input length is greater than previous input length, continue receiving input
     */
    while ( strList.size() < 2 || strList.get( strList.size() - 1  ).length() 
            >= strList.get(strList.size() - 2 ).length() )
    {
        System.out.println(" Enter new String: " );
        strList.add( scn.next() );
    }

    System.out.println( "Previous String is shorter." );
    scn.close();

    System.out.println(Arrays.toString( strList.toArray() ));

Update, Using array implementation.

Scanner scn = new Scanner( System.in );
String [] strArray = new String[0];

/*
 * First clause to allow logic receive at least 1 input without
 * performing the check. Next clause to check if current input length is
 * greater than previous input length, continue receiving input
 */
while ( strArray.length < 2
    || strArray[strArray.length - 1].length() >= strArray[strArray.length -2].length() )
{
    String[] tempArr = new String[strArray.length + 1];
    for(int i = 0; i < strArray.length; i++){
        tempArr[i] = strArray[i];
    }
    strArray = tempArr;
    System.out.println( " Enter new String: " );
    strArray[strArray.length - 1] = scn.next();
}

System.out.println( "Previous String is shorter." );
scn.close();

System.out.println( Arrays.toString( strArray ) );
Samuel Kok
  • 575
  • 8
  • 16
  • Do write your own implementation if you seek an array implementation as I'm pretty sure that's covered in most basic programming courses. Or raise it as another question if need be. – Samuel Kok Sep 07 '16 at 02:31
  • hi, thnx for answering. but is there no possible way to solve it by using String[] giveMeAString = new String[];. i want to keep it as simple as possible. and i havn't learned about arrayList. so i dont Think its neccensary for this home exercise. i basically want to run a for loop and write a String and add that to an Array of string so that i can call it later in system.out.println. i must minimum write 2 strings, so that i can check which one is shorter. the last written one isnt aloud to be shorter than the previous one. because then it should break;. – adevapp Sep 07 '16 at 02:51
  • do a google search, lots of post out there with [response](http://stackoverflow.com/questions/12524318/how-to-increase-the-size-of-an-array-in-java) to that question. – Samuel Kok Sep 07 '16 at 02:54
  • i honestly dont Think i am supposed to use arrayList, because we havnt gone trough that yet in our school. – adevapp Sep 07 '16 at 02:57
  • if you've checked the link, you should have found the below response that fits your needs. http://stackoverflow.com/a/19620061/6785649 – Samuel Kok Sep 07 '16 at 02:59