Page 1 of 2 12 LastLast
Results 1 to 20 of 25
  1. #1
    javauserjava is offline Member
    Join Date
    Mar 2011
    Posts
    27
    Rep Power
    0

    Default replace inside the println

    Hi everyone,

    I hope you are doing very well,,

    I have faced a problem with the next line


    Java Code:
    System.out.println(sc.nextLine().replace("Hi there", "DecimalFormat numberformat = new DecimalFormat("$ ##,###.00");"));

    the output of this print line has to be like this

    Java Code:
    DecimalFormat numberformat = new DecimalFormat("$ ##,###.00");
    but unfortunately it gave me an error in the ("$ ##,###.00") part if I take this part of everything is working perfectly,,


    the error
    XML Code:
    Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    	Syntax error on tokens, delete these tokens
    
    	at ReadAndWrite.main(ReadAndWrite.java:30)



    Your help is really needed and appreciated,,

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    You have a space in the format. Whitespace is not a valid number.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

  4. #4
    javauserjava is offline Member
    Join Date
    Mar 2011
    Posts
    27
    Rep Power
    0

    Default

    Dear Junky,

    I think it is not because the white spaces because

    this line is working
    Java Code:
    System.out.println(sc.nextLine().replace("Hi there", "DecimalFormat numberformat = new DecimalFormat;"));
    but with the ("$ ##,###.00") it is not working

    Java Code:
    System.out.println(sc.nextLine().replace("Hi there", "DecimalFormat numberformat = new DecimalFormat("$ ##,###.00");"));

    Thanks again

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    see my post above.

  6. #6
    javauserjava is offline Member
    Join Date
    Mar 2011
    Posts
    27
    Rep Power
    0

    Default

    Dear Fubarable,

    I need ("$ ##,###.00") to be printed.. if I use backslash before them it will not be printed at all.

    Thanks for your reply ,,

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by javauserjava View Post
    Dear Fubarable,

    I need ("$ ##,###.00") to be printed.. if I use backslash before them it will not be printed at all.

    Thanks for your reply ,,
    Then you're using it wrong. You'll need to show us how you're trying to use it, you think?

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Ahh my bad. I couldn't see the first line of code posted.

  9. #9
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    I believe you should also be escaping the " for double quotes inside double quotes.

    Java Code:
    System.out.println("Hi \"BOB\"");
    will print out Hi "BOB"

  10. #10
    javauserjava is offline Member
    Join Date
    Mar 2011
    Posts
    27
    Rep Power
    0

    Default

    Here I will post the whole class and I hope it is readable and understandable,,

    Java Code:
    import java.io.*;
    import java.util.Scanner;
    
    
    public class ReadAndWrite{
    
    public static void main(String args[]) throws IOException {
    
    		Scanner sc = new Scanner(new File("javaOutput.java"));
    		while(sc.hasNextLine()){
    
    		System.out.println(sc.nextLine().replace("-", "").replace("StringName", "PRODUCTNAMEININ").replace("PRODUCTNAMEININ2", "PSTOCKIN")
    		.replace("PRODUCTNAMEININ3", "PSALESIN").replace("PRODUCTNAMEININ4", "SCOMPANYNAMEIN").replace("PRODUCTNAMEININ5", "SPHONEIN")
    		.replace("PRODUCTNAMEININ6", "SFNAMEIN").replace("PRODUCTNAMEININ7", "SLNAMEIN").replace("01", "PRODUCTNAMEIN")
    		.replace("02", "PRODUCTINSTOCKIN").replace("03", "PRODUCTSALESPRICEIN").replace("04", "SUPPLIERCOMPANYNAMEIN")
    		.replace("05", "SUPPLIERPHONEIN").replace("06", "SUPPLIERFIRSTNAMEIN").replace("07", "SUPPLIERLASTNAMEIN").replace("line", "PIDOUT1")
    		.replace("PIDOUT11", "PRODUCTNAMEININ").replace("PIDOUT12", "PSTOCKIN").replace("PIDOUT13", "PSALESIN")
    		.replace("PIDOUT14", "SIDOUT1").replace("PIDOUT15", "SCOMPANYNAMEIN").replace("PIDOUT16", "SPHONEIN")
    		.replace("PIDOUT17", "SFNAMEIN").replace("PIDOUT18", "SLNAMEIN").replace("PIDOUT19", "PID")
    		.replace("String PSALESIN;", "String PSALESIN; DecimalFormat numberformat = new DecimalFormat("$##,###.00");")
    		.replace("PRODUCTSALESPRICEIN.readLine();", "numberformat.format(Integer.parseInt(PRODUCTSALESPRICEIN.readLine()));")
    		.replace("PRODUCTNAMEININ0", "SID").replace("PRODUCTOOUT", "PRODUCTOUT").replace("SUPPLIEROOUT", "SUPPLIEROUT")
    		.replace("BOTHOUT1", "BOTHOUT").replace("PRODUCTNAMEININ1", "PSALESIN"));
    	
    		}
    	}
    }

    Without just
    Java Code:
     ("$##,###.00")
    everything is work 100%

    Thanks for your help

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by sunde887 View Post
    I believe you should also be escaping the " for double quotes inside double quotes.

    Java Code:
    System.out.println("Hi \"BOB\"");
    will print out Hi "BOB"
    hm, this recommendation looks vaguely familiar. :rolleyes:

  12. #12
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    ARRRGGGH! My eyes. Kill the ugly code!

  13. #13
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by javauserjava View Post
    Here I will post the whole class and I hope it is readable and understandable,,

    Java Code:
    // ... etc

    Without just
    Java Code:
     ("$##,###.00")
    everything is work 100%

    Thanks for your help
    Why are you not showing us how you tried to escape your nested quotes? By the way, you really should refactor that code somehow.

  14. #14
    javauserjava is offline Member
    Join Date
    Mar 2011
    Posts
    27
    Rep Power
    0

    Default

    Also Dear Sunde it did not work,,

  15. #15
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by javauserjava View Post
    Also Dear Sunde it did not work,,
    And still you don't show what you tried -- why?

  16. #16
    javauserjava is offline Member
    Join Date
    Mar 2011
    Posts
    27
    Rep Power
    0

    Default

    LoooooL dear Junky,,

    I am really doing this code in propose,, I have a really big story and a big project working on..

  17. #17
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    You are writing awful code on purpose? Don't get a job were I work. Try using a StringBuilder instead.

  18. #18
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    [blush]

    I just realised I am to blame as I showed you how to chain 2 replace methods together in another thread. But if you are doing this many calls to replace then I strongly urge using the StringBuilder class instead.

  19. #19
    javauserjava is offline Member
    Join Date
    Mar 2011
    Posts
    27
    Rep Power
    0

    Default

    Dear repliers,


    I tried to use (\"$##,###.00\") but it did not write at all,,

    I don't have a good experiences with Jave but I have to do these small things in Jave but the other things were in the other programing languages,, Hope these things are helpful,

    I hope I answered your question Dear Fubarable,

  20. #20
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    You have done it for one replace call. Have you done it for all of them?

Page 1 of 2 12 LastLast

Similar Threads

  1. Println VS system.out.println
    By ccie007 in forum New To Java
    Replies: 2
    Last Post: 05-20-2010, 09:52 AM
  2. Need help with println
    By jhetfield18 in forum New To Java
    Replies: 8
    Last Post: 09-18-2009, 09:26 AM
  3. println doesn't print from inside for loop, et.al.
    By rdtindsm in forum New To Java
    Replies: 5
    Last Post: 03-27-2009, 02:19 PM
  4. difference between system.out.println() & out.println()
    By wickedrahul9 in forum Advanced Java
    Replies: 5
    Last Post: 10-19-2008, 12:06 AM
  5. System.out.println
    By sunjavaboy in forum Advanced Java
    Replies: 3
    Last Post: 03-22-2008, 02:30 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •