Thread: Help converting a java program to C

  1. #1
    Registered User
    Join Date
    Aug 2012
    Posts
    22

    Talking Help converting a java program to C

    I am trying to convert the following program to C and actually understand it. This is an introductory type program for the beginner that is me. In my intro to C class we have just barely got to if statements and it would be fantastic if I could get this program working in time for a little fantasy football. If someone could take the time and help me through this and give me guidance from square one (especially with inputting external files into my program) I would be forever in their debt (ok not forever but it would be greatly appreciated).

    Anyways, here is the code in java:

    Code:
    import java.io.*;
    import java.util.*;
    
    public class FootballPlayer implements Comparable<FootballPlayer> {
        
        // Components of a football player.
        private String name;
        private String position;
        private int powerRanking;
        
        // Creates a Football player object with all the necessary statistics.
        public FootballPlayer(String n, String p, int pass, int rush, int rec, int TD) {
            name = n;
            position = p;
            powerRanking = pass/30 + (rush+rec)/10 + 6*TD;
        }
        
        // Here is how we want a player to print out.
        public String toString() {
            return name + " " + position + " " + powerRanking;
        }
        
        public int compareTo(FootballPlayer other) {
            
            // First sort by power ranking.
            if (this.powerRanking != other.powerRanking)
                return other.powerRanking - this.powerRanking;
            
            // Break ties by name.
            return this.name.compareTo(other.name);
            
        }
        
        public static void main(String[] args) throws Exception {
            
            // Open file.
            Scanner stdin = new Scanner(System.in):
            System.out.println("What is the name of your input file?");
            String fileName = stdin.next();
            Scanner fin = new Scanner(new File(fileName));
            
            ArrayList<FootballPlayer> nfl = new ArrayList<FootballPlayer>();
            
            int numPlayers = fin.nextInt();
            
            // Go through the file.
            for (int i=0; i<numPlayers; i++) {
                
                // Get this player's stats.
                String name = fin.next();
                String position = fin.next();
                int pass = fin.nextInt();
                int rush = fin.nextInt();
                int rec = fin.nextInt();
                int TDs = fin.nextInt();
                
                // Add this guy to our list!
                FootballPlayer tmp = new FootballPlayer(name,position,pass,rush,rec,TDs);
                nfl.add(tmp);
            }
            
            // Sort and print.
            Collections.sort(nfl);
            
            for (int i=0; i<nfl.size(); i++)
                System.out.println(nfl.get(i));
                
            fin.close();
        }
    }
    Last edited by rpmischris; 08-09-2012 at 06:56 PM.

  2. #2
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    For all your string operations, you will want to read through this: Strings in C

    Your object will become a "struct" (structure)

    And you may want to look into how to declare a function in C language (You will need to slightly change the header of each function)

    (And, although a lot of people will spit at their screens after reading this) I like to search the msdn site for most things - Here is an overview of C style functions Functions (C)

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    You'll get a lot farther if you study some tutorials and slam out a bunch of practice code to understand the concepts, rather than asking for someone to hold your hand step by step.

    I'm not trying to be rude, but you're asking for a huge favor. If you're just learning "if" statements, then it's going to take a little time and a lot of practice before you're ready to master, for instance, character arrays (strings) and pointers. Do you expect someone here to stand by you and wait while you climb the learning curve of programming? It's not a quick journey, and people have their own things to do and their own skills to develop.

    Again, I'm not trying to be rude, but your request sounds something like this: "I just picked up a guitar and learned the names of the strings. If someone could walk me through playing Purple Rain, I would really appreciate it."

    But I wish you the best of luck. If you start this project and have any little questions along the way, I'll do my best to help you.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The above program would be used for a simple flat file database. In C, we use structs with struct.members for object manipulations (you might see words like records with fields in them in other languages, these are another names for the same things).

    It's usually best to prototype the structures, above main(), so every function will have access to that prototype.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    #define NSIZE 40
    #define POSIZE 15
    #define PLSIZE 100
    
    struct player {
       char name[NSIZE];
       char position[PSIZE];
       float power;
    };
    Then, in main(), you'll create an array of struct player players[PLSIZE], and fill it with the data from the file, make your changes and do your views and searches, using the data in the array. As changes are made, you'll write out the data to a new data file, delete the old data file, and rename the new data file, with the old data files, name.

    As players are deleted (they retire perhaps), you'll zero out their name, and your program will keep the empty records - but you'll never see them, or know that they're present, in any way.

    All of this is fairly basic programming, but the "flow" of the program has to be spot on. Without a fuller understanding of how C works, it will be impossible for you to do this, right now. You could learn by carefully reading over such a program's code, but it would mean very little to you, right now.

    Wait until you have a basic understanding of structs and arrays, and all the other building blocks of C logic, and THEN start your database program. You will want functions for:

    Adding a record, deleting a record, editing a record, searching for a record, sorting the array of records. All of this will be options in a menu() function, along with a Quit choice, as well.

    The data will be loaded from the file, without user input, every time the program starts, and written out (if it's changed in any way), every time the program ends.

    I'm writing such a program right now, to help me manage my DVD's.

  5. #5
    Registered User
    Join Date
    Aug 2012
    Posts
    22
    Quote Originally Posted by Matticus View Post
    You'll get a lot farther if you study some tutorials and slam out a bunch of practice code to understand the concepts, rather than asking for someone to hold your hand step by step.

    I'm not trying to be rude, but you're asking for a huge favor. If you're just learning "if" statements, then it's going to take a little time and a lot of practice before you're ready to master, for instance, character arrays (strings) and pointers. Do you expect someone here to stand by you and wait while you climb the learning curve of programming? It's not a quick journey, and people have their own things to do and their own skills to develop.

    Again, I'm not trying to be rude, but your request sounds something like this: "I just picked up a guitar and learned the names of the strings. If someone could walk me through playing Purple Rain, I would really appreciate it."

    But I wish you the best of luck. If you start this project and have any little questions along the way, I'll do my best to help you.
    Ya, you do sound rude but I understand what you are getting at. I found this in an introduction to java program example and just figured it to be simpler than how you put it. Thanks, I will just keep plucking away at it.

  6. #6
    Registered User
    Join Date
    Aug 2012
    Posts
    22
    Thanks for some awesome advice. I will hold off on it until I get further in my course and have more of an understanding of the basics of C.

  7. #7
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by rpmischris View Post
    Ya, you do sound rude but I understand what you are getting at. I found this in an introduction to java program example and just figured it to be simpler than how you put it. Thanks, I will just keep plucking away at it.
    My apologies, I just come across that way. Do you know java? That would be a big help - then you could go through the code and takes notes on the logic, which would help as a reference for when you started writing it in 'C'.

    If you don't know java, then (after you gain some experience per Adak's and my advice) you can try to figure out the logic without a reference, which is a great learning process on its own. Again, if you ever have questions, feel free to come and ask.

  8. #8
    Registered User
    Join Date
    Aug 2012
    Posts
    22
    I got through an intro to java course just fine but it wasn't very thorough and was more of a learn through control/data structures course. So the course was in java but more or less just a foundational programming course.

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Java (or any programming language), would help, but don't study java for this - it's clear what you do need, and that is more knowledge of C - and only more knowledge of C.

    The logic in the program is very basic. You don't even need to refer to that Java program, anymore. It's ready for the trash bin.

    YOU are in THE C HOUSE, now!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting Java code into C, I need help >.<
    By minidragon in forum C Programming
    Replies: 3
    Last Post: 05-26-2011, 04:30 AM
  2. Help Converting Java to C
    By handsomedan in forum C Programming
    Replies: 1
    Last Post: 03-16-2010, 09:17 PM
  3. Help converting array program to link list program
    By hsmith1976 in forum C++ Programming
    Replies: 0
    Last Post: 02-14-2010, 09:50 PM
  4. Replies: 6
    Last Post: 08-07-2003, 02:05 PM
  5. converting from java
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 12-05-2001, 11:17 AM