AppletTalk.com Forum Index AppletTalk.com
Java discussions newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Need help with final part of my assignment

 
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java Help
View previous topic :: View next topic  
Author Message
x1xsup3rm4nx1x
Guest





PostPosted: Tue Jul 20, 2004 7:39 pm    Post subject: Need help with final part of my assignment Reply with quote



Ok, i finally think i got my assignment done, but i keep getting an error
saying i need a return statement. I dont think i need to put one 3 times
in a method, so if anyone can help me find the solution, i would really
appreciate it.

This is just for reference, here is my ListTest file that was given for
the assignment.

____________________________________________________________
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
/*
* Created on Jul 14, 2004
*
* Tests phone entry list and perform incremental search on the list
* Press ESC key to quit the program
*
* @author Prabu
*
* Asg4
*/

public class ListTest extends JPanel
implements KeyListener,
ActionListener
{
static JFrame frame;
JTextArea displayArea;
JTextField typingArea;
JButton button;
String keyString = "";
static List phoneList;
boolean eraseInput = true;

static final String newline = "n";

public ListTest()
{
super(new BorderLayout());

button = new JButton("Clear");
button.addActionListener(this);

typingArea = new JTextField(20);
typingArea.addKeyListener(this);

displayArea = new JTextArea();
displayArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(displayArea);
scrollPane.setPreferredSize(new Dimension(375, 125));

add(typingArea, BorderLayout.PAGE_START);
add(scrollPane, BorderLayout.CENTER);
add(button, BorderLayout.PAGE_END);
}

/** Handle the button click. */
public void actionPerformed(ActionEvent e)
{
//Clear the text components.
typingArea.setText("");

//Return the focus to the typing area.
typingArea.requestFocusInWindow();
}

/** Handle the key typed event from the text field. */
public void keyTyped(KeyEvent e)
{
displayInfo(e, "KEY TYPED: ");
}

/** Handle the key pressed event from the text field. */
public void keyPressed(KeyEvent e)
{
//displayInfo(e, "KEY PRESSED: ");
}

/** Handle the key released event from the text field. */
public void keyReleased(KeyEvent e)
{
//displayInfo(e, "KEY RELEASED: ");
}

protected void displayInfo(KeyEvent e, String s)
{
//You should only rely on the key char if the event
//is a key typed event.
int id = e.getID();
if (id == KeyEvent.KEY_TYPED)
{
char c = e.getKeyChar();
if (c == 'u001B') // when ESC key is pressed
{
frame.dispose(); // close the frame (window)
}
keyString += c;
int matches = phoneList.matchCount(keyString);
if (matches <= 1)
{
String outputString;
if (matches == 1)
{// unique match
Entry entity = phoneList.getEntry(keyString);
if (entity != null)
{
outputString = entity.getName() + " " + entity.getPhoneNumber();
}
else
{
outputString = "Error in Array List content";
}
}
else
{
outputString = "No match is found";
}
displayArea.append(newline + outputString + newline);
displayArea.setCaretPosition(displayArea.getDocument().getLength());
button.doClick();
keyString = "";
eraseInput = true;
}
else
{ // no. of matches is more than one -- accept additional character
if (eraseInput)
{ // erase the last character of the previous input string
eraseInput = false;
button.doClick();
}
}
}
}

/**
* Create the GUI and show it.
*/
private static void createAndShowGUI()
{
//Make sure we have nice window decorations.
JFrame.setDefaultLookAndFeelDecorated(true);

//Create and set up the window.
frame = new JFrame("Incremental List Search [press ESC to quit]");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

//Create and set up the content pane.
JComponent newContentPane = new ListTest();
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);

//Display the window.
frame.pack();
frame.setVisible(true);
}

public static void main(String[] args) throws IOException
{
phoneList = new List();
loadEntries("phone.txt", phoneList);
displayEntries(phoneList); // for debugging purpose

System.out.println("Phone list search...");

//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable()
{
public void run()
{
createAndShowGUI();
}
});
}

public static void loadEntries(String fileName, List phList) throws
IOException
{
BufferedReader inpFile = new BufferedReader(new FileReader(fileName));

String lineText, keyValue, phoneNr;
int entryCount = 0;

/*
* Assume that each entry is stores as a single line on the file
* The first value contains the entry name
* The next value corresponds to the associated phone number
* There is no blank line in the file
*/
while ((lineText = inpFile.readLine()) != null )
{
String values[] = lineText.split(" ");
keyValue = values[0];
phoneNr = values[1];
Entry phEntry = new Entry(keyValue, phoneNr);
phList.add(phEntry);
entryCount++;
}
inpFile.close();
System.out.println("No. of entries read = " + entryCount);
}

public static void displayEntries(List phList) throws IOException
{
System.out.println("nLoaded phone list entries:");

for (int i= 0; i < phList.getSize(); i++ )
{
Entry ph = phList.getEntry(i);
if (ph != null)
System.out.println(ph.getName() + " " + ph.getPhoneNumber());
}
System.out.println();
}

}
____________________________________________________________

