Results 1 to 4 of 4
  1. #1
    jdubya is offline Member
    Join Date
    Apr 2014
    Posts
    9
    Rep Power
    0

    Default Need help converting C++ code to Java

    Hello all. First post on this forum. I only have experience in C++ and a little bash. I found a Java bubble sort online and I tried to use it as a guide to convert my C++ code to Java. I'm not using an IDE. I downloaded a Java compiler for my Linux terminal. I'm using a bubble sort algorithm here and reading from an array. You can see on line 2 and 3 how I'm compiling and running it. Line 5, "package test;" not sure if that is what I need there. Here's the code:

    Java Code:
    // BubbleSort Java
    // compile with:  javac BubbleSort.java
    // run with: java BubbleSort
    
    package test;
    
    import java.util.Arrays;
    
    public class BubbleSort {
        
        private static int swapHolder = 0;
        private static int sizeOfArray = 10; 
        private static int pass = 0;
    
      
        public static void main(String args[]) {
            int[] myArray = {44,9,237,1,5,34,534,17,21,819};
            bubbleSort(myArray);
        }   
      
        public static void bubbleSort(int[] myArray){
              
            while(pass < sizeOfArray) {
                for(int j=0; j < sizeOfArray - pass; j++) {
    	       if(myArray[j] > myArray[j+1]) { // needs to be swapped
    	          swapHolder = myArray[j+1];
    		  myArray[j+1] = myArray[j]; // this makes both the same
    		  myArray[j] = swapHolder;
    	       }
    	    } // end of pass
            pass++;
            }
            //**first pass puts greatest number in last index**
            //cout << "The array sorted from lowest to highest:" << endl;
            System.out.printf("The array sorted from lowest to highest:\n");
            for(int j=0; j < sizeOfArray; j++){
               //cout << myArray[i] << " " ;
               System.out.printf(myArray[j] + " ");
            }
        }
    }
    I compiles but I get these errors when I run it:

    Exception in thread "main" java.lang.NoClassDefFoundError: BubbleSort (wrong name: test/BubbleSort)
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java :800)
    at java.security.SecureClassLoader.defineClass(Secure ClassLoader.java:142)
    at java.net.URLClassLoader.defineClass(URLClassLoader .java:449)
    at java.net.URLClassLoader.access$100(URLClassLoader. java:71)
    at java.net.URLClassLoader$1.run(URLClassLoader.java: 361)
    at java.net.URLClassLoader$1.run(URLClassLoader.java: 355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.j ava:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:4 25)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launche r.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:3 58)
    at sun.launcher.LauncherHelper.checkAndLoadMain(Launc herHelper.java:482)
    Last edited by jdubya; 04-12-2014 at 06:42 PM. Reason: originally called errors "compile errors" It compiles but I get the errors when I execute

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,573
    Blog Entries
    7
    Rep Power
    21

    Default Re: Need help converting C++ code to Java

    If your class 'BubbleSort' is in the package 'test', the complete class name is test.BubbleSort and you should run it as such. You're not doing anything with the classpath environment variable so your current working directory should be the parent dir of the 'test' directory where the BubbleSort.class file is stored. For starters you could remove that package statement, but you'll meet the same troubles sooner than later ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    jdubya is offline Member
    Join Date
    Apr 2014
    Posts
    9
    Rep Power
    0

    Default Re: Need help converting C++ code to Java

    Thanks for the reply. This is where my class is located: jdubya@JLnotebook ~/csc290/bin $
    What exactly should my package be?

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,573
    Blog Entries
    7
    Rep Power
    21

    Default Re: Need help converting C++ code to Java

    Quote Originally Posted by jdubya View Post
    Thanks for the reply. This is where my class is located: jdubya@JLnotebook ~/csc290/bin $
    What exactly should my package be?
    If your package name is 'test', your class should be stored in ~/csc290/bin/test and you should run it from the dir ~/csc290/bin.

    kind regards,

    Jos

    ps. there's a compiler flag that makes the compiler create the correct directories for you; see 'javac -help'.
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. converting java into c# code
    By nat in forum New To Java
    Replies: 3
    Last Post: 12-14-2013, 08:32 PM
  2. converting simple C code to Java
    By sbutt in forum New To Java
    Replies: 4
    Last Post: 10-25-2011, 06:15 PM
  3. My code for converting temperatures...
    By king4ever in forum New To Java
    Replies: 8
    Last Post: 01-18-2011, 04:27 AM
  4. Converting String to Usable code
    By xael in forum New To Java
    Replies: 4
    Last Post: 09-21-2010, 09:08 PM
  5. When converting code into an iPhone app...
    By Gio D. in forum New To Java
    Replies: 0
    Last Post: 03-16-2010, 07:36 PM

Tags for this Thread

Posting Permissions

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