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 2 of 2 FirstFirst 12
Results 26 to 45 of 45

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

  1. #26
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

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

    What did that program do? Did it get the part of the String that you want?

    problem is working with more than one line.
    Can you identify what line has the data so you can use split to extract the part you want?
    Pseudo code:
    begin loop
    read line
    if this line has the data
    then extract the data from this line
    end loop
    If you don't understand my answer, don't ignore it, ask a question.

  2. #27
    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.

    It printed "Pos001"

    And with what you are suggesting, I dont think thats gonna work for me.

    Because the data will always be different, "pos001" is just an example.

    I will want the last word of the line #3 in the string...

  3. #28
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

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

    This line:
    System.out.println(str.split(" ")[2]);
    will print the third element in the array that was split from the String contained in str.
    The contents of str would be what was read into the variable line in the loop in the registry reading program.
    Replace str with line to see what you need in the main program.
    If you don't understand my answer, don't ignore it, ask a question.

  4. #29
    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.

    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.split(" ")[2]);
     
    		}
    	}
    }

    It gave me this error:

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2
    	at other.main(other.java:21)

  5. #30
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

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

    Remember when you tried using substring and got errors because the line was empty?
    Now you are having the same problem again. The code needs to look at the line to see if it is long enough.
    Also it needs to look at the size of the array returned by the split method to see if it has 3 elements before trying to access the third element.

    I posted this pseudo code on what the code needs to do:
    You need to identify what line has the data so you can use split to extract the part you want:

    Pseudo code:
    begin loop
    read line
    if this line has the data <<<<<<<<<<<< NEED THIS TEST
    then extract the data from this line
    end loop
    If you don't understand my answer, don't ignore it, ask a question.

  6. The Following User Says Thank You to Norm For This Useful Post:

    JosPhantasmE (January 22nd, 2013)

  7. #31
    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 just tough about using an "If" statement:

    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) {
     
     
    			if(line.contains("REG_SZ"))
     
    				System.out.println(line.split(" ")[2]);
    		}
    	}
    }

    But when I run it, noting gets printed, but if I change this:
    System.out.println(line.split(" ")[2]);
    for this:
    System.out.println(line);
    It will print the line that I want to work with.

        Certified    REG_SZ    Pos001

  8. #32
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

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

    noting gets printed
    Are you sure? Maybe it is printing a blank line. Add an id string to the println call:
    System.out.println("[2]="+line.split(" ")[2]+"<");  //  print with delimiters

    Put this line in front of the if statement so you see ALL the lines that are read before the if test:
    System.out.println("line="+line+"<");

    Put the value returned by the split() method in an array and print the array's contents using the Array class's toString() method:
    System.out.println("an ID "+ java.util.Arrays.toString(theArrayName));
    If you don't understand my answer, don't ignore it, ask a question.

  9. #33
    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.

    Put the value returned by the split() method in an array and print the array's contents using the Array class's toString() method:
    System.out.println("an ID "+ java.util.Arrays.toString(theArrayName));
    [/QUOTE]

    You lost me over there...

    And when I run this code:

    import java.io.*;
     
    //By José D. Hernández, 2013.
     
    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="+line+"<");
     
    			if(line.contains("Pos"))
     
    			{
    				System.out.println("[2]="+line.split(" ")[2]+"<");
     
    			}
    		}
    	}
    }

    I get this:


    [2]=<

  10. #34
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

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

    [2]=<
    The third element is an empty String. What is in the rest of the array?
    See the end of post #32 for how to print the contents of an array.


    Why is the println commented out? You need to see everything until you get the code debugged and working.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #35
    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 started playing around with the numbers and I got this:

    import java.io.*;
     
    //By José D. Hernández, 2013.
     
    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="+line+"<");
     
    			if(line.contains("Pos"))
     
    			{
    				System.out.println("[12]="+line.split(" ")[12]+"<");
     
    			}
    		}
    	}
    }

    Pinted:

    [12]=Pos001<

    What did just happen, I'm just trying to make sense out of this.

  12. #36
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

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

    What did just happen
    I can not tell without seeing the full contents of all the lines.
    You need to print out many values to see what is happening. See post#32 where I suggested some things to print out.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #37
    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 got what I wanted but know my code:


    //By José D. Hernández, 2013.
     
    import java.io.*; 
    import java.util.Scanner;
    import java.io.File;
     
    public class ugh 
    { 
    	public static void main(String args[]) throws IOException, InterruptedException 
    	{ 
    		String line, usern, passw, servdns = "adlx2101vze.serveftp.net", check1, check2;
    		CharSequence look = "REG_SZ", Error = "0 match(es)";
    		Process p;
     
    		Scanner scan = new Scanner (System.in);
     
    		Runtime runtime = Runtime.getRuntime();
    		Process process = runtime.exec("reg query HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Korbel\\TERMSVR1 /v Starting /t REG_SZ"); 
    		InputStream is = process.getInputStream();
    		InputStreamReader isr = new InputStreamReader(is);
    		BufferedReader br = new BufferedReader(isr);
     
     
    		while ((line = br.readLine()) != null) 
     
    		{
     
    			if(line.contains(Error))
     
    			{
    				System.out.print("Username: ");
    				usern = scan.next();
    				System.out.print ("Password: ");
    				passw = scan.next();
     
    				p=Runtime.getRuntime().exec("reg add HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Korbel\\TERMSVR1 /v Certified /t REG_SZ /d " + usern); 
    				p=Runtime.getRuntime().exec("reg add HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Korbel\\TERMSVR1 /v Starting /t REG_SZ /d " + passw);
    				p=Runtime.getRuntime().exec("reg add HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Korbel\\TERMSVR1 /v Verified /t REG_SZ /d " + servdns); 
     
    				p=Runtime.getRuntime().exec("cmdkey /generic:TERMSRV/" + servdns + " /user: " + usern + " /pass: " + passw);
     
    				p=Runtime.getRuntime().exec("mstsc.exe C:\\Windows\\System32\\drivers\\etc\\EOLine.rdp");
     
    			}
     
    			else
    			{
    				p=Runtime.getRuntime().exec("reg query HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Korbel\\TERMSVR1 /v Certified"); 
    				p.waitFor(); 
    				BufferedReader reader=new BufferedReader(new InputStreamReader(p.getInputStream())); 
    				check1=reader.toString();
     
    				while ((check1 = reader.readLine()) != null) 
    				{					
    					if(check1.contains("REG_SZ"))
     
    					{
    						String username = (check1.split(" ")[12]);
     
    						System.out.println(username);
     
    					}
    				}
    			}
    		}
    	}
    }

    When running the "else" part is printing this out:

    Pos001
    Pos001
    Pos001
    Pos001
    Pos001

    Is printing the info that I want 5 times. Which is suppose to be only 1.

  14. #38
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

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

    How many lines does the code read in the loop? What are on those lines?
    Are there 5 lines with the same data?
    How many times does the loop execute?

    You need to debug your code to see how it is executing. Add some println statements that shows where the execution flow is going and what is being read. The code has nested loops that could be executing the inner loop more than once. Print out some messages to see what the code is doing.
    If you don't understand my answer, don't ignore it, ask a question.

  15. #39
    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'm so confuse I dont know what could it be...

    When I run a System.out.println(check1); afther the while it print the whole string 5 times, and IDK why, can you help me understand?

  16. #40
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

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

    How many times was the inner loop executed?
    How many times do you want it to execute?

    I don't understand why there is an inner loop that can be executed more than one time.
    Every time it is executed it executes the same reg command and gets the same response.
    If it is executed 5 times, it will print out the string 5 times.
    If you don't understand my answer, don't ignore it, ask a question.

  17. #41
    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 mean it gets executed only one time,. thats my entire code right there.

  18. #42
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

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

    it gets executed only one time
    Did you add a println to print out a message every time the reg command is executed?
    How many times did it print out a message that the reg command was being executed?
    If you don't understand my answer, don't ignore it, ask a question.

  19. The Following User Says Thank You to Norm For This Useful Post:

    JosPhantasmE (January 22nd, 2013)

  20. #43
    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.

    It print the job 5 times. I think I found the problem but not sure how to solve, everything is inside a while statement, that might be the problem, but how can I assign a "else" to the if statement that is inside that while statement without being inside the same while statement, does that make sense?

  21. #44
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

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

    If you want to exit a while loop you can use the break statement.
    If you don't understand my answer, don't ignore it, ask a question.

  22. The Following User Says Thank You to Norm For This Useful Post:

    JosPhantasmE (January 22nd, 2013)

  23. #45
    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.

    Perfect, thank you very much! I stop it by using a boolean.

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Replies: 1
    Last Post: December 3rd, 2012, 02: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, 08:35 AM