Exception in thread "main" java.lang.Error: Unresolved compila

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 11468 Views - Last Post: 22 May 2011 - 12:21 PM Rate Topic: -----

#1 rchandra  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 21-March 11

Exception in thread "main" java.lang.Error: Unresolved compila

Posted 17 May 2011 - 01:00 AM

//getting formatted output into a string
public class Ex2 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
	
		int i =65;
		String s = "Hai";
	    Char ch = 'A';
	
		
		//format the output and get into str
		String str = String.format("i=%d\ns=%s\nch=%c", i , s,ch);
		System.out.println(str);

	}

}

This post has been edited by japanir: 17 May 2011 - 03:32 AM

Is This A Good Question/Topic? 0
  • +

Replies To: Exception in thread "main" java.lang.Error: Unresolved compila

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Exception in thread "main" java.lang.Error: Unresolved compila

Posted 17 May 2011 - 01:13 AM

Char ch = 'A';

should be

char ch = 'A';

Happy coding

P.S.
in the future please post your question in the body of the message and :code:
Was This Post Helpful? 1
  • +
  • -

#3 rchandra  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 21-March 11

Re: Exception in thread "main" java.lang.Error: Unresolved compila

Posted 17 May 2011 - 02:18 AM


public class Ex2 {

	
	public static void main(String[] args) {
	
		int i =65;
		String s = "Hai";
	    char ch = 'A';
	
		
		//format the output and get into str
		String str = String.format("i=%d%ns=%s%nch=%c", i , s,ch);
		System.out.println(str);
    



hi i have same problem in that only.the error is
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method Format(String, int, String, char) is undefined for the type String

This post has been edited by rchandra: 17 May 2011 - 02:18 AM

Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Exception in thread "main" java.lang.Error: Unresolved compila

Posted 17 May 2011 - 03:07 AM

It means your JDK is 1.4.xxx

String.format() was introcudec with JDK 1.5
Update your JDK to the latest JDK which is 1.6.2xx
Was This Post Helpful? 1
  • +
  • -

#5 rchandra  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 21-March 11

Re: Exception in thread "main" java.lang.Error: Unresolved compila

Posted 19 May 2011 - 02:27 AM



import java.io.*;
public class Sort {

	public static void main(String[] args)throws IOException {
		
		// to accept data from keyboard
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		
		//create an int type array
		System.out.print("How many elements? ");
		int n = Integer.parseInt(br.readLine());
		int arr[] = new int [n];
		
		//accept integer element into the array
		for(int i =0; i<n; i++)
	  {
		System.out.print("Enter int: ");
		arr[i] = Integer.parseInt(br.readLine());
		
	}
	//use bubble sort  technique to sort the integers
	int limit = n-1; //elements from 0 to n-1
	boolean flag =false; //if it is true, swapping down
	int temp; //temporary variable
	
	for(int i =0; i<n; i++)
	{
	  for(int j =0; j<n; j++)
	  {
		   //if first element is bigger than second one, then swap
		   if(arr[j]> arr[j+1])
		   {
			   temp = arr[j];
			   arr[j] = arr[j+1];
			   arr[j+1] = temp;
			   flag = true; //true -> swapping done
		   }
		   
	  }  
	  if(flag==false)break; //no swapping, so come out
	  else flag = false; //assign intial value
	}
	//display the sorted array
	System.out.println("the shorted array is: ");
	for(int i =0; i<n; i++)
	System.out.println(arr[i]);
	
	}
}




when i try to execute the program the error is
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
at Sort.main(Sort.java:37)
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Exception in thread "main" java.lang.Error: Unresolved compila

Posted 19 May 2011 - 03:15 AM

If n == 5 then arr = new int[5]; which means that the indexes go from 0 to 4
for(int j =0; j<n; j++)  
   arr[j] = arr[j+1];     // <--- and here, when j will == 4 you will try to access arr[5]



P.S. beside that do you say "Thank you sometimes ?" Did my other posts fixed your problems ?
Was This Post Helpful? 0
  • +
  • -

#7 rchandra  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 21-March 11

Re: Exception in thread "main" java.lang.Error: Unresolved compila

Posted 20 May 2011 - 08:29 AM

hi sorry to trobule u?
hi same problem in the same program, but the error occurs.
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
at Sort.main(Sort.java:39)
Was This Post Helpful? 0
  • +
  • -

#8 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7743
  • View blog
  • Posts: 13,082
  • Joined: 19-March 11

Re: Exception in thread "main" java.lang.Error: Unresolved compila

Posted 20 May 2011 - 08:59 AM

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5


This means that the length of the array is less than 6, and you're trying to access an element that doesn't exist.
Was This Post Helpful? 0
  • +
  • -

#9 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Exception in thread "main" java.lang.Error: Unresolved compila

Posted 20 May 2011 - 09:07 AM

Have you done what pbl told you? :nottalkingtoyou:
Was This Post Helpful? 1
  • +
  • -

#10 rchandra  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 21-March 11

Re: Exception in thread "main" java.lang.Error: Unresolved compila

Posted 20 May 2011 - 11:50 AM

View Postjon.kiparsky, on 20 May 2011 - 08:59 AM, said:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5


This means that the length of the array is less than 6, and you're trying to access an element that doesn't exist.


i tried what pbl told me but it is giving me the same error which i have posted
Was This Post Helpful? 0
  • +
  • -

#11 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7743
  • View blog
  • Posts: 13,082
  • Joined: 19-March 11

Re: Exception in thread "main" java.lang.Error: Unresolved compila

Posted 20 May 2011 - 12:07 PM

int arr[] = new int [n];
for(int j =0; j<n; j++)
	  {
		   //if first element is bigger than second one, then swap
		   if(arr[j]> arr[j+1])
		   {
		   	// do stuff
		   }
	   }


If you have an array of length n, your last element of that array is index n-1. If you have an array [5], its elements are numbered [0,1,2,3,4].
for(int j =0; j<n; j++)
 

For any n, what is the range of values taken by j in this loop?

	   if(arr[j]> arr[j+1])


For the range of j you got in the prevous question, what is the range of indexes you're trying to access here?

Do you see the problem now?
Was This Post Helpful? 2
  • +
  • -

#12 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8332
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Exception in thread "main" java.lang.Error: Unresolved compila

Posted 20 May 2011 - 02:33 PM

You really can't do

for(int j =0; j<n; j++)  
   arr[j] = arr[j+1];     // <--- and here, when j will == 4 you will try to access arr[5]


may be
for(int j =0; j< n - 1; j++)  
   arr[j] = arr[j+1];  


Was This Post Helpful? 2
  • +
  • -

#13 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7743
  • View blog
  • Posts: 13,082
  • Joined: 19-March 11

Re: Exception in thread "main" java.lang.Error: Unresolved compila

Posted 20 May 2011 - 08:36 PM

pbl is right. The important thing to understand is why this doesn't work.
It will probably help to walk through the loop with a pencil and watch what the values do.
Draw the array: for an array arr[5], draw 5 boxes. Number them, starting from zero. Then start with j at zero, and go through everything that loop does. Repeat until this all makes sense.
Was This Post Helpful? 2
  • +
  • -

#14 rchandra  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 21-March 11

Re: Exception in thread "main" java.lang.Error: Unresolved compila

Posted 22 May 2011 - 12:10 PM

View Postpbl, on 20 May 2011 - 02:33 PM, said:

You really can't do

for(int j =0; j<n; j++)  
   arr[j] = arr[j+1];     // <--- and here, when j will == 4 you will try to access arr[5]


may be
for(int j =0; j< n - 1; j++)  
   arr[j] = arr[j+1];  



hi thanks for u help, when i started execute the program the warning message comes Updated class files does not found.
Was This Post Helpful? 0
  • +
  • -

#15 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7743
  • View blog
  • Posts: 13,082
  • Joined: 19-March 11

Re: Exception in thread "main" java.lang.Error: Unresolved compila

Posted 22 May 2011 - 12:15 PM

Please post the exact compiler output.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2