8 Replies - 925 Views - Last Post: 23 November 2010 - 10:33 PM Rate Topic: -----

#1 slixtrix  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 62
  • Joined: 24-April 10

array question

Posted 18 November 2010 - 04:52 PM

i am having trouble putting numbers in a file into an array...any help would be appreciated..

import java.util.*;
import java.io.*;

public class TreeTraversal{
	
       //gives the length of the current file.
	public static int setArrayLength() throws Exception{
		FileReader file = new FileReader("numbers.txt");
		LineNumberReader ln = new LineNumberReader(file);
		  
	      int count = 0;
	  
	        while (ln.readLine() != null){
	          count++;
	        }
	        
	        System.out.println(count);
			return count;
		
	        
		
	}
	

	public static void createArray(int[] array) throws Exception{
		Scanner file = new Scanner("numbers.txt");
		for(int i = 0; i < array.length; i++){
			array[i] = file.nextInt();
			System.out.println(array[i]);
		}
		
	}
	
	public static void main(String args[]) throws Exception {
                //sets the length of the array using the setArrayLength() method above.
		int[] array = new int[setArrayLength()];

                 //puts the integers of the file into the array, each line at a time.
		createArray(array);
	}
}
 



the file looks something like this:
12
23
53
11
9
28
25
etc..

and the length of the file is about 1000 lines.

Is This A Good Question/Topic? 0
  • +

Replies To: array question

#2 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1011
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: array question

Posted 18 November 2010 - 05:16 PM

I am sure all DIC's would love to help, but please, help us help you :)
what exactly are these troubles you mentioned?
are these error messages you get? if yes, please post the exact error messages.
are these some logic errors? if yes, what are they?
Was This Post Helpful? 0
  • +
  • -

#3 slixtrix  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 62
  • Joined: 24-April 10

Re: array question

Posted 18 November 2010 - 05:26 PM

1000
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:840)
at java.util.Scanner.next(Scanner.java:1461)
at java.util.Scanner.nextInt(Scanner.java:2091)
at java.util.Scanner.nextInt(Scanner.java:2050)
at TreeTraversal.createArray(TreeTraversal.java:26)
at TreeTraversal.main(TreeTraversal.java:34)


this is what prints out in the console when i run the program
Was This Post Helpful? 0
  • +
  • -

#4 slixtrix  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 62
  • Joined: 24-April 10

Re: array question

Posted 21 November 2010 - 12:29 PM

any ideas?
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,908
  • Joined: 06-March 08

Re: array question

Posted 21 November 2010 - 08:49 PM

I'll bet you have an empty line at the end of your file.
Easier to do it that way

int[] createArray(String filename) {
    File file = new File(filename);
    int count = 0;
    // Scanner to read the file
    Scanner scan = new Scanner(file);
    // count number of int
    while(scan.hasNextInt()) {
       ++count;
       scan.nextInt();
    }
    // close it
    scan.close();
    // create int[] array
    int[] num = new int[count];
    // read back the file
    scan = new Scannner(file);
    for(int i = 0; i < count; i++)
      num[i] = scan.nextInt();
    return num;
}


Was This Post Helpful? 1
  • +
  • -

#6 slixtrix  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 62
  • Joined: 24-April 10

Re: array question

Posted 22 November 2010 - 02:01 PM

thanks! that worked perfectly...what was wrong with my code? because even when i took out the blank line at the bottom (you were correct, when you proposed that idea) it still had the errors..but yours worked great
Was This Post Helpful? 0
  • +
  • -

#7 slixtrix  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 62
  • Joined: 24-April 10

Re: array question

Posted 22 November 2010 - 03:28 PM

ok, so now i have another problem....
my assignment is to create a tree out of a file of number, which i put first into an array, and them read them out of the array....my problem is my output is creating duplicate numbers with some crazy variables such as:

0 x 43
0 x 1
1 x 103
1 x 1
2 x 31
2 x 1

i will post all 3 classes with description of each:

This first class is a custom stack class that our professor requires us to make, "no outside packages(not including Scanner, File, etc.. all the necessary ones.)
this will create a array called stack of type node(one of my other classes).

public class stackOb {
	int top;
	node[] stack;
	int maxLen;
	
	public stackOb(int maxLen){
		stack = new node[maxLen];
		top = -1;
		this.maxLen = maxLen;
	}
	
	public node peek() {
		return stack[top];
	}
	
	public void push(node item){
		top++;
		stack[top] = item;
	}
	
	public node pop(){
		node num;
		num = stack[top];
		top--;
		return num;
	}
	
	public boolean empty(){
		if(top == -1){
			return true;
		}else{
			return false;
		}
	}
	
	public void reset(){
		top = -1;
	}
	
	public void showStack(){
		int j;
		
		System.out.println(" ");
		System.out.println("Stack contents ...");
		for(j = top;j > -1; j--) {
			System.out.println(stack[j].dataItem);
		}
		System.out.println(" ");
	}
	
	public void showStack0toTop(){
		int j;
		
		System.out.println(" ");
		System.out.println("Stack contents ...");
		for(j = 0; j <= top; j++) {
			System.out.print(stack[j].dataItem);
		}
		System.out.println(" ");
	}
	
}



This class is to create an object called node. in it is methods such as buildTree, which actually creates the tree...and inOrder which goes through the tree and prints out the data in a sorted manner. most of these methods were provided by the professor.
public class node {
	//establishes the variables for the node object.
	static node root = null;
	public int dataItem;
	public int dataNum;
	public static int compares;
	public static int treeLength;
	public node right, left;
	
	//creates a new node.
	public static node makeNode(int num){
		node newNode = new node();
		newNode.right = null;
		newNode.left = null;
		newNode.dataItem = num;
		newNode.dataNum = 1;
		
		return newNode;
		
	}
	
	public static void buildTree(int ndats, int[] nums){
		node spot;
		int j;
		treeLength = 0;
		
		for(j = 0; j < ndats; j++){
			if((j == 0) && (root == null)){
				root = makeNode(nums[j]);
				treeLength += 1;
				
			}else{
				spot = searchTreeR(root, nums[j]);
				if(nums[j] < spot.dataItem){
					spot.left = makeNode(nums[j]);
					treeLength += 1;
				}else{
					spot.right = makeNode(nums[j]);
					treeLength += 1;
				}
			}
		}
	}
	
	public static node searchTreeR(node tree, int number){
		node curr = tree;
		
		if((number < curr.dataItem)&&(curr.left != null)){
			++compares;
			curr = searchTreeR(curr.left, number);
		}
		if((number > curr.dataItem)&&(curr.right != null)){
			++compares;
			curr = searchTreeR(curr.right, number);
		}
		if((number == curr.dataItem)){
			++compares;
			curr.dataNum += 1;
		}
		
		return curr;
	}
	
	public static void inOrderI(node root){
		stackOb stack = new stackOb(100);
		node curr = root;
		stack.top = 0;
		while((!stack.empty())||(curr != null)){
			
			while(curr != null){
				stack.push(curr);
				curr = curr.left;
			}
			
			if(stack.empty() == false){
				curr = stack.pop();
				System.out.println(curr.dataItem + " x " + curr.dataNum);
				curr = curr.right;
			}
		}
	}
	
	//inserts an object into the left pointer of another object.
	public void insertLeft(node child, node parent){
		parent.left = child;
	}
	
	//inserts an object into the right pointer of another object.
	public void insertRight(node child, node parent){
		parent.right = child;
	}

	//goes through the tree and finds the spot where a particular number belongs.
	public node findSpot(node root, int num){
		if((num < root.dataItem) && (root.left != null)){
			findSpot(root.left, num);
		}
		if(num > root.dataItem){
			findSpot(root.right, num);
		}
		
		return root;
	}
	
	//fills the left pointer slot with an object with the data of an integer.
	public void setLeft(node Node, int num){
		if(Node.left != null){
			System.out.println("Error in method setLeft()");
		}else{
			Node.left = makeNode(num);
		}
	}
	
	//fills the right pointer slot with an object with the data of an integer.
	public void setRight (node Node, int num){
		if(Node.right != null){
			System.out.println("Error in method setRight()");
		}else{
			Node.right = makeNode(num);
		}
	}

}




Lastly, this is my class on which i do everything that the assignment is asking (some features left out for now, as im trying to get a certain one to work.) What this class does is, create an array from a list of numbers that it will read and then create a tree by using this particular array. this is also where the MAIN class is.
import java.util.*;
import java.io.*;

public class TreeTraversal{
	static int count;
	static int[] array;
	static node tree = null;
	
	//reads through the file, and puts the numbers into an array.
	public static int[] createArray(String filename) throws FileNotFoundException{
		File file = new File(filename);
	    count = 0;
		Scanner scan = new Scanner(file);
		
		//finds the length of the array.
		while(scan.hasNextInt()){
			++count;
			scan.nextInt();
		}
		
		scan.close();
		//prints out the array length, count.
		//System.out.println(count);
		
		int[] num = new int[count];
		
		//scans the file and puts the numbers into an array.
		scan = new Scanner(file);
		for(int i = 0; i < count; i++){
			num[i] = scan.nextInt();
		}
		
		return num;
	}
	
	public static void main(String args[]) throws Exception {
		array = createArray("numbers.txt");
		node.buildTree(count, array);
		node.inOrderI(node.root);
		
	}
}



any help about any part of this program would be useful...i will be working on it and checking the forum in case someone would care to make comments or questions about this particular program.
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8346
  • View blog
  • Posts: 31,908
  • Joined: 06-March 08

Re: array question

Posted 23 November 2010 - 07:26 PM

while (ln.readLine() != null){  
        count++;  
}


This will read anything, blank line, line containing only \n
Was This Post Helpful? 1
  • +
  • -

#9 slixtrix  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 62
  • Joined: 24-April 10

Re: array question

Posted 23 November 2010 - 10:33 PM

thanks, i figured out my problem tho, so you can mark this as solved
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1