• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to output int array as string  RSS feed

 
battousai liang
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I was just wondering if someone can give me an example of how to convert elements in an array to output as a string ?

for example if you have int[] array = {5,8,5,6,9} the indexes are 0 1 2 3 4
then you write a method like:

public string OutputString()

// the method would need to output to the display each index (but as 1 2 3 4 5) and also the element of of each index in brackets, with one space in between like:

1(5) 2(8) 3(5) 4(6) 5(9)

I hope that makes sense, any help is really appreciated
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You forgot to include your code. What problems are you having with it?
 
battousai liang
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I am working on a uni assignment so i can't post my work for legal reasons, but if i could see an example of how it works i would be able to work it out by myself.

the problem is i find it hard to explain what i'm trying to do in java, but i thought i would try. If no one can show an example i will have to keep going over my material and try to find the logic there

thanks for your time
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
battousai liang wrote:Hi

I am working on a uni assignment so i can't post my work for legal reasons, but if i could see an example of how it works i would be able to work it out by myself.


I'm sure google has plenty of examples of printing the contents of an array.

the problem is i find it hard to explain what i'm trying to do in java


Forget Java. Explain what you're trying to do without regard to any programming language whatsoever. You have a bunch of boxes. Each box is labeled with a number (an index) to identify it, starting with 0 and going up to 1 less than the number of boxes. In each box is a card with an integer written on it. You want to write down some characters in a certain format that show each box's index and what number is written on the card inside it.

Write down the steps to do that in English or your native language. Write very simple, precise steps. Don't think about Java until you're done. Then translate each step to Java. If any given step or small group of doesn't translate fairly easily and directly to some Java where you understand exactly what it's doing, either your steps were too complicated or you need to study that aspect of Java more. (Most likely your steps weren't precise or simple enough. That part is actually much harder than just translating to Java.)
 
Ashish Dutt
Ranch Hand
Posts: 172
Java MySQL Database Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perhaps you could at least post the pseudocode or an algorithm of what you are trying to achieve
Because it would be worthwhile to know how you develop the logic in order to tackle this problem and we could take it up from there?
Its the initial concept, the initial logic to solve a problem that counts the most, so do let us know what have you thought of in order to solve this problem and we can work on it from there.
Cheers.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
battousai liang wrote:I am working on a uni assignment so i can't post my work for legal reasons,

Then I'm sure you understand that we can't give you the solution, for legal reasons. We wouldn't want to be seen as helping you cheat on your homework.

apart from the EXCELLENT advice Jeff has given you, break the problem down into parts, and work on solving those. Think of programming as working with Legos. You have (well, in programming you create) a bunch of teeny tiny parts that you assemble into a larger thing.

Some of the things you need to do:

Determine how big the array is
print all the integers between two numbers (say between 0 and 10)
Find a specific value in an array, given an index (i.e. you want the value at position 7)
compute one more than a given value
print a variable's contents

Once you can do each of these things, you can start assembling them into the program/method you need. Note that each of the above may be their own method, so your method may call some (or all) of these. Some of the above may already be doable in java - i.e. they are so common, there are built in mechanisms for doing them.

Part of the fun is figuring out which is which.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!