ok, here is my Entry.java file, i think im finished with this, and it
compiles with no errors.

____________________________________________________________

public class Entry
{
public static String phone;
public static String name;
public static boolean namePre;
public static int c;

public Entry(String a, String b)
{
name = a;
phone = b;
}

public boolean namePrefix(String c)
{

for(int i = 1; i < getName().length(); i++)
{
if(getName().substring(1,i).equalsIgnoreCase(c))
{
return true;
}

return false;
}
return true;
}

public String getName()
{
return name;
}
public String getPhoneNumber()
{
return phone;
}


}
____________________________________________________________

Now this is what i need help on, it is my List.java file, i keep getting a
missing return statement error when i compile, i think i have messed up
somewhere, but im not sure where. The error keeps comming up in the last 2
methods, the Entry getEntry(String...) and the Entry getEntry(int). If
someone could help me and tell me what im doing wrong, or tell me how to
fix it, i would appreciate it.
____________________________________________________________

import java.util.ArrayList;

public class List
{
private ArrayList numbers;



public List()
{
numbers = new ArrayList();
}

public void add(Entry a)
{
numbers.add(a);
}
public int matchCount(String b)
{
int matches = 0;
int z = 0;
for (int i = 0; i < numbers.size(); i++)
{
Entry ok = (Entry)numbers.get(i);
if(ok.getName().substring(z, z+1).equalsIgnoreCase(b))
{
matches++;
}

return matches;
}


}
public int getSize()
{
return numbers.size();
}
public Entry getEntry(String c)
{
int z = 0;
for (int i = 0; i < numbers.size(); i++)
{
Entry ok = (Entry)numbers.get(i);
if (ok.getName().substring(z, z+1).equalsIgnoreCase(c))
{
z = 0;
return ok;
}


}
}

public Entry getEntry(int d)
{
for(int i = 0; i < numbers.size(); i++)
{
Entry ok2 = (Entry)numbers.get(d);
return ok2;
}



}

}

Back to top
Andrew Thompson
Guest





PostPosted: Tue Jul 20, 2004 7:59 pm    Post subject: Re: Need help with final part of my assignment Reply with quote



On Tue, 20 Jul 2004 15:39:54 -0400, x1xsup3rm4nx1x wrote:

Quote:
Ok, i finally think i got my assignment done,

You have an interesting interpretation
of 'done'. It does not compile yet, so
I assume you have not yet uncovered all
the bugs that will be revealed in your
*testing*, ;-)

Quote:
..but i keep getting an error
saying i need a return statement.

That is because you need a return statement, as
I believe at least one person has tried to explain..

Look at this method..
...
// first - please stop pasting 'tabs' in code
public int matchCount(String b)
{
// 'matches' is a poor name for this attribute
// 'index' would be much better
int matches = 0;
int z = 0;
for (int i = 0; i < numbers.size(); i++)
{
Entry ok = (Entry)numbers.get(i);
if( ok.getName().substring(z, z+1).equalsIgnoreCase(b) )
{
matches++;
}
return matches;
}
// if the code gets to here it means 'b' does
// NOT MATCH ANYTHING, so
// ..what are you gonna *do* now?
return -1;
}
....

HTH

--
Andrew Thompson
http://www.PhySci.org/ Open-source software suite
http://www.PhySci.org/codes/ Web & IT Help
http://www.1point1C.org/ Science & Technology

Back to top
Bryce
Guest





PostPosted: Tue Jul 20, 2004 8:07 pm    Post subject: Re: Need help with final part of my assignment Reply with quote



On Tue, 20 Jul 2004 15:39:54 -0400, "x1xsup3rm4nx1x"
<x1xsup3rm4nx1x (AT) hotmail (DOT) com> wrote:

Quote:
public Entry getEntry(String c)
{
int z = 0;
for (int i = 0; i < numbers.size(); i++)
{
Entry ok = (Entry)numbers.get(i);
if (ok.getName().substring(z, z+1).equalsIgnoreCase(c))
{
z = 0;
return ok;
}


}
}


That's because the compiler doesn't know what to return if
numbers.size() == 0 or ok.getName().substring(z,
z+1).equalsIgnoreCase(c) returns false...


Quote:
public Entry getEntry(int d)
{
for(int i = 0; i < numbers.size(); i++)
{
Entry ok2 = (Entry)numbers.get(d);
return ok2;
}



}

See above. what if numbers.size() is 0? No return statement.

--
now with more cowbell

Back to top
Roedy Green
Guest





PostPosted: Tue Jul 20, 2004 9:38 pm    Post subject: Re: Need help with final part of my assignment Reply with quote

On Tue, 20 Jul 2004 15:39:54 -0400, "x1xsup3rm4nx1x"
<x1xsup3rm4nx1x (AT) hotmail (DOT) com> wrote or quoted :

