• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

format method question

 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I have a question about this code.

Its based on examlab practice exam 3 question 47.

The output is a[a-dA-D]\d.
The point is of this code is to show that the format() method doesn't take reg-ex patterns such as "a[a-dA-D]\\d". Reg-ex patterns are left up to the Pattern class

I noticed that the 2nd string object--"At 8 Kr-3V ac45 aef32" is ignored and I looked up the Formatter.format() nethod in the API to find out why it was ignored. It says "If there are more arguments than format specifiers, the extra arguments are ignored"

My Question:
Where is the format specifier ever mentoned in this code?
Is it becuase of a lack of format specifer leads to the first string being printed?

-Thank you
-Fritz
 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
String test = String.format("hello", "world");
System.out.println(test);
//Output: hello

String test = String.format("hello %s", "world");
System.out.println(test);
//Output: hello world

Regex is not what expected in first parameter. It can be a sequence of format specifiers appearing in a string with syntax: %[argument_index$][flags][width][.precision]conversion

 
Fritz Guerilus
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for the clarification Byju , but I still would like to know:

Is it becuase there is a lack of format specifer which leads to the first string only being printed?

I'm guessing this is by design, so that only strings, or format strings are allowed in the format() method.
 
Ranch Hand
Posts: 257
Hibernate Oracle Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Fritz,

You are right, format specifiers are replaced with corresponding value. but as there is not format specifiers in the first string no replacement is done.
 
Fritz Guerilus
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank You Salil, very much appreciated.
 
reply
    Bookmark Topic Watch Topic
  • New Topic