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 2 of 2

Thread: Why am I getting so many errors in my code?

  1. #1
    Member
    Join Date
    Oct 2013
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Why am I getting so many errors in my code?

    import java.util.*;
    import java.io.*;
     
    class arraysort{
    	public static void main(String args[]) throws Exception{
    		int index,number=0;			
    		Scanner input = new Scanner(new File("wordlist.txt"));
    		String words[] = new String[100];
    		String max,k;
     
    		int a[];
    		for(int i=0;i<words.length;i++){
    		   number = (int)(Math.random()*10);
    		   for (int j=0;j<number;j++){
    		     words[j] = input.nextLine();
    		}
     
    		for (int c=0;c<99;c++){
               {
    		     max = a[c];
    		     for (int j=c+1;j<100;j++)
    			   {
    			     if (a[j].compareTo(max)>0)
    				   {
    				     max = a[j];
    					 index = j;
    				   }
    			   }
    			 k = a[c];
    			 a[c] = a[index];
    			 a[index] = k;
    		    }
    		}
    	  }
     
    	}
    }

    Errors are:

    arraysort.java:20: error: incompatible types
    max = a[c];
    ^
    required: String
    found: int
    arraysort.java:23: error: int cannot be dereferenced
    if (a[j].compareTo(max)>0)
    ^
    arraysort.java:25: error: incompatible types
    max = a[j];
    ^
    required: String
    found: int
    arraysort.java:29: error: incompatible types
    k = a[c];
    ^
    required: String
    found: int
    arraysort.java:31: error: incompatible types
    a[index] = k;
    ^
    required: int
    found: String
    5 errors
    Error: Could not find or load main class arraysort
    Press any key to continue . . .


    How do I resolve these errors?


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

    Default Re: Why am I getting so many errors in my code?

    Take them one at a time.

    The error that says int cannot be dereferenced means that you're trying to use the dot operator on an int, which is a primitive. Think about exactly what you're trying to do; there is a simpler way than trying to call a method on a primitive.

    The errors about incompatible types means that you're trying to store a String value in an int variable, or an int value in a String value, or something similar. There are functions in the String and Integer classes that might help with conversions.

    Also, fix your indentations, which should help you match up parenthesis.
    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!

Similar Threads

  1. Replies: 3
    Last Post: April 27th, 2013, 08:19 AM
  2. NullPointException errors in code?
    By fw123 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 28th, 2013, 08:13 AM
  3. Someone help me with my code it has some errors? Thanks!
    By skitheeast8 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: November 1st, 2012, 07:37 PM
  4. weird errors in my code
    By jobje325 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 17th, 2012, 12:15 PM
  5. Code is giving an error in console, but there are no errors apparent in the code!
    By JamEngulfer221 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 15th, 2011, 09:30 PM