• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

Manipulating arrays  RSS feed

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,

Given these:

public static void main(String[] args) {

int[] buildingNumbers;
String[] familyNames;

buildingNumbers = new int[]{57, 32, 9, 50, 31, 50, 70, 57, 50, 73};
familyNames = new String[]{"Andersson", "Johansson", "Nordin", "Holmgren", "Svensson", "Olsson", "Henriksson", "Bergner", "Loren", "Markus"};


I've been asked to complete this method:

public static void printFamilyNames(int buildingNumber, int[] buildings, String[] names){


I was thinking of completing it like that :

f


But it doesn't work. I've tried for too long and I cannot understand what I'm doing wrong.

I've just started java and I know that my problem seems too easy to the most of you. Any kind of help will be appreciated!

Thanks!

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The way you're declaring your for loops isn't correct. The bit before the first semi colon should initialise some variable. Your first one doesn't quite do that. The bit between the semi colons should evaluate to a boolean (true or false), again your first loop isn't doing that. Also, remember you need to close all brackets/parenthesis or things won't work. Are you working in an IDE? This is the sort of thing they'll point out to your straight away.
 
Andy Cushing
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One more thing. What exactly is your method supposed to do? I can try to guess but it's best if you tell me
 
Sara Suomela
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I'm working on IDE and my method should print out all the family names that live in the building number, which is given by variable buildingNumber and then to print the number of families that live in buildingNumber.

I tried to think in another way but again it doesn't work.

for (int i=0; i<buildings.length; i++) {
        buildings[i]=i+10;
}

for (int j=0; j<names.length; j++) {


if (buildings[i]==names[j]) {
System.out.println(names);

}


I've got honestly stuck!!!
 
Marshal
Posts: 60867
190
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Thank you for working oout how to use the code button, but please always indent your code inside the code tag, and don'tdouble‑space code. Indentation is important; if you don't indent your code, you can make all aosrts of strange mistakes and not see them. Your loop, which I think will produce no output, should look like this:-

When you were told to ues parallel arrays, did they tell you what bad design parallel arrays are? Did they tell you that a static method is often a bad idea, too? Did they tell you there is an easier way to initialise those arrays?Or that your familyNames array makes the line too long to read easily? I have shown you how you can make the lines shorter. In real life you would create a House Object with number and name as fields. Please always tell us what goes wrong; we can only help if we know the full details. In this case, however, I can guess what goes wrong. Why are you using two loops? Why didn't you use one loop? I can see no point in having the second loop. Look at your line 5. Write down what it will be evaluating if i is 3 and j is 3. Why would you want to do anything if i is 3 and j is 4?
Write down the array indices, the house numbers, and the names as columns:-Imagine there is a 0 as the first line number, then read the indices on the left. Does that give you any idea how to implement this exercise?
 
Campbell Ritchie
Marshal
Posts: 60867
190
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you see any > symbols in my last post? I don't know why thery have appeared, but they are only there by mistake.
 
Campbell Ritchie
Marshal
Posts: 60867
190
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Sara Suomela wrote:. . . my method should print out all the family names that live in the building number, . . .

That is different from what I thought.
 
Campbell Ritchie
Marshal
Posts: 60867
190
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Sara Suomela wrote:. . . I tried to think in another way . . .

That looks like guessing. You can make 1,000,000 guesses and you are bound to get a correct guess somewhere. Or you can think about what the problem is and get it forst time. If you want all the names at a particular house number, your first attempt was actually closer than the second attempt.
 
Sara Suomela
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all, thank you so much for trying to explain to me and make me to understand what I'm doing wrong.

Actually I haven't been told about how to use parallel arrays or about the bad design. I've been taught only about the basics of arrays and now I'm trying for getting your idea and how to implement it for writing this assignment.

When you say about using only 1 loop, do I use only this loop:

for (int=0; i<buildings.length; i++) {


To me, it seems that the names as well as the building numbers should be evaluated in this case, but I don't get why my if-statement is wrong. Is it because I put an integer array(buildingNumber) with a string array(names)? but since buildingNumber and names are the ones that should be evaluated, then how to write the control condition differently?

My main method is this one:









 
Andy Cushing
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As to why 1 loop is needed, earlier you had 1 loop that iterated through buildingNumbers and another that iterated through familyNames. Then you were comparing each of the elements in building names and seeing if they were equal to any of the elements in familyNames. This doesn't make a whole lot of sense because building number is a number and family name is not.

The way your arrays are structured (I presume) is that the Andersson family lives in building number 57, the Johansson family lives in building number 32 and so on...

You want to iterate through buildingNumbers until you find the buildingNumber given to you to search for, then once you find it you take the corresponding family and output it.

Hope this helps.
 
Campbell Ritchie
Marshal
Posts: 60867
190
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Sara Suomela wrote:First of all, thank you so much . . .

That's a pleasure

When you say about using only 1 loop, do I use only this loop:

Delete lines 12‑16. You can put them back later when you have solved the first problem.
You weren't clear about your requirements. Now you have shown us your lines 9‑10, it is becoming clearer. I think you need to go back to my first post. Write out a list of names and numbers as I showed you. Write down how you would find everybody at No 57 without using a computer. But it has to be very simple, in very small parts, so simply saying, “Look for No 57,” needs much more detail. When you have worked out how to find No 57, work out how to find the names of the people living there.

. . . I don't get why my if-statement is wrong. Is it because I put an integer array(buildingNumber) with a string array(names)?  . . .

If you write down what I suggested for your line 5 in your first post, it should become clear what you are doing with the == operator. For 3 you are testing if (50 == "Holmgren") ...

Don't use the == operator on Strings.
 
Sara Suomela
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unfortunately I cannot delete the lines 12-16 because this is given to me. I wish I could and do it as you say. I have to find the way otherwise it seems and I'm totally lost at the moment.

I start in this way:

f

So I think that now I get each element by buildings [I].
Now I need to check if it equals buildingNumber, so I'm using control condition:



and inside the if I print the family name:





Again it doesn't work, but the problem is my control condition. I tried to write it otherwise but I get always an error.


Any help?
 
Campbell Ritchie
Marshal
Posts: 60867
190
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Sara Suomela wrote:Unfortunately I cannot delete the lines 12-16 because this is given to me.  . . .

Of course you can delete those lines. You solve lines 9‑11 and then replace lines 12‑16. Or comment out those lines by writing // at their beginning. Try ctrl‑shift‑C on your IDE. All you achieve by keeping those lines in code is having to write another method to go wrong. Sort out one thing at a time.

Any help?

Stop guessing. Start with your method reading this:-You are going to delete that statement in a few minutes. Write down how you are going to find the people at No 57, using tiny steps, as I told you earlier, and show us what you have written. If that is correct, it will give you instructions (=pseudocode) for writing your code.
 
Sara Suomela
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just a quick question: how can I delete the information I need for completing my method? Actually I'm trying to understand.

I've already posted my main method, from which I'll extract the information for this method :public static void printFamilyNames. In order to complete this second method, I should print all the family names that lives in the building number that is given by variable buildingNumber + also, to print the number of families that live in buildingNumber.

I think that my problem is the control condition. I've written it like this  but I know that it's wrong. I don't know how to write an if-statement between an integer array(buildingNumber) and a String array (family names).
 
Campbell Ritchie
Marshal
Posts: 60867
190
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Make your main method look like this:-You can remove the // and restore lines 12‑16 very easily. Use spaces for indenting not tabs; I am surprised that your IDE doesn't have “Convert one tab to four spaces” set up as a default setting.
Start by counting how many people live there. Do one thing at a time. You can't do several things simultaneously.
If you had written down the steps to get the family names as I told you earlier, you would proabbly have that method working nicely by now.
 
Campbell Ritchie
Marshal
Posts: 60867
190
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Sara Suomela wrote:. . . I don't know how to write an if-statement between an integer array(buildingNumber) and a String array (family names).

I don't think you need that at all.
 
Sara Suomela
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After working so many hours without any result, I think that I'm getting more lost and tired. I do apologize, thus, if I get you also tired by repeating things.

I've been told that I must use loop, if statement and system.out.print command. So, I cannot follow your way even if I personally prefer it. I have to work with the guidelines that are given to me.

As I've already mentioned, I'm a totally beginner in Java and this is why such a simple task has been turned out a nightmare for me. I keep on trying though even if I got stuck in my control condition.

 
Sara Suomela
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know who live in that specific building number as well the number of the families. I just cannot put it correctly in code. I know that I've started well with my loop but I've got really stuck for hours....
 
Campbell Ritchie
Marshal
Posts: 60867
190
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My idea will need a loop, an if and System.out.printXXX.
If you are getting tired, I recommend you get some beer/tea/coffee/cannabis and go for a walk in the park. You will be more successful if you stop coding and take a rest.
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Sara Suomela wrote:. . . uildingNumbers = new int[]{57, 32, 9, 50, 31, 50, 70, 57, 50, 73};
familyNames = new String[]{"Andersson", "Johansson", "Nordin", "Holmgren", "Svensson", "Olsson", "Henriksson", "Bergner", "Loren", "Markus"};
. . .

I  have question that, she had created a anonymous array and assigned to references so is it  possible to read the elements of anonymous array through for loop
 
Sheriff
Posts: 5300
142
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, those aren't anonymous arrays, they're array initializers.  So this:
is illegal, but this:
is not.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!