Java Pls Help!!!

Get help on programming - C++, Java, Delphi, etc.
Tiza
Registered User
Posts: 1486
Joined: 01 Dec 2004, 02:00
Location: At your window
Contact:

Post by Tiza »

Yes the Char method is the one I used back in the day
Image
I am a guy so please people stop sending me these shady pm's and start playing computer games or bodybuilding. Forums aren't a good place to get chicks the gym is...
Tiza
Registered User
Posts: 1486
Joined: 01 Dec 2004, 02:00
Location: At your window
Contact:

Post by Tiza »

Is there a simpler way of writing the code?

I cant get that to work??? :oops:
Image
I am a guy so please people stop sending me these shady pm's and start playing computer games or bodybuilding. Forums aren't a good place to get chicks the gym is...
Hex_Rated
Registered User
Posts: 3679
Joined: 19 Jan 2006, 02:00
Contact:

Post by Hex_Rated »

Rusty's code works, here is a class with the code inside:

Code: Select all

public class Main {
    
    /** Creates a new instance of Main */
    public Main() {
        System.out.println(reverse("Hello"));
    }
    
    
    public String reverse(String input){
       if(input==null||input.length()==0)
         return "";

       String retval = "";
       for(int i = input.length()-1;i>=0;i--)
            retval += input.charAt(i);
       return retval;
    } 
    
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        new Main();
    }
    
}
DFI LanParty X48 LT-2TR
Intel Q9450 @ 3.2Ghz
Dell 24" 2408WFP | Phillips 37" 1080p
Sapphire HD4870 X2 2GB
4GB Corsair DDR-2 1066 | Thermalrite 120 Ultra Extreme | G9 Mouse | G15 Keyboard
Vista Ultimate x64
CountZero
Registered User
Posts: 307
Joined: 09 Nov 2005, 02:00
Location: Behind you!

Post by CountZero »

Reverse the elements in an array of n elements. When it is finished, the element at a[0] will be exchanged with a[n-1], a[1] exchanged with a[n-2], etc.

Code: Select all

void reverse(int a[], int n) {
    // function: Reverses the elements of a.
    // parameters:
    //    a    inout  array of values.
    //    n    in     number of values in a, a[0]..a[n-1]
    // returns: nothing
   
    for (int i=0; i<n/2; i++) {
        int temp = a[i];
        a[i]     = a[n-i-1];
        a[n-i-1] = temp;
    }
    return;
}
Another way to understand it better would be:

Code: Select all

void reverse(int a[], int n) {
    int head = 0;   // index of first element
    int tail = n-1; // index of last element
    while (head < tail) {      
        int temp = a[head];
        a[head]  = a[tail];
        a[tail]  = temp;
        head++;
        tail--;
    }
    return;
}
Hope this helps?

BTW. Rusty's code works!
Tiza
Registered User
Posts: 1486
Joined: 01 Dec 2004, 02:00
Location: At your window
Contact:

Post by Tiza »

Ok thanks guys.

I am reading information in from a file the I want to insert something

I have this code so far

Code: Select all

public void Insert(){
		
		int pos = Integer.parseInt(JOptionPane.showInputDialog("Please enter the position"));
		
		for (int i = 0; i >=  pos; i -- ){
			
			strArr [i] = strArr [i -1];
			
			}
		
		strArr [pos] = JOptionPane.showInputDialog("Please enter the word you would like to insert");
		size ++;
		
It sometimes works and sometime the words are in different orders and stuff :cry:
Image
I am a guy so please people stop sending me these shady pm's and start playing computer games or bodybuilding. Forums aren't a good place to get chicks the gym is...
Tiza
Registered User
Posts: 1486
Joined: 01 Dec 2004, 02:00
Location: At your window
Contact:

Post by Tiza »

Never mind guys dumb mistake on my part.

The int i = 0, it should be int i = size...

Man I must be fatiguing :(

After my IT exam today I realized that Eclipse is total trash. The pc i used froze every ten minutes and Eclipse bomed out so many times, i spent more time waiting for Eclipse to load than programming, no joke.

At home I use Netbeans is that the best free program available?
Image
I am a guy so please people stop sending me these shady pm's and start playing computer games or bodybuilding. Forums aren't a good place to get chicks the gym is...
Post Reply