d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Need Help On A Simple Assignment
Add Reply New Topic New Poll
Member
Posts: 17,596
Joined: Aug 26 2005
Gold: 20,600.00
Jan 22 2015 12:22pm
Code
import java.util.*;

public class PhoneBookEntry
{

ArrayList <String> user = new ArrayList <String>();
ArrayList <String> phone = new ArrayList <String>();

public void setUser(String contact)
{
user.add(contact);
}

public void setPhone(String number)
{
phone.add(number);
}

public String getUser()
{
for(String contact : user)
{
return (contact);
}
}

public String getPhone()
{
for(String number : phone)
{
return (number);
}
}

}


So I just need to simply take in a bunch of names/numbers and be able to return it. To my eyes and knowledge I am doing that but its still saying i need to add a return twice in here. What am I doing wrong here?

Thanks for the help in advance.
Member
Posts: 17,596
Joined: Aug 26 2005
Gold: 20,600.00
Jan 22 2015 01:09pm
Guess while I have a topic up...

Code
import java.io.*;
import java.util.*;

public class NumberAnalysis
{
private double low , high, avg, sum ;
private double[] numbers = new double[50];
int i = 0, count;
String file1;

public void openFile(String numberFile) throws IOException
{
File file = new File(numberFile);
Scanner in = new Scanner(file);

file1 = numberFile;

while(in.hasNext())
{
numbers[i] = in.nextDouble();
i++;
}

count = i;
}

public void setLow()
{
for(i = 0; i<10; i++)
{
if(numbers[i]<low)
low = numbers[i];
}
}

public void setHigh()
{
for(i = 0; i<10; i++)
{
if(numbers[i]>high)
high = numbers[i];
}
}

public void setAverage()
{
for(i = 0; i<10; i++)
{
sum = sum + numbers[i];
}
avg = sum/10;
}

public double getLow()
{
return low;
}

public double getHigh()
{
return high;
}

public double getAverage()
{
return avg;
}

public double getTotal()
{
return sum;
}

public double getValues()
{
return numbers[2];
}

}


it runs fine and my other file that runs can get numbers so it is saving the numbers into the array but when I try and set the high/low/average it's sticking at zero. It's pretty clear I need some practice on creating classes -_-
Member
Posts: 2,757
Joined: Nov 26 2007
Gold: 1,214.81
Jan 22 2015 02:28pm
First, I don't understand what you're trying to do with the PhoneBookEntry. Shouldn't it just have 1 name, but multiple phone numbers?

Also this is wrong

Code
public String getUser()
{
for(String contact : user)
{
return (contact);
}
}


It will always return the first contact in the list. Plus it won't work at all if user is empty (hence the compile error)

Should be

Code
public List<String> getUser( ) {
return user;
}
Member
Posts: 17,596
Joined: Aug 26 2005
Gold: 20,600.00
Jan 22 2015 03:13pm
Quote (labatymo @ Jan 22 2015 02:28pm)
First, I don't understand what you're trying to do with the PhoneBookEntry. Shouldn't it just have 1 name, but multiple phone numbers?

Also this is wrong

Code
public String getUser()
{
for(String contact : user)
{
return (contact);
}
}


It will always return the first contact in the list. Plus it won't work at all if user is empty (hence the compile error)

Should be

Code
public List<String> getUser( ) {
return user;
}


The idea is to save a name and a number for each person. It's running now, thanks. Any insight on the second file on why my high/low/average values aren't being set to anything?
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Jan 22 2015 03:17pm
Quote (J-dawg @ Jan 22 2015 04:13pm)
The idea is to save a name and a number for each person. It's running now, thanks. Any insight on the second file on why my high/low/average values aren't being set to anything?


i see you have setLow, setHigh, etc methods defined, but you aren't calling them.
Member
Posts: 17,596
Joined: Aug 26 2005
Gold: 20,600.00
Jan 22 2015 03:28pm
Quote (carteblanche @ Jan 22 2015 03:17pm)
i see you have setLow, setHigh, etc methods defined, but you aren't calling them.


Wouldn't the getLow/getHigh/etc methods return the value I set for high? When I do call getHigh it brings me back a 0.
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Jan 22 2015 03:39pm
Quote (J-dawg @ Jan 22 2015 04:28pm)
Wouldn't the getLow/getHigh/etc methods return the value I set for high? When I do call getHigh it brings me back a 0.


show me what line of code you're setting them. as far as i can tell, you never call set, so the values stay with their default 0.
Member
Posts: 17,596
Joined: Aug 26 2005
Gold: 20,600.00
Jan 22 2015 04:20pm
Quote (carteblanche @ Jan 22 2015 03:39pm)
show me what line of code you're setting them. as far as i can tell, you never call set, so the values stay with their default 0.


Ooo I see now, thanks.
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll