Results 1 to 5 of 5
  1. #1
    dylanka is offline Member
    Join Date
    Oct 2011
    Posts
    7
    Rep Power
    0

    Default String seperating Help.

    Hi all, so I am currently working on a problem, which I want to do this. Basically, it will read in a text file, which contains several numbers, which can be changed in how many numbers. For example, the file may have

    30, 24, 60, 14, 72, 22, 21, 63
    I want my program then to take in these numbers, and then using a binary tree method, to print the numbers in order. SO I want the output to be:

    14
    21
    22
    24
    10
    60
    63
    72
    I have already been able to create a program, which can do this, if each number is inserted individually. But I have been trying to manipulate it, so it reads the numbers from a file.

    But, the main problem is, I dunno how to sucessfully split it, and then insert it into the binary tree. I try splitting it with the , and space to indicate to split. But then, I want it so you dont have to put in each individual part, but somehow loop it, but I am kinda confusing myself now, as to how to do that.

    I am also using the first value in the file, in this case 30, as the rootnode. If anyone understands what I am trying to say, and can see what to do, I will much appreciate it. Here is what I have currently.

    Java Code:
    import java.io.*;
     class Node 
     {
         Node left;
         Node right;
         int value;
    
         public Node(int value) {
             this.value = value;
         }
    }
    
    
    public class BinaryTreeExample {
    
      public static void main(String[] args){
    	  try{
    		  FileInputStream fstream = new FileInputStream("NameOfFile");
    		  
    		  // Get the object of DataInputStream
    		  
    		  DataInputStream in = new DataInputStream(fstream);
    		  BufferedReader br = new BufferedReader(new InputStreamReader(in));
    		  String strLine = null;
    		  
    		  while ((strLine = br.readLine()) != null)   {
    			  
    			  String[] Parts = strLine.split(", ");	
    			  
    			  int rn = Integer.parseInt(Parts[0]);
    			  Node rootnode = new Node(rn);
    			  
    			  int x = Integer.parseInt(Parts[]);{
    			  
    				  insert(rootnode, x);
    				  printInOrder(rootnode);
    			  }
    			  
    		  }
    		  }
    		  catch (Exception e){
    			  System.err.println("Error: " + e.getMessage());
    		  }
    	  
    
    
      }
    
      public static void insert(Node node, int value) {
        if (value < node.value) {
           if (node.left != null) {
              insert(node.left, value);
           } else {
              node.left = new Node(value);
           }
        } else if (value > node.value) {
           if (node.right != null) {
              insert(node.right, value);
           } else { 
              node.right = new Node(value);
           }
        }
      }
    
      public static void printInOrder(Node node) {
        if (node != null) {
           printInOrder(node.left);
           System.out.println(node.value);
           printInOrder(node.right);
        }
      }
    }
    And if interested, here it was before I manipulated it, maybe it'll help:

    Java Code:
    class Node 
     {
         Node left;
         Node right;
         int value;
    
         public Node(int value) {
             this.value = value;
         }
    }
    
    
    public class BinaryTreeExample {
    
      public static void main(String[] args)
      {
         Node rootnode = new Node(25);
         insert(rootnode, 45);
         insert(rootnode, 65);
         insert(rootnode, 46);
         insert(rootnode, 123);
         insert(rootnode, 12);
    
    
      }
    
      public static void insert(Node node, int value) {
        if (value < node.value) {
           if (node.left != null) {
              insert(node.left, value);
           } else {
              node.left = new Node(value);
           }
        } else if (value > node.value) {
           if (node.right != null) {
              insert(node.right, value);
           } else { 
              node.right = new Node(value);
           }
        }
      }
    
      public static void printInOrder(Node node) {
        if (node != null) {
           printInOrder(node.left);
           System.out.println(node.value);
           printInOrder(node.right);
        }
      }
    }
    I dunno if I make sense at all though, but if I do, and you can help at all, I will be soooo appreciative :D

  2. #2
    tnrh1 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    251
    Rep Power
    4

    Default Re: String seperating Help.

    Before you start coding make sure you know what to do.
    Basically you have to insert the whole line into a string and then run a loop that will split your string.
    In the loop you will run on the string length and ask if the curent char is "," if true cut(substring) from 1 (not 0, 0 is ",") till your loop index(the substring cuts from a to b-1).

    The substring of course will be putted in a new string(you can use string array or just print it depends on your wills) and the String that holds the full line will be cutted from the index to the end.

    String st1 = "1,2,3,";
    String st2 = "1"; cut to a new string
    String st1 = ",2,3,"; cut this string also to move on.

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,327
    Rep Power
    25

    Default Re: String seperating Help.

    Or you could use the String class's split() method. If your data is formatted as you show, split should work.
    I see that you've used the split method. What was the results?
    Add some printlns to show the line that was read and to show the contents of the array created by split().
    You can use the Arrays.toString() method to show the array's contents

    I want it so you dont have to put in each individual part, but somehow loop it
    You can use the length of the Parts array to control the loop that gets each element out of the array and adds it to the tree.
    Last edited by Norm; 11-24-2011 at 09:42 PM.

  4. #4
    dylanka is offline Member
    Join Date
    Oct 2011
    Posts
    7
    Rep Power
    0

    Default Re: String seperating Help.

    Hey, had to go off, so tried doing it myself. Had a look at it again, and I had read the question wrong. Instead of the file being as above, i.e

    30, 24, 60, 14, 72, 22, 21, 63
    it is actually just

    30
    24
    60
    14
    72
    22
    21
    63
    So it made it easier. I know have some code. Which I think, does what I want to, but the output is not as I want, so obviously it doesn't. Instead of the output I have already stated above that I wanted. It is instead
    24
    30
    30
    60
    14
    30
    30
    72
    22
    30
    21
    30
    30
    63
    I can see that this is kinda doing what it is meant to do. It is placing the rootnode, i.e. 30, either left or right of each number. So if it is smaller than 30, ie the 21, the 30 comes the line after the 21. But if its greater, its the other way round. By current code is:

    Java Code:
    import java.io.BufferedReader;
    import java.io.DataInputStream;
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.InputStreamReader;
    
     class Node 
     {
         Node left;
         Node right;
         int value;
    
         public Node(int value) {
             this.value = value;
         }
    
    	public void InOrder() {
    		// TODO Auto-generated method stub
    		
    	}
    }
    
    
    public class Lab {
    
      public static void main(String[] args) throws NumberFormatException, IOException
      {
    	  FileInputStream fstream = new FileInputStream("C:/BinTree.txt");
    	  // Get the object of DataInputStream
    	  DataInputStream in = new DataInputStream(fstream);
    	  BufferedReader br = new BufferedReader(new InputStreamReader(in));
    	  
    	  String line;
    	  String rnn = br.readLine();
    	  int rn = Integer.parseInt(rnn);
    	  
    	  while((line = br.readLine()) != null){
    		  int line1 =  Integer.parseInt(line);
    	  
    	  	 Node rootnode = new Node(rn);
    	     insert(rootnode, line1);
    	     printInOrder(rootnode);
    	  }
    
    
      }
    
      public static void insert(Node node, int value) {
        if (value < node.value) {
           if (node.left != null) {
              insert(node.left, value);
           } else {
              node.left = new Node(value);
           }
        } else if (value > node.value) {
           if (node.right != null) {
              insert(node.right, value);
           } else { 
             node.right = new Node(value);
           }
        }
      }
      
    
      
      public static void printInOrder(Node node) throws IOException {
    	if (node != null) {
    		printInOrder(node.left);
    		System.out.println(node.value + "");
    		printInOrder(node.right);
    	}
    	  
      }
    }
    I would appreciate again any help. This website is helping me grow my knowledge so much :D

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,327
    Rep Power
    25

    Default Re: String seperating Help.

    Have you solved the problem of reading in the input and storing in the binary tree?
    If so, what are you currently having problems with?

    When you print out numbers, you should print labels on them so you know where they were printed.

    I don't see any debugging printlns in your code. If it is not working the way you want, you will need to do some debugging to find out where the problem is. I debug this kind of code by adding lots of printlns to show how the variables are changing and how the execution flow goes.

Similar Threads

  1. Replies: 11
    Last Post: 08-15-2011, 10:12 AM
  2. Replies: 12
    Last Post: 11-26-2010, 08:33 AM
  3. Replies: 1
    Last Post: 11-04-2010, 06:53 PM
  4. combine string[] into string like perl's join function
    By tekberg in forum Advanced Java
    Replies: 9
    Last Post: 02-23-2009, 01:05 PM
  5. Replies: 5
    Last Post: 01-11-2009, 10:12 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
  •