0

I am new in java and have a problem writing my code. The problm is, im taking 10 strings from user and wants to print the largest one out all 10 strings.The Input Section works well, But there is an IDE error: "of bad operands for binary '>' operator" while using if condition . The code is below..

import java.util.Scanner;
import java.io.InputStream;

public class Largest_String {


    public static void main(String[] args) {


         Scanner input=new Scanner(System.in);
         System.out.println("Please Enter The 10 Strings: ");
        for(int i=1; i<=10; i++){


            System.out.print(i+") ");
            String str=input.nextLine();

            String array[]=new String[10];



            for(int j=10; j>0; j++){

            if(array[i]>array[j]){

            System.out.println("The Largest String if"+ array[i]);
            }

            }
        }
    }
}
skaffman
  • 398,947
  • 96
  • 818
  • 769
Farhan
  • 25
  • 2
  • 5

6 Answers6

2
array[i].length()>array[j].length()

you need to compare string length and not the string itself.

Jerome Cance
  • 8,103
  • 12
  • 53
  • 106
1

The arrays that are subject to the '>' operator are of type String, so this is not a valid operator.

If you want to confirm the longest string (number of characters) then you should perform the comparison on the length of the strings as follows:

if(array[i].length() >array[j].length() ){
rhinds
  • 9,976
  • 13
  • 68
  • 111
  • Thankx a lot guys, i have done it. I have learned new and useful things from you all. Thank you all once again. – Farhan Feb 29 '12 at 13:04
1

You can use lenght() of individual string in array. This is the method that returns the length of given string in integer type. You are using the > operator for strings, use it for integers i.e., length of each string.

1

here is a slightly different way of doing it:

import java.util.Scanner;
import java.io.InputStream;

public class Largest_String {
    public static void main(String[] args) {
         Scanner input=new Scanner(System.in);
         System.out.println("Please Enter The 10 Strings: ");

         String longest = "";

         for(int i=0; i<10; i++){
            System.out.print("Enter String "+(i+1)+": ");
            String str=input.nextLine();
            if( str.length() > longest.length() )
            {
               longest = str;
            }
        }
        System.out.println("The Largest String is "+ longest);
     }
 }

comment: get used to doing your loops 0-based

mindandmedia
  • 6,800
  • 1
  • 24
  • 33
  • 1
    Its not ideal to paste code on homework-tagged question, you can answer with ideas. – sgowd Feb 29 '12 at 11:58
  • pardon me, i shall refrain in the future – mindandmedia Feb 29 '12 at 11:59
  • Thanks a lot Sir was near about to done it, actually im new and don't know the syntax of java properly. But you people helped me out and i have done it. – Farhan Feb 29 '12 at 13:11
  • you should accept an answer as the correct one. if none of them are correct, post the correct one yourself and accept that one. good luck to you! – mindandmedia Feb 29 '12 at 15:31
1
  1. Use array[i].length>array[j].length to compare the lengths of the strings.

  2. for(int j=10; j>0; j++): this will give you an IndexOutOfBoundsException when j is greater than elements in array

  3. The idea:

    1. read in user input
    2. check if this input is longer than the previously entered value (use 0 as initial value)
hage
  • 5,966
  • 3
  • 32
  • 42
1

You need to take a good long look at your loop nesting. It looks something like this:

for (/* i */) {
    String array[] = new String[10];
    for (/* j */) {
        /* ... */
    }
}

The end result of this nesting is that you're creating ten arrays, each with ten elements. Move the array outside the outer for loop, and make sure you know why.

Next thing, focus on the loop indexes and how you use them to index into the arrays:

for (int i = 1; i <= 10; i++) {
    String array[] = new String[10];
    for (int j = 10; j > 0; j++) {
        if (array[i] > array[j]) {
        }
    }
}

An array declared as new String[10] has valid indices: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9. You're accessing array[10] in this loop, which will blow up with an out-of-range exception. (You are also incrementing with j++, but intended to start at the end of the array and count down. Oops.)

When iterating over an array, it is best to stick to this formula:

for (int i = 0; i < array.length; i++)

If you want to count down, you can, but it is more tricky:

for (int i = array.length - 1; i >= 0; i--)

(Again, n-1 to 0, inclusive.) This question shows two additional forms that also work for iterating backwards:

for (int i = array.length; --i >= 0; )
for (int i = array.length; i-- > 0; )

Though I dislike for loops that combine the test and iterative step in one go, I can definitely respect that both these get the job done correctly and are likely to be idiomatic.

Iterating backwards is rare.

Community
  • 1
  • 1
sarnold
  • 102,305
  • 22
  • 181
  • 238
  • Great Sir, for your kind reply. I am much pleased of getting a useful knowledge from your golden words. Thanks a lot. – Farhan Feb 29 '12 at 13:07