Results 1 to 13 of 13
  1. #1
    sanox is offline Member
    Join Date
    Sep 2009
    Posts
    14
    Rep Power
    0

    Default Why is the code not working

    Java Code:
    public static void main(String[] args) {
        	
        	Scanner scanner = new Scanner(System.in);
        	Vector<String> string = new Vector<String>();
        	System.out.println("Please enter five names");
        	System.out.print("First name: ");
        	String name1 = scanner.next();
        	System.out.print("Second name: ");
        	String name2 = scanner.next();
        	System.out.print("Third name: ");
        	String name3 = scanner.next();
        	System.out.print("Fourth name: ");
        	String name4 = scanner.next();
        	System.out.print("Fifth name: ");
        	String name5 = scanner.next();
        	
        	string.addElement(name1);
        	string.addElement(name2);
        	string.addElement(name3);
        	string.addElement(name4);
        	string.addElement(name5);
        	
        	System.out.print("Please enter a name to search: ");
        	String str = scanner.next();
        	
        	int index, smallestIndex, minIndex, temp;
        	
        	Object vectorArray[] = string.toArray();
    
        	for (index = 0; index < vectorArray.length–1; index++){
            	smallestIndex = index; 
            	for (minIndex = index + 1; minIndex < vectorArray.length; minIndex++)
                	if (vectorArray[minIndex].toString().compareTo(vectorArray[smallestIndex].toString()) > 0)
                    	smallestIndex = minIndex; 
    
            	temp = vectorArray[smallestIndex];
            	vectorArray[smallestIndex] = vectorArray[index];
            	vectorArray[index] = temp;
        	}
        	
        	//After sorting again converted back to Vector
        	Vector v = new Vector(Arrays.asList(vectorArray));
    	 	System.out.println(v);
    
        }
    }
    The error was
    C:\Documents and Settings\user\My Documents\JCreator Pro\MyProjects\T.java:41: illegal character: \8211
    for (index = 0; index < vectorArray.length–1; index++){
    ^
    C:\Documents and Settings\user\My Documents\JCreator Pro\MyProjects\T.java:41: ')' expected
    for (index = 0; index < vectorArray.length–1; index++){
    ^
    C:\Documents and Settings\user\My Documents\JCreator Pro\MyProjects\T.java:41: ';' expected
    for (index = 0; index < vectorArray.length–1; index++){
    ^
    3 errors

    Process completed.
    WHat i am trying to do is selection sort. Thanks in advance.

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    For starters, count your braces and make sure every { has a corresponding }. This is easy to do if the code is well formatted. Also make sure every ( has a corresponding ).

  3. #3
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    The message says you have an illegal character with code 8211 on line 41. I believe this is a dash '-' in some character sets (Chinese?). Did you copy and paste this code from a web site? You will sometimes find that code on web pages has certain characters replaced with different character codes that look the same (spaces are often a problem). You need to replace these with the correct characters.

    You need to edit the code and retype the '-' in line 41, in the 'for' statement that the error message is complaining about. If you copy the code into an editor like TextPad, for example, (not notepad), these illegal characters will often show as black blocks.
    Last edited by dlorde; 09-06-2009 at 08:10 PM.

  4. #4
    sanox is offline Member
    Join Date
    Sep 2009
    Posts
    14
    Rep Power
    0

    Default

    Thanks guys, By the way, please do not care about the braces as i didnt copy it as they make the codes longer. I will deal with them. Here is my another problem.
    Java Code:
    int index, smallestIndex, minIndex;
        	String temp;
        	
        	Object vectorArray[] = string.toArray();
    
        	for (index = 0; index < vectorArray.length - 1; index++){
            	smallestIndex = index; 
            	for (minIndex = index + 1; minIndex < vectorArray.length; minIndex++)
                	if (vectorArray[minIndex].toString().compareTo(vectorArray[smallestIndex].toString()) > 0)
                    	smallestIndex = minIndex; 
    
            	temp = vectorArray[smallestIndex];
            	vectorArray[smallestIndex] = vectorArray[index];
            	vectorArray[index] = temp;
        	}
        	
        	//After sorting again converted back to Vector
        	Vector v = new Vector(Arrays.asList(vectorArray));
    	 	System.out.println(v);
    Note that i changed the temp variable to String type. This is due to my selection sort is a string sorting

    The error was
    found : java.lang.Object
    required: java.lang.String
    temp = vectorArray[smallestIndex];
    ^
    Note: C:\Documents and Settings\user\My Documents\JCreator Pro\MyProjects\T.java uses unchecked or unsafe operations.
    Note: Recompile with -Xlint:unchecked for details.
    1 error

    Process completed.
    .

  5. #5
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    You are not using generics and vectors store objects by default. Either add generics Lesson: Generics (The Java™ Tutorials > Learning the Java Language) or cast the objects to Strings

    So, when you say
    Java Code:
    temp = vectorArray[smallestIndex];
    temp is of type String, but vectorArray[smallestIndex] is of type Object. You need to cast it or use generics to begin with, or use the toString method you used before:
    Java Code:
    temp = vectorArray[smallestIndex].toString();
    However, while this works in this case, I do not suggest doing it this way. If you were dealing with ints or some other type of object all together, toString() would not work. So, I reassert my statement about generics. Here is an example:
    Java Code:
    public class GenericsTest {
        Vector<String> withGenerics;
        Vector noGenerics;
    
        public GenericsTest(){
            withGenerics = new Vector<String>();
            noGenerics = new Vector();
            
            withGenerics.add("Hi");
            noGenerics.add("Hi");
            withGenerics.add("There");
            noGenerics.add("There");
            withGenerics.add("!");
            noGenerics.add("!");
            
            
            String test1 = withGenerics.get(1); //works
            String test2 = noGenerics.get(1);   //does not work
            
        }
    }

  6. #6
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    Oops, I did not see before (I mis-read your code/wasn't thinking) you are using a regular array called vector, not an actual vector. In that case, use a cast:
    Java Code:
    temp = (String)vectorArray[smallestIndex];
    Also, I suggest looking into the ArrayList class, as it supports generics the way I described in my previous example or the older (but not thread-safe) Vector class which I actually used in my example.

  7. #7
    sanox is offline Member
    Join Date
    Sep 2009
    Posts
    14
    Rep Power
    0

    Default

    quad64bit, Thanks a lot. Really appreciate that. They worked. By the way, i tried to modify my code to not to convert those vectors to arrays and here is the result.

    Java Code:
    int index, smallestIndex, minIndex;
        	String temp;
    for (index = 0; index < string.length - 1; index++){
            	smallestIndex = index; 
            	for (minIndex = index + 1; minIndex < string.length; minIndex++)
            		if (string.elementAt(smallestIndex).toString().compareTo(string.elementAt(minIndex).toString()) > 0)
                    	smallestIndex = minIndex; 
    
            	temp = string.elementAt(smallestIndex);
            	string.elementAt(smallestIndex) = string.elementAt(index);
            	string.elementAt(index) = temp;
    System.out.print(string);
        	}
    The above part did not change.

    and here is the error.
    C:\Documents and Settings\user\My Documents\JCreator Pro\MyProjects\Tjava:51: cannot find symbol
    symbol : variable length
    location: class java.util.Vector<java.lang.String>
    for (index = 0; index < string.length - 1; index++){
    ^
    C:\Documents and Settings\user\My Documents\JCreator Pro\MyProjects\T.java:53: cannot find symbol
    symbol : variable length
    location: class java.util.Vector<java.lang.String>
    for (minIndex = index + 1; minIndex < string.length; minIndex++)
    ^
    C:\Documents and Settings\user\My Documents\JCreator Pro\MyProjects\T.java:58: unexpected type
    required: variable
    found : value
    string.elementAt(smallestIndex) = string.elementAt(index);
    ^
    C:\Documents and Settings\user\My Documents\JCreator Pro\MyProjects\T.java:59: unexpected type
    required: variable
    found : value
    string.elementAt(index) = temp;
    ^
    4 errors

    Process completed.
    It runs well but with those 4 errors (when using this method, without converting to array first).
    Last edited by sanox; 09-07-2009 at 07:31 AM.

  8. #8
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    I would suggest shelving that code for now and starting with Sun's Java tutorial.
    Once you get the basics from Sun's tutorial, you will then realize that a compilation error message actually gives you clues as to what the problem in the code is.

  9. #9
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    r035198x is correct, and I agree. But for the sake of being helpful here is whats happening.

    It looks like in your code you're using the Vector class? Well, the problem stems from the fact that you cannot get the size of a vector in the same was as an array. An array has a field called length so someArray.length will give you an int representing the size.

    Vector does not. You need to use a method. In this case, someVector.size() . I strongly recommend that if you go down that path that you use the ArrayList class instead though -- its newer and doest things slightly differently. Check it out in the Java API on sun's website.

  10. #10
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    832
    Rep Power
    6

    Default

    This question answered by me in this thread.I don't know the reason for your confusion.

    Vector
    Ramya:cool:

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    This question answered by me in this thread.I don't know the reason for your confusion.
    Don't you just hate it when they create a new thread before acknowledging that they even read your previous reply?

  12. #12
    sanox is offline Member
    Join Date
    Sep 2009
    Posts
    14
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Don't you just hate it when they create a new thread before acknowledging that they even read your previous reply?
    I have read and understood. Thank you very much RamyaSivakanth for the answer on my previous thread. But that was selection sort and this is insertion sort. So, i would like to make it into 2 different threads. And 1 more thing, i would like to know how i can accomplish with vector method instead of converting it to an array. Anyhow, i really appreciate your posts in the previous thread. And thank you for that. I used that but just want to clarify this.


    By the way, I have this problem with insertion sort.

    Java Code:
    int firstOutOfOrder, location;
        String temp;
        
        Object vectorArray[] = string.toArray();   //Transform a vector to an array.
        for (firstOutOfOrder = 1; firstOutOfOrder < vectorArray.length; firstOutOfOrder++){
        if (vectorArray[firstOutOfOrder].toString().compareTo(vectorArray[firstOutOfOrder - 1].toString()) > 0){
            temp = vectorArray[firstOutOfOrder].toString();
    		location = firstOutOfOrder;
            do{
                vectorArray[location] = vectorArray[location - 1];
                location--;
            } 
            while(location > 0 && vectorArray[location - 1].toString().compareTo(temp) > 0);
            vectorArray[location] = temp;
        }
        }
        //After sorting again converted back to Vector
        Vector v = new Vector(Arrays.asList(vectorArray));
    	System.out.println(v);
    result:
    Please enter five names
    First name: asd
    Second name: rfg
    sThird name: vsd
    Fourth name: ds
    Fifth name: fsdg
    [ds, fsdg, rfg, vsd, asd]

    Process completed.
    the last item should be at the first place. But it is at the last. Any ideas?
    Last edited by sanox; 09-07-2009 at 07:50 PM.

  13. #13
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    832
    Rep Power
    6

    Default

    Without array,using only Vector I did sorting.Is this u want? I have not run the code.Please check it.
    Please try to read the api and do it by yourself to improve ur logic.

    Don't keep main key words as variable names.

    Gothru the code below .I did small modification to ur code.
    Java Code:
       import java.util.*;	
       public class Test
       {
         /*******************************************************************************
         *  Outputs integers from user input external files.
         ********************************************************************************/
        
           public static void main(String[] commandlineArguments)throws InputMismatchException 
           {
            Scanner scanner = new Scanner(System.in);
        	Vector<String> string = new Vector<String>();
        	System.out.println("Please enter five names");
        	System.out.print("First name: ");
        	String name1 = scanner.next();
        	System.out.print("Second name: ");
        	String name2 = scanner.next();
        	System.out.print("Third name: ");
        	String name3 = scanner.next();
        	System.out.print("Fourth name: ");
        	String name4 = scanner.next();
        	System.out.print("Fifth name: ");
        	String name5 = scanner.next();
        	
        	string.addElement(name1);
        	string.addElement(name2);
        	string.addElement(name3);
        	string.addElement(name4);
        	string.addElement(name5);
        	
           
    		for(int i=0;i< string.size() ;i++)
    		{
    			for (int j=i+1;j< string.size();j++)
    			{
    				if(string.get(i).toString().compareTo(string.get(j).toString()) > 0 )
    				{
    					Object x,y,temp;
    					temp=string.get(i);
    					string.setElementAt(string.get(j).toString(),i);
    					string.setElementAt(temp.toString(),j);
    				} //if
        		
    		  }//for
    	   }//for
    
    				System.out.println(string);     																																																																								
          	     
       }//main
      }//class
    Ramya:cool:

Similar Threads

  1. Next Page Code is not working
    By Java.child in forum AWT / Swing
    Replies: 2
    Last Post: 02-18-2009, 05:26 PM
  2. JNI: Why this code not working?
    By playwin2 in forum Advanced Java
    Replies: 5
    Last Post: 11-25-2008, 01:12 PM
  3. My code is not working properly ..modify it
    By Shyam Singh in forum New To Java
    Replies: 14
    Last Post: 07-16-2008, 05:48 PM
  4. Replies: 8
    Last Post: 05-28-2008, 07:00 AM
  5. Replies: 1
    Last Post: 04-26-2007, 03:52 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •