Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


[Java] Help needed

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
superfirepig
Master Cheater
Reputation: 0

Joined: 07 Oct 2006
Posts: 313
Location: city of hackers

PostPosted: Fri Mar 20, 2009 3:33 pm    Post subject: [Java] Help needed Reply with quote

Q1:
I am new to java, and I am trying to do an exercise that involves array. The program takes in first, last and city names in x number of elements inside an array. I am stuck on choice 3 of the code; the program never goes into the if statement no matter what i try. Please take a look at my code.

Thanks

Q2:
And is it better to use two dimensional array in this type of problem?



Code:
import java.util.Scanner;

public class friendList {
    public static String[] cOne(){
        int n;
        Scanner input = new Scanner(System.in);
        System.out.println("Number of friends:");
        n = input.nextInt();
        String[] f = new String[n * 3];
        for(int i = 3; i <= n * 3; i += 3){
            System.out.println("Enter a First Name");
            f[i-3] = input.next();
            System.out.println("Enter a Last Name:");
            f[i-2] = input.next();
            System.out.println("Enter City:");
            f[i-1] = input.next();
        }
        return f;
    }
    public static void main(String[] args) {
        String[] f = null;
        int c = 0;
        int count = 1;
        int countOne = 1;
        while (c != 6){
        Scanner input = new Scanner(System.in);
        System.out.println("Friend List Main Menu");
        System.out.println("Choice 1: Make a new Friend List");
        System.out.println("Choice 2: Print existing friend list");
        System.out.println("Choice 3: Search for a friend in your current list");
        System.out.println("Choice 4: Add a friend to your existing list");
        System.out.println("Choice 5: Delete a friend to your existing list");
        System.out.println("Choice 6: Exit Friend List");
        System.out.println("Enter your choice:");
        c = input.nextInt();
       
        if (c == 1){
            f = cOne();
        }else if (c==2){
            if (f == null){
                System.out.println("You don't have friends...-_-\"");
                System.out.println("");
            }else {
                System.out.println("Current Friends list is:");
                for(int i = 3; i <= f.length; i += 3){
                    System.out.println("Friend #"+count+":");
                    System.out.println(f[i-3]+" "+f[i-2]+", who lives in "+f[i-1]);
                    count = count + 1;
                    System.out.println("");
                }
            }
        }else if (c==3){
            if (f!= null){
            System.out.println("Enter a friends first and last name:");
            String sf = input.next();
            String sl = input.next();
            for(int i = 3; i < f.length; i += 3){
                if (sf.equals(f[i-3]) && sl.equals(f[i-2])){
                    System.out.println("Found your friend...His/Her Friend Number is:"+count);
                    System.out.println(f[i-3]+" "+f[i-2]+", who lives in "+f[i-1]);
                    countOne += 1;
                }else{
                    System.out.println("Friend does not exist");
                }}
                }else{
                   
                }
        }else if (c==4){
               
        }else if (c==5){
               
            }else{
                System.out.println("GoodBye! o.0!");
                break;
            }
        }
    }

}

_________________
You don't have enough MP to f#@% me, please try again later
Back to top
View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger
smartz993
I post too much
Reputation: 2

Joined: 20 Jun 2006
Posts: 2013
Location: USA

PostPosted: Fri Mar 20, 2009 3:46 pm    Post subject: Reply with quote

Are you picking option 1 before option 3..? Or else it won't work.

Anyway, i wouldn't recommend using the same variable name for both strings, it gets confusing.
Back to top
View user's profile Send private message
superfirepig
Master Cheater
Reputation: 0

Joined: 07 Oct 2006
Posts: 313
Location: city of hackers

PostPosted: Fri Mar 20, 2009 4:47 pm    Post subject: Reply with quote

smartz993 wrote:
Are you picking option 1 before option 3..? Or else it won't work.

Anyway, i wouldn't recommend using the same variable name for both strings, it gets confusing.


Yes. that is the idea. first i store some information in to the array by using option 1, than i use option 2,3, and so on...hope that clears things up a bit...

And thanks for the advice i need to get rid of that habit, a lot of people say my codes are confusing.

_________________
You don't have enough MP to f#@% me, please try again later
Back to top
View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger
indiana
Newbie cheater
Reputation: 0

Joined: 29 Aug 2008
Posts: 24

PostPosted: Fri Mar 20, 2009 4:49 pm    Post subject: Reply with quote

Learn step-by-step or breakpoint debugging.

Or just put some dumps (like System.out.println-s) to see how the flow goes. When you have a problem you don't throw beans and try to guess it, you learn to debug.

And when you do that long enough, you'll be able to spot the errors very fast, and after that you'll also start to code with much less errors.

_________________
indiana
pokerai.org/pf3
Back to top
View user's profile Send private message
smartz993
I post too much
Reputation: 2

Joined: 20 Jun 2006
Posts: 2013
Location: USA

PostPosted: Fri Mar 20, 2009 6:36 pm    Post subject: Reply with quote

superfirepig wrote:
smartz993 wrote:
Are you picking option 1 before option 3..? Or else it won't work.

Anyway, i wouldn't recommend using the same variable name for both strings, it gets confusing.


Yes. that is the idea. first i store some information in to the array by using option 1, than i use option 2,3, and so on...hope that clears things up a bit...

And thanks for the advice i need to get rid of that habit, a lot of people say my codes are confusing.


I just tried it, it works fine..but the numbers are screwed up. It displays the wrong friend number.
Back to top
View user's profile Send private message
VolatileAce
Cheater
Reputation: 0

Joined: 19 Mar 2009
Posts: 30
Location: 0001001100110111

PostPosted: Sat Mar 21, 2009 7:23 pm    Post subject: Reply with quote

Suggestions on how you can improve on readability of your code:

- Not a requirement, but you should name your Java Class starting with capital letter. Its a "tradition".
- This would be a major requirement if you plan to succeed in Java: Program in Objects.
- Your Scanner Object might as well be a global variable.
- Some of your indentations are "wrong" (Non standard, resulting in hard to read codes. Example: 'Scanner input' under 'while (c != 6){')
- For 'if' and 'else' on integers or chars, use 'switch' and 'case' instead.

Now enough of my criticisms, here is how I would do it, I've provided few lines of comments to clarify what is being done at some sections.

Code:
/**
 * @(#)FriendList.java
 *
 *
 * @author VolatileAce
 * @version 1.00 2009/3/21
 */

//Arrays is simply a static final class that lets you sort and search arrays.
import java.util.Arrays;
import java.util.Scanner;

//Capitalized F
public class FriendList{
//Static.
    //Make Scanner global.
    private static Scanner in;
    private static FriendList myFriends;
    private static int selection;
    public static void main(String args[]){
        in = new Scanner(System.in);
        myFriends = null;
        selection = -1;
        //Do-while loop. Its basically a while loop except that it always runs at least once.
        do{
            System.out.println("Friend List Main Menu:");
            System.out.println("Choice 1: Make a new Friend List.");
            System.out.println("Choice 2: Print existing friend list.");
            System.out.println("Choice 3: Search for a friend in your current list.");
            System.out.println("Choice 4: Add a friend to your existing list.");
            System.out.println("Choice 5: Delete a friend to your existing list.");
            System.out.println("Choice 6: Exit Friend List");
            System.out.println("So what would you like to do? ");
            //Use switch case to 'if' 'else' integers.
            switch(selection = in.nextInt()){
                case 1: myFriends = new FriendList(); break;
                case 2: checkPrint(myFriends.toString()); break;
                case 3: checkPrint(myFriends.search()); break;
                case 4: checkPrint(myFriends.addFriend()); break;
                case 5: checkPrint(myFriends.removeFriend()); break;
                case 6: System.out.println("GoodBye! o.0!"); break;
                default: System.out.println("Please choose from 1 of the 6 options provided.\n");
            }
        }while(selection != 6);
    }

    //It'll print out "But you don't have any friends!" if myFriends is null, else it'll print out the message parameter.
    //The (condition) ? return value if true : return value if false; is a "Tentative operation", partly used for neat code and partly for laziness.
    private static final void checkPrint(String message){
        System.out.println((myFriends == null) ? "But you don't have any friends!" : message);
    }
   
//Non-Static; everything below here are Objects.

    private Friend[] friends;
   
    //When creating a new FriendList, you automatically ask the user to input friends.
    private FriendList(){
        friends = new Friend[]{};
        addFriend();
    }
   
    //Notice the overloading addFriend methods.
    public final String addFriend(){
        System.out.println("How many friends would you like to add? ");
        int numFriends = in.nextInt();
        for(int i = 1; i <= numFriends; i++){
            String first, last, city;
            System.out.println("Please enter friend #" + i + "'s first name: ");
            first = in.next();
            System.out.println("Please enter friend #" + i + "'s last name: ");
            last = in.next();
            System.out.println("Please enter friend #" + i + "'s city: ");
            city = in.next();
            addFriend(first, last, city);
        }
        return "";
    }
   
    public final void addFriend(String firstName, String lastName, String city){
        addFriend(new Friend(firstName, lastName, city));
    }
   
    //Since arrays are fix in size after being made, you must make a new array and copy elements over to "resize" the array.
    //Also notice I sort the array with Arrays.sort(), that uses a modified merge sort to quickly sort the array.
    public final void addFriend(Friend f){
        Friend[] temp = new Friend[friends.length + 1];
        for(int i = 0; i < friends.length; i++)
            temp[i] = friends[i];
        temp[friends.length] = f;
        Arrays.sort(temp, null);
        friends = temp;
    }
   
    //A binary search is, in short, much faster (the larger the list the faster) then the O(n) time used with a standard looping search approach.
    private final int binarySearch(String name){
        return Arrays.binarySearch(friends, name, null);
    }
   
    public final String search(){
        System.out.println("Please enter your friend's last name: ");
        int result = binarySearch(in.next());
        return (result < 0) ? "Your friend does not exist in the Friend List." : friends[result].toString();
    }
   
    //This finds the index.
    public final String removeFriend(){
        System.out.println("Please enter your friend's last name: ");
        int result = binarySearch(in.next());
        return (result < 0) ? "Your friend does not exist in the Friend List." : removeFriend(result);
    }
   
    //This removes it, but if you already knew the index, you can just call this one.
    public final String removeFriend(int index){
        Friend f = friends[index];
        Friend[] temp = new Friend[friends.length - 1];
        for(int i = 0, j = 0; i < temp.length; i++)
            if(i != index)
                temp[j++] = friends[i];
        friends = temp;
        return "'" + f.getLastName() + ", " + f.getFirstName() + "' has been removed from the Friend List.";
    }
   
    //Overriding the toString method inherited, you'll learn inheritance once you get into objects.
    public final String toString(){
        String s = "";
        for(Friend f : friends)
            s += f.toString() + "\n";
        return s;
    }
   
    //Inner class Friend, I didn't want to make 2 files so I dumped it in here.
    //Basically by implementing Comparable and its methods, I define how it'll be sorted for the sorting used above.
    private final class Friend implements Comparable<Friend>{
        private final String firstName;
        private final String lastName;
        private final String city;
        public Friend(String firstName, String lastName, String city){
            this.firstName = firstName;
            this.lastName = lastName;
            this.city = city;
        }
        public final String getFirstName(){return firstName;}
        public final String getLastName(){return lastName;}
        public final String getCity(){return city;}
        public final String toString(){
            return "[" + lastName + ", " + firstName + "] : " + city;
        }
        public final int compareTo(Friend o){
            return lastName. compareTo( o.lastName);
        }
        //instanceof simply compare the object in question if its of the type after the keyword, in this case, it's asking if o is a "Friend" Object.
        public final boolean equals(Object o){
            return o instanceof Friend && lastName.equals(((Friend)o).lastName);
        }
    }
}


Hope this helps. Feel free to PM me for further help.
Back to top
View user's profile Send private message
superfirepig
Master Cheater
Reputation: 0

Joined: 07 Oct 2006
Posts: 313
Location: city of hackers

PostPosted: Thu Mar 26, 2009 8:41 am    Post subject: Reply with quote

Quote:
Suggestions on how you can improve on readability of your code:

- Not a requirement, but you should name your Java Class starting with capital letter. Its a "tradition".
- This would be a major requirement if you plan to succeed in Java: Program in Objects.
- Your Scanner Object might as well be a global variable.
- Some of your indentations are "wrong" (Non standard, resulting in hard to read codes. Example: 'Scanner input' under 'while (c != 6){')
- For 'if' and 'else' on integers or chars, use 'switch' and 'case' instead.

Now enough of my criticisms, here is how I would do it, I've provided few lines of comments to clarify what is being done at some sections.


Thanks a lot man! Very Happy +rep

_________________
You don't have enough MP to f#@% me, please try again later
Back to top
View user's profile Send private message AIM Address Yahoo Messenger MSN Messenger
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites