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

    Default Combine two S.O.P in one

    Hi everyone,

    I have this code and I would like two combine the two print statements in one because right now it just does the first print statement.



    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("01", "1212"));
    			System.out.println(sc.nextLine().replace("-", ""));
    	
    		}
    	}
    }


    you help is really needed and appreciated ,,

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

    Default

    Quote Originally Posted by javauserjava View Post
    it just does the first print statement.
    this doesn't make sense. The second print statement will also be executed unless an exception is thrown. Is an exception being thrown? Perhaps it is printing nothing. More information is needed.

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

    Default

    Thanks for your reply,,

    There is no exception at all.. What I mean by "it just does the first print statement." it does not take the dashes off as shown in the second print statement.. Also if I change the order to be like this

    System.out.println(sc.nextLine().replace("-", ""));
    System.out.println(sc.nextLine().replace("01", "1212"));

    It will do the first statement which is going to take the dashes of but will not replace the (01).. which mean the second print statement will do nothing ..

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

    Default

    You do realise that those two print statements are being performed on 2 different lines of text. You call sc.nextLine in both so each will read a different line from the file. If your intention is to perform the changes on the same line then either read the line and store in a variable, perform two calls to replace and then print. Or you can chain the calls.
    Java Code:
    System.out.println(sc.nextLine().replace("-", "").replace("01", "1212"));

  5. #5
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,233
    Rep Power
    7

    Default

    First of all your are reading two different lines so your are performing the replace on two different strings.

    Secondly strings are immutable so if you want to invoke to different replaces on your string then the code would look something like:
    Java Code:
    String test = "...".
    test = test.replace(...);
    test = test.replace(...);

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

Similar Threads

  1. How to combine Buttons and threads
    By jnms in forum New To Java
    Replies: 7
    Last Post: 02-24-2011, 09:06 PM
  2. can i combine this 2 code into one?
    By reeveliew in forum New To Java
    Replies: 3
    Last Post: 05-09-2010, 02:24 PM
  3. How can I do this? Combine variable.
    By PeterFeng in forum New To Java
    Replies: 5
    Last Post: 01-14-2009, 05:44 PM
  4. [SOLVED] How do I combine two variables?
    By bobleny in forum New To Java
    Replies: 12
    Last Post: 07-08-2008, 03:15 PM
  5. How to combine mysql and java?
    By sandeeprao.techno in forum Advanced Java
    Replies: 1
    Last Post: 05-21-2008, 04:41 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
  •