Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 1 of 2 12 LastLast
Results 1 to 25 of 45

Thread: Print just what I want, help me with my code.

  1. #1
    Member
    Join Date
    Jan 2013
    Posts
    57
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Print just what I want, help me with my code.

    I have this code:

    import java.io.*;
     
    //By José D. Hernández, 2012.
     
    public class other {
     
    	public static void main(String args[]) throws IOException, InterruptedException 
    	{ 
    		Runtime runtime = Runtime.getRuntime();
    		Process process = runtime.exec("reg query HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Korbel\\TERMSVR1 /v Certified"); 
    		InputStream is = process.getInputStream();
    		InputStreamReader isr = new InputStreamReader(is);
    		BufferedReader br = new BufferedReader(isr);
    		String line;
     
    		while ((line = br.readLine()) != null) {
     
     
    			System.out.println(line);
    		}
    	}
    }

    This code will search for that windows registry key and print out something like this:

    HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Korbel\TERMSVR1
        Certified    REG_SZ    Pos001

    Is there any possible way to only print out "Pos001", but Poss001 is only an example, there could be anything, so I'm trying to print out wherever is in the position of Pos001, or if is possible wherever arguments that are in "HKCU\Software\Microsoft\Windows\CurrentVersion\Ko rbel\TERMSVR1 /v Certified" Which in this case is "Pos001"

    Thanks.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,100
    Thanks
    48
    Thanked 2,153 Times in 2,126 Posts

    Default Re: Print just what I want, help me with my code.

    If what you show is in a String, you can scan the String and extract a substring from it to print.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Jan 2013
    Posts
    57
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Print just what I want, help me with my code.

    How exactly? And sorry for my ignorance.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,100
    Thanks
    48
    Thanked 2,153 Times in 2,126 Posts

    Default Re: Print just what I want, help me with my code.

    Is the data you want in a String?
    The String class has many methods for searching a String and getting a substring from it.

    Where is the data located in the String? What search method would locate it? Read the API doc to see which ones could be useful.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Jan 2013
    Posts
    57
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Print just what I want, help me with my code.

    Yes, it is a String.

    --- Update ---

    Apparently, is not a String, because if does not matter how I do it, it give me an error.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,100
    Thanks
    48
    Thanked 2,153 Times in 2,126 Posts

    Default Re: Print just what I want, help me with my code.

    it give me an error.
    Post the full contents of the error message.
    Add a println to print out its value, execute the code and post the contents of the console from when it is executed.

    To copy the contents of the command prompt window:
    Click on Icon in upper left corner
    Select Edit
    Select 'Select All' - The selection will show
    Click in upper left again
    Select Edit and click 'Copy'

    Paste here.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Jan 2013
    Posts
    57
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Print just what I want, help me with my code.

    This is my code:

    import java.io.*;
    import java.util.regex.*;
     
    //By José D. Hernández, 2012.
     
    public class other {
     
    	public static void main(String args[]) throws IOException, InterruptedException 
    	{ 
    		Runtime runtime = Runtime.getRuntime();
    		Process process = runtime.exec("reg query HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Korbel\\TERMSVR1 /v Certified"); 
    		InputStream is = process.getInputStream();
    		InputStreamReader isr = new InputStreamReader(is);
    		BufferedReader br = new BufferedReader(isr);
    		String line;
     
    		while ((line = br.readLine()) != null) {
     
    			System.out.println(line.substring( 0, 2 ));
     
    		}
    	}
    }

    This is the error I get:

    Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 2
    	at java.lang.String.substring(Unknown Source)
    	at other.main(other.java:19)

    But when I do a simple print: System.out.println(line);

    I get this:

    HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Korbel\TERMSVR1
        Certified    REG_SZ    Pos001

    Which is to me a String, so I don't know whats going on.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,100
    Thanks
    48
    Thanked 2,153 Times in 2,126 Posts

    Default Re: Print just what I want, help me with my code.

    When you print out the String by itself without the substring, how many lines are printed?
    Are some of the lines very short? Include the String's length in the print out.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Jan 2013
    Posts
    57
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Print just what I want, help me with my code.

    THis is what is printed... THere not short


    HKEY_CURRENT_USER\Software\Microsoft\Windows\Curre ntVersion\Korbel\TERMSVR1
    Certified REG_SZ Pos001

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,100
    Thanks
    48
    Thanked 2,153 Times in 2,126 Posts

    Default Re: Print just what I want, help me with my code.

    It looks like there are two blank lines printed. You need to print the length of each String when the String is printed.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Member
    Join Date
    Jan 2013
    Posts
    57
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Print just what I want, help me with my code.

    OMG, and how can I do that, can you give me an example?

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,100
    Thanks
    48
    Thanked 2,153 Times in 2,126 Posts

    Default Re: Print just what I want, help me with my code.

    The String class has a method that returns the length of the String. Print out what it returns.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Member
    Join Date
    Jan 2013
    Posts
    57
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Print just what I want, help me with my code.

    Thanks, and now, i get this.:


    0
    HKEY_CURRENT_USER\Software\Microsoft\Windows\Curre ntVersion\Korbel\TERMSVR1
    75
    Certified REG_SZ Pos001
    33

    0

    --- Update ---

    when I run this code:


    System.out.println(line);
    			System.out.println(line.length());

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,100
    Thanks
    48
    Thanked 2,153 Times in 2,126 Posts

    Default Re: Print just what I want, help me with my code.

    Do you see now what you need to do? You need to consider the String's length before trying to take a substring of it.


    The printlns could be:

    	System.out.println("len=" + line.length() + " >" + line + "<");
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Member
    Join Date
    Jan 2013
    Posts
    57
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Print just what I want, help me with my code.

    Okay, thanks.

    And how can I select line 3, and try to look for the last word in that line?

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,100
    Thanks
    48
    Thanked 2,153 Times in 2,126 Posts

    Default Re: Print just what I want, help me with my code.

    Count the lines as they are read.
    Use one of the String class's methods.
    If you don't understand my answer, don't ignore it, ask a question.

  17. #17
    Member
    Join Date
    Jan 2013
    Posts
    57
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Print just what I want, help me with my code.

    But which one?? Don't forget I just started programming.

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,100
    Thanks
    48
    Thanked 2,153 Times in 2,126 Posts

    Default Re: Print just what I want, help me with my code.

    Time to do some experimenting. Write a small simple program that defines a String:
    String str = "Certified REG_SZ Pos001";
    Then use different String class methods to scan and split and search the String. Print out the results of each test and see if any of them are useful.

    This is a technique I have used many, many times when working with new classes. I used it once today with another OPs problem to see how to use the classes and methods that he was having problems with,
    If you don't understand my answer, don't ignore it, ask a question.

  19. #19
    Member
    Join Date
    Jan 2013
    Posts
    57
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Print just what I want, help me with my code.

    I've tried everything with no luck, can you help me with some code?

    I have this:

    import java.io.*;
     
    //By José D. Hernández, 2012.
     
    public class other {
     
    	public static void main(String args[]) throws IOException, InterruptedException 
    	{ 
    		Runtime runtime = Runtime.getRuntime();
    		Process process = runtime.exec("reg query HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Korbel\\TERMSVR1 /v Certified"); 
    		InputStream is = process.getInputStream();
    		InputStreamReader isr = new InputStreamReader(is);
    		BufferedReader br = new BufferedReader(isr);
    		String line;
     
    		while ((line = br.readLine()) != null) 
    		{
     
    			System.out.println(line);
     
    		}
    	}
    }

    That print out this:

     
    HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Korbel\TERMSVR1
        Certified    REG_SZ    Pos001


    As you can see there is 4 lines printed but I only want "Pos001" to be printed.

    Please, help, this is killing me.

    Thanks.

  20. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,100
    Thanks
    48
    Thanked 2,153 Times in 2,126 Posts

    Default Re: Print just what I want, help me with my code.

    Did you try what I suggested in post#18? Post the code for the small testing program you used that tried the different String class methods
    If you don't understand my answer, don't ignore it, ask a question.

  21. #21
    Member
    Join Date
    Jan 2013
    Posts
    57
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Print just what I want, help me with my code.

    I've tried everything, seriously!

  22. #22
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,100
    Thanks
    48
    Thanked 2,153 Times in 2,126 Posts

    Default Re: Print just what I want, help me with my code.

    Post the code and its output that shows what you have tried. You could be very close and only need a small change to get it to work.
    If you don't understand my answer, don't ignore it, ask a question.

  23. #23
    Member
    Join Date
    Jan 2013
    Posts
    57
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Print just what I want, help me with my code.

    Here are some examples of what I have tried:

    import java.io.*;
    import java.util.regex.*;
     
    //By José D. Hernández, 2012.
     
    public class other {
     
    	public static void main(String args[]) throws IOException, InterruptedException 
    	{ 
    		Runtime runtime = Runtime.getRuntime();
    		Process process = runtime.exec("reg query HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Korbel\\TERMSVR1 /v Certified"); 
    		InputStream is = process.getInputStream();
    		InputStreamReader isr = new InputStreamReader(is);
    		BufferedReader br = new BufferedReader(isr);
    		String line;
     
     
    		while ((line = br.readLine()) != null) {
     
     
     
    			//System.out.println(line);
    			//System.out.println(line.length());
    			//System.out.println("len=" + line.length() + " >" + line + "<");
    			//System.out.println(line.split("HKEY")[0]);
     
    			//String trimmedString = line.trim();
    			//System.out.println(trimmedString);
     
    			String adjusted = line.replaceAll("(?m)^[ \t]*\r?\n", "");
    			System.out.println(adjusted);
     
    		}
    	}
    }

  24. #24
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,100
    Thanks
    48
    Thanked 2,153 Times in 2,126 Posts

    Default Re: Print just what I want, help me with my code.

    Did you write a small simple testing program as I suggested in post #18?

    Use that to try the different methods to find methods that will help you parse the String that is shown in post#18.

    The code in post #23 causes extra complications for the job of finding what String methods to use.
    No one else can test the code because their registry would have different contents.
    If you don't understand my answer, don't ignore it, ask a question.

  25. #25
    Member
    Join Date
    Jan 2013
    Posts
    57
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: Print just what I want, help me with my code.

    I did what you told me and this worked:

    //By José D. Hernández, 2012.
     
    public class texting {
     
    	public static void main(String args[])
    	{ 
    		String str = "Certified REG_SZ Pos001";
     
    		System.out.println(str.split(" ")[2]);
     
    	}
    }

    But my problem is working with more than one line.

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 1
    Last Post: December 3rd, 2012, 01:35 PM
  2. Replies: 16
    Last Post: September 16th, 2012, 11:59 PM
  3. letterGrade not print out on my code
    By troj4nk1ng in forum What's Wrong With My Code?
    Replies: 4
    Last Post: July 8th, 2011, 08:31 PM
  4. print code to browser
    By bookface in forum Java Theory & Questions
    Replies: 4
    Last Post: April 21st, 2010, 01:09 AM
  5. Can someone please tell me why my code doesn't print out anything?
    By deeerek in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 6th, 2010, 07:35 AM