0

Basically I have to prompt a user to enter 10 string values, and then in another loop print them in ascending order, then in a final loop, print them in descending order. My array is bringing back null, obviously because I am not prompting users to enter actual information into the array object. I am really stuck on this. I know I need to somehow reference the "userStrings[]" array in my first while loop. I keep researching and keep getting integer loops questions and For loops. This has to be a while loop. I just can no figure out how to get the userStrings[] to actually fill up when the user enters the values. How do I get it linked in the loop?

public class HomeWork10
{

    public static void main(String[] args)
    {
        String[] userStrings = new String[10];

        int count = 0;
        int count2 = 0;

        while (count < 10)
        {
            System.out.println("Please enter a string value ");

            Scanner input = new Scanner(System.in);
            String userInput = input.next();

            count++;
        }

        while (count2 < 1)
        {
            System.out.println(Arrays.asList(userStrings));
            count2++;
        }      
    }
}

3 Answers 3

1

You are not putting the values in the String[]

Do it like this:

 while (count < 10) {
    System.out.println("Please enter a string value ");

    Scanner input = new Scanner(System.in);
    String userInput = input.next();
    userStrings[count] = userInput;
    count++;
}

Also, declare Scanner input = new Scanner(System.in) outside your while() {...}

Sign up to request clarification or add additional context in comments.

2 Comments

OK, so that did it. thanks! I do have a question though. why do you call count here... userStrings[count] = userInput;. I thought maybe it counted and I could remove count++, but that made an infinite loop. I'm not sure the purpose of that, but I noticed if I removed it, the code does not compile.
Just read about Arrays and while loop for better understanding of both
1

See below code snippet may solve your problem.

package com.suresh;

import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;

public class HomeWork10 {

public static void main(String[] args) {
    String[] userStrings = new String[10];
    int count = 0;
    System.out.println("\t Reading Array Elements ");
    while (count < 10) {
        System.out.print("\t Please enter a string value : ");
        Scanner input = new Scanner(System.in);
        userStrings[count] = input.next();
        count++;
    }
    System.out.println("\t PRINTING ORIGINAL ARRAY OF ELEMENTS ");
    count = 0;
    while (count < userStrings.length) {
        System.out.println("\t " + userStrings[count]);
        count++;
    }

    Collections.sort(Arrays.asList(userStrings), new StringAscComparator());
    System.out.println("\t ASCENDING ORDER ");
    count = 0;
    while (count < userStrings.length) {
        System.out.println("\t " + userStrings[count]);
        count++;
    }

    System.out.println("\t DESCENDING ORDER ");
    Collections.sort(Arrays.asList(userStrings), new StringDescComparator());

    count = 0;
    while (count < userStrings.length) {
        System.out.println("\t " + userStrings[count]);
        count++;
    }
}

static class StringAscComparator implements Comparator<String> {

    @Override
    public int compare(String o1, String o2) {
        return o1.compareTo(o2);
    }
}

static class StringDescComparator implements Comparator<String> {

    @Override
    public int compare(String o1, String o2) {
        return o2.compareTo(o1);
    }
}

}

Comments

0

You created the array with 'String[] userStrings = new String[10];' and inside your while loop to access it you need to do something like this 'userStrings[0] = input.next()' This says the first item in the array userStrings will be set to input.next(). I'm not great at java so I'm not sure what input.next() will do though.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.