Heap Sort - Number Format Exception

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 248 Views - Last Post: 07 February 2012 - 08:18 PM Rate Topic: -----

Topic Sponsor:

#1 fury283  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 31-January 12

Heap Sort - Number Format Exception

Posted 06 February 2012 - 10:51 PM

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

public class App2 
        {
	 
	    /**
	     * @param args the command line arguments
	     */
	    public int count;
	    public String string[];
	    public Scanner input;
	    public String lines[]; 
	     
	    App2()
	    {
	           
	        string = new String[1000];
	        input = new Scanner(System.in);
	        count = 0;
                lines = new String[32];
	    }

public static void main(String[] args) throws IOException 
        {
	          
	        App2 ja = new App2();
	        String filename;
                
	         	         
	        if (args.length > 0)//Finds the command arguement in file
	        {
	            for(int i = 0; i < args.length; i++)
	            {
	                filename = args[i];
	                ja.test(filename);
	            }
	             
	        }
                ja.random();
					 ja.heap();
                
        }
public void test(String inFiles) 
	     {
	         File infile = new File(inFiles);
	         String k;
	          
	          
	             try
	             {
	                 Scanner inside = new Scanner(infile);
	                  
	                 while(inside.hasNext())
	                 {
	                     k = inside.next();
	                     job(k);
	                 }
	             }
	             catch(FileNotFoundException e)
	             {
	                 System.out.println("Not working!");
	                       
	             }
	          
	     }

public void job(String result)
{

     
     string[count] = result;
     count++;
    
   

}

public void random()
{
    for(int i = 0; i < 31; i++)
    {
        Random r = new Random();
        int n = r.nextInt(999);
        lines[i] = string[n];
        
        System.out.println(lines[i]);
        
    }
}
public void heap()
{
    int i;
for (i = 0; i < lines.length; i++)
  //System.out.println(" "+lines[i]);
  for(i=lines.length; i>1; i--)
  {
  fnSortHeap(lines, i - 1);
  }
  System.out.println("\n  Sorted array\n---------------\n");
  for (i = 0; i < lines.length; i++)
  System.out.println(" "+lines[i]);
}

public static void fnSortHeap(String array[], int arr_ubound){
  int i = 0; 
  int o;
  int lChild = 0; 
  int rChild = 0;
  int mChild = 0; 
  int root;
  String temp;
  int lines1 = Integer.parseInt(array[rChild]);
  int lines2 = Integer.parseInt(array[lChild]);
  int lines3 = Integer.parseInt(array[mChild]);
  int lines4 = Integer.parseInt(array[i]);
  root = (arr_ubound-1)/2;

  for(o = root; o >= 0; o--){
  for(i=root;i>=0;i--){
  lChild = (2*i)+1;
  rChild = (2*i)+2;
  if((lChild <= arr_ubound) && (rChild <= arr_ubound)){
  if(lines1 >= lines2)
  mChild = rChild;
  else
  mChild = lChild;
  }
  else{
  if(rChild > arr_ubound)
  mChild = lChild;
  else
  mChild = rChild;
  }

  if(lines4 < lines3){
  temp = array[i];
  array[i] = array[mChild];
  array[mChild] = temp;
  }
  }
  }
  temp = array[0];
  array[0] = array[arr_ubound];
  array[arr_ubound] = temp;
  return;
  }
}




This is a assignment, yes. :P
We have to get a list of 1000 words, pull 32 random words from the string using a random number generator. We then list then unsorted, then use 4 different sorts. I am only working on ONE sort right now as, if I can get one working I can get the others working easily enough.

Now I know the issue I'm having is with the parseInts. But I don't know where....

Exception in thread "main" java.lang.NumberFormatException: For input string: "<First word in list here>"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at App2.fnSortHeap(App2.java:113)
at App2.heap(App2.java:98)
at App2.main(App2.java:41)

Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: Heap Sort - Number Format Exception

#2 jon.kiparsky  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1782
  • View blog
  • Posts: 3,358
  • Joined: 19-March 11

Re: Heap Sort - Number Format Exception

Posted 06 February 2012 - 11:06 PM

Try looking here:


at App2.fnSortHeap(App2.java:113)


Looks like that's this:
int lines1 = Integer.parseInt(array[rChild]);


What is the value of array[rChild] at that point? Is there an easy way to find out? Is that a rhetorical question?
Was This Post Helpful? 0
  • +
  • -

#3 fury283  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 31-January 12

Re: Heap Sort - Number Format Exception

Posted 06 February 2012 - 11:15 PM

View Postjon.kiparsky, on 06 February 2012 - 11:06 PM, said:

Try looking here:


at App2.fnSortHeap(App2.java:113)


Looks like that's this:
int lines1 = Integer.parseInt(array[rChild]);


What is the value of array[rChild] at that point? Is there an easy way to find out? Is that a rhetorical question?



Well, when I was modifying it to take strings instead of ints, it wanted me to define a value for each of them when I was adding the parseInts. I set all Childs for the heap sort at = 0. Though I'm second guessing on what the right child (its a heap sort, so its basically a tree), if I remember correctly, the right child is always bigger.... though I'm lost when it comes to Java most of the time...
Was This Post Helpful? 0
  • +
  • -

#4 jon.kiparsky  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1782
  • View blog
  • Posts: 3,358
  • Joined: 19-March 11

Re: Heap Sort - Number Format Exception

Posted 06 February 2012 - 11:18 PM

