boi yeet
boi yeet

Reputation: 86

How do I make the code compare 2 values in an array?

I have the following code:

Scanner sc=new Scanner(System.in);
    System.out.println("Type in your order(ex.5 7 4 6 8 3 9 2 0 1 - SPACES REQUIRED): ");
    String input=sc.nextLine();
    for(int i=0;i<input.length(); i++) {
    String[] b=input.split(" ");
    if(b[i] < b[i+1]) { // Condition of "int input"
        System.out.println("Acsending"); 
    }
    else {
            System.out.println("Mixed"); 
        }
    }

But it comes up as an error in the part where it says if(b[i] < b[i+1]), saying "The operator < is undefined for the argument type(s) java.lang.String, java.lang.String." What should I do?

Upvotes: 0

Views: 88

Answers (5)

Ajfasdjf &#246;sadfla
Ajfasdjf &#246;sadfla

Reputation: 21

Usually you use an index to access an array at the point i Then you only need to convert the 2 variables so they are compareable and there you go

Upvotes: 1

Arvind Kumar Avinash
Arvind Kumar Avinash

Reputation: 79435

Do it as follows:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Type in your order(ex.5 7 4 6 8 3 9 2 0 1 - SPACES REQUIRED): ");
        String input = sc.nextLine();
        String[] b = input.split(" ");
        int i;
        for (i = 0; i < b.length - 1; i++) {
            // Instead of Integer::parseInt, you can use Integer::valueOf
            // Check https://docs.oracle.com/javase/9/docs/api/java/lang/Integer.html for more details
            if (Integer.parseInt(b[i]) > Integer.parseInt(b[i + 1])) {
                System.out.println("Mixed");
                break;
            }
        }
        if (i == b.length - 1) {
            System.out.println("Ascending");
        }
    }
}

Sample run-1:

Type in your order(ex.5 7 4 6 8 3 9 2 0 1 - SPACES REQUIRED): 
5 7 4 6 8 3 9 2 0 1
Mixed

Sample run-2:

Type in your order(ex.5 7 4 6 8 3 9 2 0 1 - SPACES REQUIRED): 
1 2 3 4 5
Ascending

Alternatively, you can do it as follows:

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Type in your order(ex.5 7 4 6 8 3 9 2 0 1 - SPACES REQUIRED): ");
        String input = sc.nextLine();
        String[] b = input.split(" ");
        int i;
        for (i = 0; i < b.length - 1; i++) {
            if (b[i].compareTo(b[i + 1]) > 0) {
                System.out.println("Mixed");
                break;
            }
        }
        if (i == b.length - 1) {
            System.out.println("Ascending");
        }
    }
}

Sample run-1:

Type in your order(ex.5 7 4 6 8 3 9 2 0 1 - SPACES REQUIRED): 
5 7 4 6 8 3 9 2 0 1
Mixed

Sample run-2:

Type in your order(ex.5 7 4 6 8 3 9 2 0 1 - SPACES REQUIRED): 
1 2 3 4 5
Ascending

Upvotes: 0

HarryQ
HarryQ

Reputation: 1423

You are comparing two String, you need to first convert the string to integer before comparison.

 if(Integer.valueOf(b[i]) < Integer.valueOf(b[i+1]))

It was the solution top of my head, as pointed out by Arvind, Integer.parseInt(String s) returns the primitive int type, which is preferred in this case.

if(Integer.parseInt(b[i]) < Integer.parseInt(b[i+1]))

Upvotes: 0

robindust
robindust

Reputation: 16

As the compiler says you can not compare Strings like that. Convert your Strings to Integers using Integer.parseInt() and then compare.

Upvotes: 0

dentist229
dentist229

Reputation: 73

You should convert it to an integer first:

String sentence = "1234";
int number = Integer.parseInt(sentence);

Upvotes: 1

Related Questions