11 Replies - 1803 Views - Last Post: 27 April 2014 - 11:42 AM Rate Topic: -----

#1 mattyeend   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 24-April 14

I need help writing some java code from pseudo code

Posted 27 April 2014 - 09:20 AM

So far, what I have is:
private int find(String name){
    for(int i = 0; i < this.size; i++)
         {
             if(
                name.equals
                (this.theDirectory[i],getName()))
                {
                    
                }
         }
    }


And I have:

public String lookUpEntry(String name) {
        if name found{
            getNumber = return;
        }
        else{
            return null;
        }
    }


The pseudo code I was given was:

use find to locate position of entry 
 if entry is found 
 use getNumber to return the telno 
 else 
 return null


I am aware that I need to find method before doing the lookUpEntry method. There are a few errors within the code and I'm unsure why this is so

Is This A Good Question/Topic? 0
  • +

Replies To: I need help writing some java code from pseudo code

#2 mike73   User is offline

  • D.I.C Addict
  • member icon

Reputation: 250
  • View blog
  • Posts: 918
  • Joined: 24-April 10

Re: I need help writing some java code from pseudo code

Posted 27 April 2014 - 09:48 AM

private int find(String name){
    for(int i = 0; i < this.size; i++)
         {
             if(
                name.equals
                (this.theDirectory[i],getName()))
                {
                    
                }
         }
    }



Firstly
06                (this.theDirectory[i],getName()))
You have a comma instead of a period before getName(). You have declared this method to return an int, so you need to do that.

Seconldy, post your errors and complete soruce code

This post has been edited by mike73: 27 April 2014 - 09:49 AM

Was This Post Helpful? 0
  • +
  • -

#3 mattyeend   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 24-April 14

Re: I need help writing some java code from pseudo code

Posted 27 April 2014 - 09:50 AM

Sorry

import java.io.File;
import java.io.PrintWriter;
import java.util.Scanner;

public class ArrayPhoneDirectory implements PhoneDirectory
{
    private static final int INIT_CAPACITY = 100; 
    private final int capacity = INIT_CAPACITY; 
 
    // holds telno of directory entries 
 
    private int size = 0; 

    // Array to contain directory entries 

    private final DirectoryEntry[] theDirectory = new DirectoryEntry[capacity]; 

    // Holds name of data file 

    private final String sourceName = "telnos.txt"; 

    // Flag to indicate whether directory was modified since it was last loaded or saved 

    private final boolean modified = false;
    private String telnumber;

    /**
     *
     * @param sourceName
     */
    public void loadData(String sourceName)
    {
        try
        {
            Scanner reader = new Scanner(new File(sourceName));
            while(reader.hasNext())
            {
                String name = reader.nextLine();
               
                String telno = reader.nextLine();
              
                this.theDirectory[this.size] = new DirectoryEntry(name, telno);
              
                this.size++;
                } 
            reader.close();
            }
        catch(Exception e)
        {
            System.out.printf("File not found%n");   
        }
        }
    
    public String lookUpEntry(String name) {
        if name found{
            getNumber = return;
        }
        else{
            return null;
        }
    }
    public String addChangeEntry(String name, String telno) {}
    public String removeEntry(String name) {}
    public void save() {}
    public String format() {}
    private int find(String name){
    for(int i = 0; i < this.size; i++)
         {
             if(
                name.equals
                (this.theDirectory[i].getName()))
                {
                    
                }
         }
    }
    private void add(String name, String telno){}
    private void reallocate(){}
    
}

This post has been edited by andrewsw: 27 April 2014 - 09:52 AM
Reason for edit:: Removed unnecessary previous quote

Was This Post Helpful? 0
  • +
  • -

#4 mattyeend   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 24-April 14

Re: I need help writing some java code from pseudo code

Posted 27 April 2014 - 10:02 AM

And the errors are on lines 62, 63, 64, 66, 70, 71, 73, 74
Was This Post Helpful? 0
  • +
  • -

#5 mike73   User is offline

  • D.I.C Addict
  • member icon

Reputation: 250
  • View blog
  • Posts: 918
  • Joined: 24-April 10

Re: I need help writing some java code from pseudo code

Posted 27 April 2014 - 10:34 AM

