Jump to content

String.format()

- - - - -

  • Please log in to reply
1 reply to this topic

#1
Kaiser24

Kaiser24

    Newbie

  • Members
  • Pip
  • 1 posts
Essentially, I'm working on a program for my class and my instructors wants us to use String.format();

System.out.println(list.toString());

That's what will call the method.

public String toString()
{
return String.format("........")
}

Pretty much, I don't know what to put within the method to get a string representation
of a list array that prints out the information.
list[0] = 6 7 2706
list[1] = 1 1 3101
list[2] = 8 8 8318
etc ....


(This is what It would look like after being printed out)

List of items:
6/7/2706
1/1/3101
8/8/8318
7/4/8424
9/30/6087
8/11/3934
11/28/8964
2/13/9717
11/7/9381
9/8/1611
2/3/8950

Anyone mind helping me out with this problem since String.format() still kind of goes over my head.
If ya need more information that I failed to supply, feel free to ask for it.

#2
Metalhead

Metalhead

    Newbie

  • Members
  • PipPip
  • 27 posts
String.fomat is not very difficult... the first argument is your String, with some dynamic values, which you give as the next aguments...
F.e. format("Test %d this", 4) -> "Test 4 this"

Here is what you could do (if I understand you correctly);
public class Test {

	public static void main(String[] args) {
		
		String[] list = new String[3];
		list[0] = "6 7 2706";
		list[1] = "1 1 3101";
		list[2] = "8 8 8318";
		
		for (String string : list) {
			System.out.println(string.format("%s/%s/%s", string.split(" ")));			
		}
    }
}





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users