2013-03-23 170 views
0

大家好我創造了這個代碼:無法編譯

import mpi.* ; 

class Hello { 

    static public void main(String[] args) { 
    MPI.Init(args); 



int myrank = MPI.COMM_WORLD.Rank(); 
long startTime = System.currentTimeMillis(); 
if (myrank == 0) { 
     String s = "111"; 
     byte[] bytes = s.getBytes(); 
     StringBuilder binary = new StringBuilder(); 
     for (byte b : bytes) { 
      int val = b; 
      for (int i = 0; i < 8; i++) { 
       binary.append((val & 128) == 0 ? 0 : 1); 
       val <<= 1; 
      } 
      binary.append(' '); 
     } 
     System.out.println("'" + s + "' to binary: " + binary); 

//int i = 54564; 
//String text = String.format("%15d", i); 
//System.out.println(message); //testing 

    char[] mess = binary.toCharArray(); 
    MPI.COMM_WORLD.Send(mess, 0, mess.length, MPI.CHAR, 1, 99); 
} else { 
    char[] mess = new char[20]; 
    MPI.COMM_WORLD.Recv(mess, 0, 200, MPI.CHAR, 0, 99); 
    System.out.println("received:" + new String(mess)); 
} 

long endTime = System.currentTimeMillis(); 
long duration = endTime - startTime; 
System.out.println(duration); 
    MPI.Finalize(); 
    } 
} 

它應該做的是從111輸入創建二進制形式的3個字節,然後發送這3個二進制字節到另一臺計算機在網絡上(一個基本的乒乓球項目)

但我不能得到它來編譯

我得到這些錯誤:

Hello.java:28: error: cannot find symbol 
    char[] mess = binary.toCharArray(); 
         ^
    symbol: method toCharArray() 
    location: variable binary of type StringBuilder 
1 error 

C:\Users\Richard\Dropbox\Year 3\DISPARP\Coursework>javac Hello.java 
Hello.java:30: error: cannot find symbol 
    char[] mess = binary.toCharArray(); 
         ^
    symbol: method toCharArray() 
    location: variable binary of type StringBuilder 
1 error 

另一個問題是

111' to binary: 00110001 00110001 00110001 

3個字節?

編輯:

已更改爲char[] mess = binary.toString().toCharArray();

,但現在得到更多的錯誤:(

MPJ Express (0.35) is started in the cluster configuration 
Starting process <0> on <Raptor> 
Starting process <1> on <Predator> 
'111' to binary: 00110001 00110001 00110001 
340 
mpi.MPIException: java.lang.ArrayIndexOutOfBoundsException: Out of bounds in des 
t array: 26 
     at mpi.SimplePackerChar.unpack(SimplePackerChar.java:112) 
     at mpi.Comm.recv(Comm.java:1305) 
     at mpi.Comm.Recv(Comm.java:1255) 
     at Hello.main(Hello.java:34) 
     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl. 
java:57) 
     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces 
sorImpl.java:43) 
     at java.lang.reflect.Method.invoke(Method.java:601) 
     at runtime.daemon.Wrapper.execute(Wrapper.java:165) 
     at runtime.daemon.Wrapper.main(Wrapper.java:180) 
Caused by: java.lang.ArrayIndexOutOfBoundsException: Out of bounds in dest array 
: 26 
     at mpjbuf.Buffer.readCheckArgs(Buffer.java:3650) 
     at mpjbuf.Buffer.read(Buffer.java:3333) 
     at mpi.SimplePackerChar.unpack(SimplePackerChar.java:110) 
     ... 9 more 
java.lang.reflect.InvocationTargetException 
     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl. 
java:57) 
     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces 
sorImpl.java:43) 
     at java.lang.reflect.Method.invoke(Method.java:601) 
     at runtime.daemon.Wrapper.execute(Wrapper.java:165) 
     at runtime.daemon.Wrapper.main(Wrapper.java:180) 
Caused by: mpi.MPIException: mpi.MPIException: mpi.MPIException: java.lang.Array 
IndexOutOfBoundsException: Out of bounds in dest array: 26 
     at mpi.Comm.Recv(Comm.java:1259) 
     at Hello.main(Hello.java:34) 
     ... 6 more 
Caused by: mpi.MPIException: mpi.MPIException: java.lang.ArrayIndexOutOfBoundsEx 
ception: Out of bounds in dest array: 26 
     at mpi.Comm.recv(Comm.java:1317) 
     at mpi.Comm.Recv(Comm.java:1255) 
     ... 7 more 
Caused by: mpi.MPIException: java.lang.ArrayIndexOutOfBoundsException: Out of bo 
unds in dest array: 26 
     at mpi.SimplePackerChar.unpack(SimplePackerChar.java:112) 
     at mpi.Comm.recv(Comm.java:1305) 
     ... 8 more 
Caused by: java.lang.ArrayIndexOutOfBoundsException: Out of bounds in dest array 
: 26 
     at mpjbuf.Buffer.readCheckArgs(Buffer.java:3650) 
     at mpjbuf.Buffer.read(Buffer.java:3333) 
     at mpi.SimplePackerChar.unpack(SimplePackerChar.java:110) 
     ... 9 more 

回答

2

方法toCharArray是未定義StringBuilder但是你可以,使用來自String的方法從toString返回:

char[] mess = binary.toString().toCharArray(); 

編輯:我與MPI不familar但嘗試使char陣列緩衝區大期間Recv接受更多的數據:

char[] mess = new char[2000]; 
MPI.COMM_WORLD.Recv(...); 
+0

非常感謝你,這解決了一個問題,但我我仍然有錯誤:( – user2065929 2013-03-23 21:43:01