Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Sort String Method

  1. #1
    Junior Member
    Join Date
    Aug 2014
    Posts
    2
    My Mood
    Mellow
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Sort String Method

    /*
    So I have this code and I am getting incombatable types, I do not know why I am getting them... Can someone help me figure out why I am getting the error? (See: //Where the problem is)


    The Error I am getting:
    stringSort.java:26: error: incompatible types
    if(myArray[j].compareToIgnoreCase(myArray[i].toString())){
    ^
    required: boolean
    found: int
    */




    import java.util.*;
    import java.io.FileInputStream;
    import java.io.InputStreamReader;
    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.FileNotFoundException;

    public class stringSort{
    public static void main(String[] args) throws Exception {

    FileInputStream fstream = new FileInputStream("textfile.txt"); // Open the file
    BufferedReader br = new BufferedReader(new InputStreamReader(fstream)); // allows file to be read

    String strLine;

    int count = Integer.parseInt(br.readLine());
    String[] myArray = new String[count];
    int i = 0;
    while ((strLine = br.readLine()) != null){ //Read File Line By Line
    myArray[i]= strLine;
    i++;
    }
    // bubble sort
    for(i=0; i<myArray.length; i++){ // while there are still lines left
    for(int j=i+1; j<myArray.length; j++){
    if(myArray[j].compareToIgnoreCase(myArray[i].toString())){ // where the problem is
    swap(myArray,i,j);
    }
    }
    }
    br.close(); //Close the input stream
    }
    }
    Last edited by KajuMax; August 7th, 2014 at 10:35 AM. Reason: Formatting/adding question marks


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Sort String Method

    When posting code, please use the highlight tags to preserve formatting.

    Also, you forgot to ask a question.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Aug 2014
    Posts
    2
    My Mood
    Mellow
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Sort String Method

    Sorry, I'm new here and I posted in a hurry.

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Sort String Method

    What value does the compareTo method return?
    Improving the world one idiot at a time!

Similar Threads

  1. [SOLVED] Which sort method to use for a String Array to output the index?
    By DANGEROUSSCION in forum Algorithms & Recursion
    Replies: 11
    Last Post: December 8th, 2012, 10:25 PM
  2. Replies: 2
    Last Post: November 11th, 2012, 10:44 PM
  3. Replies: 1
    Last Post: April 19th, 2012, 02:46 AM
  4. how to sort array by object string member value?
    By amgleason83 in forum Collections and Generics
    Replies: 9
    Last Post: March 16th, 2012, 05:29 PM
  5. How do I sort strings without using the sort method?
    By mjballa in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 4th, 2011, 03:27 PM

Tags for this Thread