Character array problem

i need to convert this program so it sorts characters instead of numbers:
public class SortDemo {
public static void main(String[] args) {
int[] arrayOfInts = { 32, 87, 3, 589, 12, 1076,
2000, 8, 622, 127 };
for (int i = arrayOfInts.length; --i >= 0; ) {
for (int j = 0; j < i; j++) {
if (arrayOfInts[j] > arrayOfInts[j+1]) {
int temp = arrayOfInts[j];
arrayOfInts[j] = arrayOfInts[j+1];
arrayOfInts[j+1] = temp;
for (int i = 0; i < arrayOfInts.length; i++) {
System.out.print(arrayOfInts[i] + " ");
System.out.println();
Any help please....

Skydiver,
Change your int[] to char[]. It makes sense to change your arrayOfInt to arrayOfChar for the sake of form.
Note that the char values for initializing the array are enclosed in single quotes.
Change your temp variable to type char.
And if you really want to be lazy.....
public class SortDemo {
     public static void main(String[] args) {
          char[] arrayOfChar = {'a','x','f','l','z','b','e','s','q','t'};
          for (int i = arrayOfChar.length; --i >=0;) {
               for (int j = 0; j < i; j++) {
                    if (arrayOfChar[j] > arrayOfChar[j + 1]) {
                         char temp = arrayOfChar[j];
                         arrayOfChar[j] = arrayOfChar[j + 1];
                         arrayOfChar[j + 1] = temp;
                    } // end of if loop
               } // end of j iteration
          } // end of i iteration
          for (int i = 0; i < arrayOfChar.length; i++)
            System.out.print(arrayOfChar[i] + " ");
          System.out.println();
     } // end method main
} // end class SortDemo

Similar Messages

  • Converting bytes to character array problem

    I am trying to convert a byte array into a character array, however no matter which Character Set i choose i am always getting the '?' character for some of the bytes which means that it was unable to convert the byte to a character.
    What i want is very simple. i.e. map the byte array to its Extended ASCII format which actually is no change in the bit represtation of the actual byte. But i cannot seem to do this in java.
    Does anyone have any idea how to get around this problem?

    Thanks for responding.
    I have however arrived at a solution. A little un-elegant but it seems to do the job.
    I am converting each byte into char manually by this algorithm:
              for (int i=0;i<compressedDataLength;i++)
                   if (((int) output)<0)
                        k=(127-((int) output[i]));
                        outputChr[i]=((char) k);
                   else
                        k=((int) output[i]);
                        outputChr[i]=((char) k);
                   System.out.println(k);
    where output is the byte array and outputChr is the character array

  • Character array type conversion problem

    Hi, I'm using BlazeDS (v3.0.0.544) to remotely call Java code on the server from my Flex client. I'm having trouble when converting a character array type from Java to ActionScript and vice-versa. In my Flex code, if the ActionScript class uses a String type to map to the char[] then the char[] is successfully converted from Java to ActionScript, but not from ActionScript to Java.
    // Extract from ActionScript code
    private var m_charArray:String;
    public function get charArray():String
    return m_charArray;
    public function set charArray(value1:String):void
    m_charArray = value1;
    Alternatively, if my ActionScript class uses an Array type then I can successfully send data from the client and populate a char[] field on the server, but can't send an updated char[] back to my ActionScript class.
    In fact the Flex console reports:
    TypeError: Error #1034: Type Coercion failed: cannot convert "myString" to Array.
    The code this time:
    // Extract from ActionScript code
    private var m_charArray:Array;
    public function get charArray():Array
    return m_charArray;
    public function set charArray(value1:Array):void
    m_charArray = value1;
    All the other types I have tried work successfully, it's only with char arrays that I've hit a problem.
    Any help would be appreciated, thanks.

    Hi Graeme,
    It is how it works. I can see the inconsistency. You can find an example how of echo char[] in blazeds\trunk\qa\apps\qa-regress\testsuites\mxunit\tests\remotingService\dataTypes\CharTy pesTest.mxml. It echos a char[] in return of a string. You can get the char in a string easily string.charAt() and get its length using length(). I think it may be the reason remoting decided to use a string to represent a char[] instead of a generic objects array. It should be more memory efficient. However, I can see that it can turn into problem as what you descibed. When the data has to send back to server, it has to convert back to array. If the char[] type is used inside an object, it is impossible to do the conversion unless the getter and setter are generic
    public function get charArray():Object
    return m_charArray;
    public function set charArray(value1:Object):void
    if (value1 is String)
    m_charArray = new Array();
    //copy the string elements to it
    else
    m_charArray = value1;
    I know it is ugly, but it the only work around I can think of. I think it needs to log a bug to correct this conversion behavior
    William

  • HOW TO DO CHECKING BETWEEN THE CHARACTER ARRAY.

    hi all!!
    can any one please help me in checking the two character arrays.
    in my code i need to compare a character array(seq) to that of hydrob and hydrop . if the seq has hydrob then it should be given a different colour. if it has a hydrop then it should be assigned a different colour. if the sequence does not have a character that is neither in hydrob and hydrop then they shuld be given a separate colour.
    for (int i=0;i<seqlength;i++)//a for loop to get a letter
                char str1 = seq; // getting the first letter from
    for (int j=0;j<hydroblen;j++)//for loop to check the
    if (str1 == hydrob[j])//checking problem in here (suppose)
    res=1;//setting a value
    System.out.println("reach") ;
    repaint();//calling d paint function to paint in
    //System.out.println("reach") ;
    //j++;
    System.out.println("over") ;
    for (int i=0;i<seqlength;i++)
    char str2 = seq[i];
    for (int k =0;k<hydroplen;k++)
    while(str2==hydrop[k])
    res =0;
    repaint();
    k++;
    System.out.println("reach") ;
    i have compared them using this. but this is not much efficient which just paints the hydrophilic sequence lenght and paints the seq for that length of sequence.
    also the if loop in the paint function which have to do the paint for hydrob is not executing.
    if(res==1)
                gc.drawString(s, 700,700) ;// not executing the loop
                 for (int i =0;i<seqlength;i++)
               gc.drawString(s, 700,700) ;  //to check whether the loop s executing.
            for (int j=0; j<hydroblen;j++)
                x= x0 + radius*Math.sin(theta*(i));
    y[i]= y0 - radius*Math.cos(theta*(i));
    X[i] = (int) x[i];
    Y[i]= (int) y[i];
    gc.fillOval(X[j]-7,Y[j]-7,14,14);
    seq1 = seq[i] + "";
    hydrop1 = hydrop[j]+"";
    gc.drawString(seq1, X[i], Y[i]-8);
    gc.drawString("",X[j], Y[j]-8);
    gc.drawLine(X[j],Y[j],X[j+1],Y[j+1]);
    gc.setColor(Color.yellow);

    Hi Nitish,
    I think you might have to consult SAP for this. In one of my project experience, I had local currency related probled due to currency change in SAP R/3 during the fiscal year.
    I had a way out there, in a way that I dropped BW data with old currency and uploaded data with new currency and re-did the consoldiation starting from begining of the fiscal year. Unfortunatley, in your scenarios this is not possible
    During my discussion with SAP, I came to know that SAP do provide specific conversion consulting in BW BCS area as well.
    Hope this might help you.
    Best Regards,
    UR

  • Filling character array with String

    I have character array of size 50. I have a string p="abcdef"
    I want to fill this String in char index ranging from 25-30(specific range).
    Is there any method to do this?? Please suggest me

    corlettk wrote:
    Ummm yeah... it's probably homework... Oops!
    </drops-and-gives-you-twenty> indeed ;-)
    But Hmmm... I've never understood educators universal compulsion to make students do things the hard way.I have no real problem with making students do things the hard way. In this case it looks to me to be an exercise in looping and accessing characters in a String. The OP will gain some knowledge by using the API but he will gain more by actually writing the detailed code himself.
    P.S. In the 'New To Java' forum I start with the assumption that everything is homework.

  • A quick Array Problem

    hey all,
    i'm having a dumb array problem, I cannot figure out what I've done wrong !
    I've set up an empty array of JButtons (because that '4' is actually a variable in my code)
    HERE IS THE CODE
    //===========================
    JButton[] buttons = new JButton[4] ;
    for (int g = 0; g < 4; g++)
    Imag[g].setIcon(imageIcons[g]);     
    buttons[g].setEnabled(true);
    System.out.print (buttons[g] + " " + g + "\n");
    //===========================
    My Error is a null pointer exception. And when I take out the:
    buttons[g].setEnabled(true);
    line, I just get the ouput, and it is:
    null 0
    null 1
    null 2
    null 3
    Ok, I know I'm probably making one dumb little mistake, but I have no idea what it is right now, Please Help !! thanks,
    Kngus

    When you want to use an array, you declare it (which you did), construct it (which you did), and initialize it (which the VM did). When you initialize an array of primitives, the primitives get their default value (0 for signed int types, 0.0 for float types, false for boolean, null-character for char) and so do object references (null).
    You are setting up an array of object references. When the VM initializes it, the elements of the array (i.e. the references to the JButtons) are set to null. That's why you're getting the NullPointerException. You need additional code, something along the lines of this:
    for(int j = 0; j < buttons.length(); j++) {
        buttons[j] = new JButton();
    }Now, your buttons array will contain references to JButtons rather than null.
    AG

  • Does key comparison work for character arrays?

    I am using the berkeley db(C) to store a 2-column table with key as a character array(not strings). Even though I can see that a record is present when i display all records, I cant get that record using the get function with the correct key. Is there a problem with key comparison? How does berkeley db check two keys for equality? Is there a way around this?
    thanks in advance.

    Hi,
    Your code posting was messed up due to the indexed addressing (the i in square brackets) which is in fact the italic start tag. You can put some spaces inside the square brackets, and make sure to enclose your source code between pre tags ([ pre ] <code goes here> [ / pre ], remove the spaces).
    So, I assume you are trying to store a line of the bi-dimensional char array.
         key.data = a + i;          
         key.size = sizeof(a[ i ]);
    // char *aa = "0a0s0d0f0g0h0j0k0l0q0w0e0r0t0y0f";(I've changed the name of this pointer to distinguish it from the array)
    aa points to a sequence of chars (a string in fact) that should be the same as the ones in the line of the bi-dimensional char array, line that was stored as key in the database, right?
    key.data = aa;
    key.size = sizeof(aa);Now, here is the problem. aa is a pointer to char, thus sizeof(aa) is 2B or 4B (depending on the pointer type, near or far) on a 32-bit machine. So, you're performing the search looking only for the match of the first 2 or 4 chars. Change that line to key.size = strlen(aa); or key.size = sizeof(*(a + i)); (the same as key.size = sizeof(a[ i ]);, a being the char array).
    Regards,
    Andrei

  • Character conversion problems when calling FM via RFC from Unicode ECC 6.0?

    Hi all,
    I faced a Cyrillic character convertion problem while calling an RFC function from R/3 ECC 6.0 (initialized as Unicode system - c.p. 4103). My target system is R/3 4.6C with default c.p. 1500.
    The parameter I used in my FM interface in target system is of type CHAR10 (single-byte, obviously).
    I have defined rfc-connection (SM59) as an ABAP connection and further client/logon language/user/password are supplied.
    The problem I faced is, that Cyrillic symbols are transferred as '#' in the target system ('#' is set as default symbol in RFC-destination definition in case character convertion error is met).
    Checking convertions between c.p. 4103  and target c.p. 1500 in my source system using tools of transaction i18n shows no errors - means conversion passed O.K. It seems default character conversion executed by source system whithin the scope of RFC-destination definition is doing something wrong.
    Further, I played with MDMP & Unicode settings whithin the RFC-destination definition with no successful result - perhaps due to lack of documentation for how to set and manage these parameters.
    The question is: have someone any experience with any conversion between Unicode and non-Unicide systems via RFC-call (non-English target obligatory !!!), or can anyone share valuable information regarding this issue - what should be managed in the RFC-destination in order to get character conversion working? Is it acceptable to use any character parameter in the target function module interface at all?
    Many thanks in advance.
    Regards,
    Ivaylo Mutafchiev
    Senior SAP ABAP Consultant

    hey,
    I had a similar experience. I was interfacing between 4.6 (RFC), PI and ECC 6.0 (ABAP Proxy). When data was passed from ECC to 4.6, RFC received them incorrectly. So i had to send trimmed strings from ECC and receive them as strings in RFC (esp for CURR and QUAN fields). Also the receiver communication channel in PI (between PI and  RFC) had to be set as Non unicode. This helped a bit. But still I am getting 2 issues, truncation of values and some additional digits !! But the above changes resolved unwanted characters problem like "<" and "#". You can find a related post in my id. Hope this info helps..

  • Backup failure due to Character set problem

    Hi,
    I am manually running a COLD backup script in Windows NT environment and all the logs has been captured below:
    Recovery Manager: Release 8.1.6.0.0 - Production
    RMAN-06005: connected to target database: db1 (DBID=754030292)
    RMAN-06009: using target database controlfile instead of recovery catalog
    RMAN> shutdown immediate;
    2> startup mount;
    3> RUN {
    4> ALLOCATE CHANNEL disk1 TYPE disk;
    5> BACKUP DATABASE TAG 'db1_db_full' FORMAT 'e:\backup\db1\db1_backup';
    6> copy current controlfile to 'e:\backup\db1\Control_db1.ctl';
    7> }
    8>
    RMAN-06405: database closed
    RMAN-06404: database dismounted
    RMAN-06402: Oracle instance shut down
    RMAN-06193: connected to target database (not started)
    RMAN-06196: Oracle instance started
    RMAN-06199: database mounted
    Total System Global Area 934143244 bytes
    Fixed Size 70924 bytes
    Variable Size 260554752 bytes
    Database Buffers 673439744 bytes
    Redo Buffers 77824 bytes
    RMAN-03022: compiling command: allocate
    RMAN-03023: executing command: allocate
    RMAN-08030: allocated channel: disk1
    RMAN-08500: channel disk1: sid=13 devtype=DISK
    RMAN-03022: compiling command: backup
    RMAN-00571: ===========================================================
    RMAN-00569: =============== ERROR MESSAGE STACK FOLLOWS ===============
    RMAN-00571: ===========================================================
    RMAN-03002: failure during compilation of command
    RMAN-03013: command type: backup
    RMAN-06003: ORACLE error from target database: ORA-06550: line 1, column 166:
    PLS-00553: character set name is not recognized
    ORA-06550: line 0, column 0:
    PL/SQL: Compilation unit analysis terminated
    RMAN-06031: could not translate database keyword
    Recovery Manager complete.
    As the above log shown, I cannot do any backup command in the RUN bracket and it complains that the character set is not recognized.
    This set of error happens when I have create six other Oracle databases in my NT box. Before that, I can manually run the backup with no problem and a backupset has been generated.
    If you have come across this problem and have solutions of it. That will be great.
    Thanks !!
    null

    kk001 wrote:
    Hi ,
    The export Backup failing due to character set problem
    . . exporting table ravidlx
    EXP-00008: ORACLE error 6552 encountered
    ORA-06552: PL/SQL: Compilation unit analysis terminated
    ORA-06553: PLS-553: character set name is not recognized
    P
    Please suggest how to set character set
    I don't know what you have.
    I don't know what you do.
    I don't know what you see.
    It is really, Really, REALLY difficult to fix a problem that can not be seen.
    use COPY & PASTE so we can see what you do & how Oracle responds.
    do as below so we can know complete Oracle version & OS name.
    Post via COPY & PASTE complete results of
    SELECT * from v$version;

  • Using translate function to correct character set problem....

    I have a table(TBL_STOCK) on Oracle XE.
    Rows come from sql server 2005 with a trigger on sql server table via the linked server.
    But there is a character set problem with some character like İ,Ş,Ğ.
    They change to Ý,Þ,Ð. in Oracle.
    How can i correct these ? Do you suggest the TRANSLATE function ?
    What do u think, if i create an After Insert trigger on Oracle table(TBL_STOCK) and convert these character using the Translate function when they inserted from sql server.
    Anyone have any other ideas that can be more efficient. Any thoughts appreciated.
    Thanks in advance.
    Adam
    PS:The NLS_CHARACTERSET of Oracle is AL32UTF8.

    It is sql server 2005 and Collation is SQL_Latin1_General_CP1_CI_AS

  • Using values to create a scatter plot in a character array

    Ok, I am fairly new to the language and i am working ona program that will first, get the users input of how many values and the x and y of each value, it will store it into a 2d array and also have the x-squared value and X*Y value. I have all the entry of the data working and the calculation for the line of best fit works correctly.
    What i need help with is, I want to take those values and store them into a character array that will display the points where they belong with a '*'. I also need to make the graph lines on the left and bottom. If the values are less than 10 for the x or y, i need to make them 10. The Y line needs to be rounded to the next highest 10s place if its over 10. I have tried a few things and i realised that it is completly wrong..
    public void graph(int[][] grid, int n)
                int highY = 0;
                for(int x=0;x<n; x++)
                 if (grid[x][1] > highY)
                    highY = grid[x][1];
             char[][] output = new char[n+1][highY+1];
             for(int z=0; z<highY+1; z++)
                 output[z][0]='+';
             for(int w=0; w<n+1; w++)
                 output[n][w]='+';
             int tempx=1; int tempy=1;
             output[tempx][tempy]='*';
             while(tempx<n+1)
                 tempy++;
                 if(tempy==highY+1)
                     tempx++;
                     tempy=0;
                 output[tempx][tempy]='*';
             System.out.println();
               for (int I=0; I<n+1; I++)
                      for (int J=0; J<highY+1; J++)
                          System.out.print(output[I][J]);
                          System.out.println();
    }Grid is the grid of the values, and for the [x][1] is all the Y values going down, [x][0] is the X values. the x would go up to however many values there are. Again, i know the code above is wrong.
    Any thoughts or suggestions would be appreciated. Also, if you need any more info from what i have done please ask.

    Big El,
    I think you can ditch the RGB To Color block, if that's what you're using.
    If I understand your description, what you need to do is use Interleave 1D Arrays to combine your three sine arrays together into one long array. Then use Draw Flattened Pixmap to turn the array into a picture. You can use Bundle By Name to stick your interleaved array into the image data cluster, and also make sure to specify 24-bit image depth and an appropriate bounding box.
    Hope that makes sense,
    John

  • I'm a character error problem in firefox. How can I solve.

    I'm a character error problem in firefox. How can I solve.

    Hello haruntt, please be a little more specific about the issue. In order to better assist you with your issue please provide us with a screenshot. If you need help to create a screenshot, please see [[How do I create a screenshot of my problem?]]
    Once you've done this, attach the saved screenshot file to your forum post by clicking the '''Browse...''' button below the ''Post your reply'' box. This will help us to visualize the problem.
    Thank you!

  • OO4 character Display Problem

    Hi All,
    I am using Oracle OLE objects to connect to Oracle 9.2 DB
    The NLS Language for the Database is AMERICAN_AMERICA.AR8ISO8859P6
    the client is using also the same NLS.
    When I retrive the information using OO4 some varchar2(Arabic Language) are return incorrectly from the DB, however when I use ADO on the same clinet it's correctly retrive all Data.
    I've only on home at my PC, and its NLS language is correct, even when I use SQL*Pluse the characters are displayed correctly.
    I also retrieved the NLS_LANG from the application using "SELECT USERENV ('LANGUAGE') FROM DUAL"
    and it was correct using ADO and OO4.
    Of course I've checked the registry value NLS_LANG and it's also AMERICAN_AMERICA.AR8ISO8859P6
    Any Help????

    I think after all it is a problem with the either:
    a. OS installer, which should really try to delete fonts which are duplicated by new system fonts
    b. OS which should give precedence to new system fonts over old system fonts where there are two the same, especially when the system fonts are in /System/Library/Fonts and the old ones are in /Library/Fonts
    The major annoyance is that this problem appeared with Leopard and took me hours of searching to figure out its origin - when you don't know if it is a character entry problem (which I thought) or a character display problem (which it turned out to be). In the end the fix was simple - delete the superceded fonts.

  • Character display problem latin characters

    For some reason old fonts show latin characters when i type "o/" and "l/". The problem seems confined to old fonts (at least Geneva; Monaco; New York), so I guess it is a bug?

    I think after all it is a problem with the either:
    a. OS installer, which should really try to delete fonts which are duplicated by new system fonts
    b. OS which should give precedence to new system fonts over old system fonts where there are two the same, especially when the system fonts are in /System/Library/Fonts and the old ones are in /Library/Fonts
    The major annoyance is that this problem appeared with Leopard and took me hours of searching to figure out its origin - when you don't know if it is a character entry problem (which I thought) or a character display problem (which it turned out to be). In the end the fix was simple - delete the superceded fonts.

  • Character display problem

    Hi All,
    When ever capital 'w' (i.e W, plain,Dialog and size 11) is first character in a text field left upper portion is not visible.But if I change any of these properties( i.e plain,Dialog, size 12) there is no problem.
    Can any body suggest me how can I display 'W' correctly with these propeties(i.e W, plain,Dialog and size 11).
    Thanks in advance

    I think after all it is a problem with the either:
    a. OS installer, which should really try to delete fonts which are duplicated by new system fonts
    b. OS which should give precedence to new system fonts over old system fonts where there are two the same, especially when the system fonts are in /System/Library/Fonts and the old ones are in /Library/Fonts
    The major annoyance is that this problem appeared with Leopard and took me hours of searching to figure out its origin - when you don't know if it is a character entry problem (which I thought) or a character display problem (which it turned out to be). In the end the fix was simple - delete the superceded fonts.

Maybe you are looking for