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.
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
Quote:
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.
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
Quote:
30, 24, 60, 14, 72, 22, 21, 63
it is actually just
Quote:
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
Quote:
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:
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
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.