Apparently, array[rChild] is some String which can't be parsed into an integer. Is there a simple way to find out what's stored in the array at that location at that time?

Again, the answer is yes.
Was This Post Helpful? 0
  • +
  • -

#5 fury283  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 31-January 12

Re: Heap Sort - Number Format Exception

Posted 06 February 2012 - 11:21 PM

Sadly, I don't know if my skill level is high enough to figure that out.... ;/ -

... hmm, the right child...
Was This Post Helpful? 0
  • +
  • -

#6 jon.kiparsky  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1782
  • View blog
  • Posts: 3,358
  • Joined: 19-March 11

Re: Heap Sort - Number Format Exception

Posted 06 February 2012 - 11:24 PM

Try using a println, just before the line with the parseInt.
Was This Post Helpful? 0
  • +
  • -

#7 smohd  Icon User is offline

  • Critical Section
  • member icon



Reputation: 1644
  • View blog
  • Posts: 4,126
  • Joined: 14-March 10

Re: Heap Sort - Number Format Exception

Posted 06 February 2012 - 11:31 PM

You set line from a string array but the string array has no anything, I dont see anywhere in your code you give values to string array. Look at your program flow and you will see what I am talking about.
Was This Post Helpful? 0
  • +
  • -

#8 fury283  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 31-January 12

Re: Heap Sort - Number Format Exception

Posted 06 February 2012 - 11:36 PM

We have to do this from the argument. string[] gets the words directly from a file using

java App words.txt

Microsoft Windows [Version 6.1.7601]
Copyright © 2009 Microsoft Corporation. All rights reserved.

C:\Users\furyfire>cd desktop



C:\Users\furyfire\Desktop>java App2 words.txt
CLOSED
RELIGION
LACK
WHICH
CHRISTIAN
MOTHER
SEEM
EFFECT
USED
BODY
CHANGE
TAKING
NATURAL
ORDER
COMMUNIST
SERVICE
RETURNED
BED
THATS
ANALYSIS
THERES
WHAT
MEMBER
THUS
COSTS
THINGS
OLD
PLACE
USUALLY
BETWEEN
ACTION
Exception in thread "main" java.lang.NumberFormatException: For input string: "C
LOSED"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at App2.fnSortHeap(App2.java:113)
at App2.heap(App2.java:98)
at App2.main(App2.java:41)

C:\Users\furyfire\Desktop>

This is what I'm getting.
Was This Post Helpful? 0
  • +
  • -

#9 jon.kiparsky  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1782
  • View blog
  • Posts: 3,358
  • Joined: 19-March 11

Re: Heap Sort - Number Format Exception

Posted 06 February 2012 - 11:38 PM

That explains why you're getting a number format problem. Why are you trying to parse words into ints? What is it you're trying to do there?
Was This Post Helpful? 0
  • +
  • -

#10 smohd  Icon User is offline

  • Critical Section
  • member icon



Reputation: 1644
  • View blog
  • Posts: 4,126
  • Joined: 14-March 10

Re: Heap Sort - Number Format Exception

Posted 06 February 2012 - 11:39 PM

That is it. You are trying to change those words into integer!!!! All of them are words and cant use parseInt() to change them to integer because they are not numeric strings.
May be what was the purpose of that idea?
Was This Post Helpful? 0
  • +
  • -

#11 fury283  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 31-January 12

Re: Heap Sort - Number Format Exception

Posted 06 February 2012 - 11:54 PM

Because most of the items in the search were int and would not work with strings themselves.. it was the only way to get the sort working in general.

Oh, just to add, the sort works fine when I try not to add the words pulled from the 1000 array. It WILL sort them if left alone.

I tried various ways to get the sort working, and as of right now, it worked when I put 'string[]' into the for loop there, but when I put lines[] into it, it has trouble. lines[] is the one that needs to go INTO it, sadly.
Was This Post Helpful? 0
  • +
  • -

#12 fury283  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 31-January 12

Re: Heap Sort - Number Format Exception

Posted 07 February 2012 - 12:10 AM

Sorry, I'm just frustrated.. I'm not very good at coding, and thought I was getting somewhere.. guess not >_<
Was This Post Helpful? 0
  • +
  • -

#13 jon.kiparsky  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1782
  • View blog
  • Posts: 3,358
  • Joined: 19-March 11

Re: Heap Sort - Number Format Exception

Posted 07 February 2012 - 12:17 AM

Previous attempt to post this failed, but:

Try using "compareTo()" method to compare the strings. Read the API description of the method, also look into the Comparable interface. This should get you where you want to be.
Was This Post Helpful? 0
  • +
  • -

#14 fury283  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 31-January 12

Re: Heap Sort - Number Format Exception

Posted 07 February 2012 - 12:20 AM

View Postjon.kiparsky, on 07 February 2012 - 12:17 AM, said:

Previous attempt to post this failed, but:

Try using "compareTo()" method to compare the strings. Read the API description of the method, also look into the Comparable interface. This should get you where you want to be.



Do I use the compareTo in the sort? ;/ Cause we HAVE to use the sort...
Was This Post Helpful? 0
  • +
  • -

#15 jon.kiparsky  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 1782
  • View blog
  • Posts: 3,358
  • Joined: 19-March 11

Re: Heap Sort - Number Format Exception

Posted 07 February 2012 - 12:24 AM

Yes. compareTo returns an integer. The sign of the integer indicates where this object sorts relative to the other one - so you can use it to tell you whether this object is "greater than" or "less than" that one. Spend a little time with google. You should be able to work this out.
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2