Let's start with the obvious:
55        if name found{
this is still pseudocode. What are you trying to do here?

This post has been edited by mike73: 27 April 2014 - 10:40 AM

Was This Post Helpful? 0
  • +
  • -

#6 mattyeend   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 24-April 14

Re: I need help writing some java code from pseudo code

Posted 27 April 2014 - 10:39 AM

I'm trying to find the name of that's already in a file. The folder this is in is called telephonedirectory, so I'm aiming to make a phone directory, and I'm stuck on this bit of it :P
Was This Post Helpful? 0
  • +
  • -

#7 mike73   User is offline

  • D.I.C Addict
  • member icon

Reputation: 250
  • View blog
  • Posts: 918
  • Joined: 24-April 10

Re: I need help writing some java code from pseudo code

Posted 27 April 2014 - 10:42 AM

Well it's not valid Java code, so it won't compile. What is the difference between that method and the one you have called find?

Otherwise:
62    public String addChangeEntry(String name, String telno) {}
63    public String removeEntry(String name) {}
65    public String format() {}

These 3 methods are declared to return a String, which you are not doing. If they are intended to just set a value, then declare their return type as void.
Was This Post Helpful? 0
  • +
  • -

#8 mattyeend   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 24-April 14

Re: I need help writing some java code from pseudo code

Posted 27 April 2014 - 10:48 AM

So this is what I have now

import java.io.File;
import java.io.PrintWriter;
import java.util.Scanner;

public class ArrayPhoneDirectory implements PhoneDirectory
{
    private static final int INIT_CAPACITY = 100; 
    private final int capacity = INIT_CAPACITY; 
 
    // holds telno of directory entries 
 
    private int size = 0; 

    // Array to contain directory entries 

    private final DirectoryEntry[] theDirectory = new DirectoryEntry[capacity]; 

    // Holds name of data file 

    private final String sourceName = "telnos.txt"; 

    // Flag to indicate whether directory was modified since it was last loaded or saved 

    private final boolean modified = false;
    private String telnumber;

    /**
     *
     * @param sourceName
     */
    public void loadData(String sourceName)
    {
        try
        {
            Scanner reader = new Scanner(new File(sourceName));
            while(reader.hasNext())
            {
                String name = reader.nextLine();
               
                String telno = reader.nextLine();
              
                this.theDirectory[this.size] = new DirectoryEntry(name, telno);
              
                this.size++;
                } 
            reader.close();
            }
        catch(Exception e)
        {
            System.out.printf("File not found%n");   
        }
        }
    
    public String lookUpEntry(String name) {
        if name found{
            getNumber = return;
        }
        else{
            return null;
        }
    }
    public String addChangeEntry(String name, String telno) {
        return null;
    }
    public String removeEntry(String name) {
        return null;
    }
    public void save() {}
    public String format() {
        return null;
    }
    private int find(String name){
    for(int i = 0; i < this.size; i++)
         {
             if(
                name.equals
                (this.theDirectory[i].getName()))
                {
                    
                }
         }
    }
    private void add(String name, String telno){}
    private void reallocate(){}
    
}


I want to get the find and the lookUpEntry methods done before the other few
Was This Post Helpful? 0
  • +
  • -

#9 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12800
  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

Re: I need help writing some java code from pseudo code

Posted 27 April 2014 - 10:54 AM

If the find() method is supposed to return the index of the element once you've found it, I'd just return i once you have found a match. If you don't find a match, the convention is to return -1.
Was This Post Helpful? 0
  • +
  • -

#10 mattyeend   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 24-April 14

Re: I need help writing some java code from pseudo code

Posted 27 April 2014 - 11:24 AM

How do I do that? :/
Was This Post Helpful? 0
  • +
  • -

#11 macosxnerd101   User is offline

  • Games, Graphs, and Auctions
  • member icon




Reputation: 12800
  • View blog
  • Posts: 45,992
  • Joined: 27-December 08

Re: I need help writing some java code from pseudo code

Posted 27 April 2014 - 11:40 AM

I pretty much just spelled it out for you. Read your find() method. When have you found a match? What section of code handles that?
Was This Post Helpful? 0
  • +
  • -

#12 mattyeend   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 24-April 14

Re: I need help writing some java code from pseudo code

Posted 27 April 2014 - 11:42 AM

Oh right, yeah, thanks :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1