Quote:
Now this is what i need help on, it is my List.java file, i keep getting a
missing return statement error when i compile,

If you want others to help you, it makes it a lot easier if you
include the error message at the troublesome spot.

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.

Back to top
Roedy Green
Guest





PostPosted: Tue Jul 20, 2004 9:41 pm    Post subject: Re: Need help with final part of my assignment Reply with quote

On Tue, 20 Jul 2004 15:39:54 -0400, "x1xsup3rm4nx1x"
<x1xsup3rm4nx1x (AT) hotmail (DOT) com> wrote or quoted :

Quote:
public int matchCount(String b)
{
int matches = 0;
int z = 0;
for (int i = 0; i < numbers.size(); i++)
{
Entry ok = (Entry)numbers.get(i);
if(ok.getName().substring(z, z+1).equalsIgnoreCase(b))
{
matches++;
}

return matches;
}


What if numbers.size() was 0? Then you would fall out the bottom of
the loop without returning an answer.

Further your code does not make sense. You return immediately whether
the first item matches or not.

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.

Back to top
x1xsup3rm4nx1x
Guest





PostPosted: Wed Jul 21, 2004 12:42 am    Post subject: Re: Need help with final part of my assignment Reply with quote

I fixed it where it would compile the program, but when you run the
listTest.java file, it only loads the last name on the phone list that it
takes. Im not sure why, but it loads the name 5 times, instead of 5
different names. Can anyone help me with why it loads that way?

here is my updated files

Entry. java

public class Entry
{
public static String phone;
public static String name;
public static boolean namePre;
public static int c;

public Entry(String a, String b)
{
name = a;
phone = b;
}

public boolean namePrefix(String c)
{

for(int i = 1; i < getName().length(); i++)
{
if(getName().substring(1,i).equalsIgnoreCase(c))
{
return true;
}

return false;
}
return true;
}

public String getName()
{
return name;
}
public String getPhoneNumber()
{
return phone;
}
}

and

List.java

import java.util.ArrayList;

public class List
{
private ArrayList numbers;



public List()
{
numbers = new ArrayList();
}

public void add(Entry a)
{
numbers.add(a);

}
public int matchCount(String b)
{
int matches = 0;
int x = 0;
for (int i = 0; i < numbers.size(); i++)
{
Entry ok = (Entry)numbers.get(i);
if(ok.getName().substring(x, x+1).equalsIgnoreCase(b))
{
matches++;
}

return matches;
}

return 0;
}
public int getSize()
{
return numbers.size();
}
public Entry getEntry(String c)
{
int z = 0;
for (int i = 0; i < numbers.size(); i++)
{
Entry ok = (Entry)numbers.get(i);
if (ok.getName().substring(z, z+1).equalsIgnoreCase(c))
{
return ok;
}
else return null;

}

return null;
}

public Entry getEntry(int d)
{
for(int i = 0; i < numbers.size(); i++)
{
Entry ok2 = (Entry)numbers.get(d);
return ok2;
}

return null;

}

}

i think it has something to do with the numbers.add() part. Im not quite
sure, but if you could help me i would appreciate it.

Back to top
Bryce
Guest





PostPosted: Wed Jul 21, 2004 3:42 pm    Post subject: Re: Need help with final part of my assignment Reply with quote

On Tue, 20 Jul 2004 20:42:34 -0400, "x1xsup3rm4nx1x"
<x1xsup3rm4nx1x (AT) hotmail (DOT) com> wrote:

Quote:
I fixed it where it would compile the program, but when you run the
listTest.java file, it only loads the last name on the phone list that it
takes. Im not sure why, but it loads the name 5 times, instead of 5
different names. Can anyone help me with why it loads that way?

Look, I don't want to be rude here. I know beginning programming can
be tough. But have you done any debugging?

Have you stepped through the code to see why it does what it does? Put
some System.out.println's in there to print information to the console
if you need to. These are simple basic debugging techniques that
SHOULD be taught in a programming class.

--
now with more cowbell

Back to top
x1xsup3rm4nx1x
Guest





PostPosted: Thu Jul 22, 2004 4:28 pm    Post subject: Re: Need help with final part of my assignment Reply with quote

no, he hasnt taught us any debugging

Back to top
Roedy Green
Guest





PostPosted: Thu Jul 22, 2004 5:07 pm    Post subject: Re: Need help with final part of my assignment Reply with quote

On Thu, 22 Jul 2004 12:28:08 -0400, "x1xsup3rm4nx1x"
<x1xsup3rm4nx1x (AT) hotmail (DOT) com> wrote or quoted :

Quote:
no, he hasnt taught us any debugging

there's always peppering your code with System.out.println

See http://mindprod.com/jgloss/debugging.html
http://mindprod.com/jgloss/debugger.html

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.

Back to top
Display posts from previous:   
Post new topic   Reply to topic    AppletTalk.com Forum Index -> Java Help All times are GMT
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.