[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 183
  • Last Modified:

Correction of code

HELLO EXPERTS,

I have a problem with the code below can anyone help me make the code runnable ???



import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.IOException;

import java.util.*;


public class ReadStringsWithArrayList
{

    public static void main( String [ ] args )
    {
        ArrayList array = getStrings( );

        for( int i = 0; i < array.size( ); i++ )
            System.out.println( array.get( i ) );
         
        BinaryTree tree = new BinaryTree();
           tree.makeTree(array);
       
           makeHashTable(array);
           
           Compare();
        }
           

    public static ArrayList getStrings( )
    {
        BufferedReader in = new BufferedReader( new InputStreamReader( System.in ) );
        ArrayList array = new ArrayList( );
        String oneLine;


        System.out.println( "Give strings: " );

        try
        {
            while( ( oneLine = in.readLine( ) ) != null && !oneLine.equals( "" ) )
                array.add( oneLine );
               
                   
        }
        catch( IOException e )
        {
            System.out.println( "IO Exception" );
        }
       
        System.out.println( "Done" );
        return array;
    }
   
   
}
       

class ReadStringsWithArrayList2
{

    public static void main( String [ ] args )
    {
        ArrayList array2 = getStrings( );

        for( int i = 0; i < array.size( ); i++ )
            System.out.println( array.get( i ) );
           
            BinaryTree tree = new BinaryTree();
           tree2.makeTree(array2);
       
           makeHashTable(array2);
        }
    public static ArrayList getStrings( )
    {
        BufferedReader in = new BufferedReader( new InputStreamReader( System.in ) );
        ArrayList array = new ArrayList( );
        String oneLine;


        System.out.println( "Give strings: " );

        try
        {
            while( ( oneLine = in.readLine( ) ) != null && !oneLine.equals( "" ) )
                array2.add( oneLine );
               
                   
        }
        catch( IOException e )
        {
            System.out.println( "IO Exception" );
        }
       
        System.out.println( "Done" );
        return array2;
        }
   
    }
       

class Hashtable
{
    public void makeHashTable()
    {
                Hashtable hashtable = new Hashtable(array.length * 2);
               
        for (int i = 0; i< array.length; i++)
        {
            hashtable.put(array[i],array[i]);
        }
    }
}

class BinaryTree
{
        public class Node
        {
            private String data;
            private Node leftLink;
            private Node rightLink;
            public Node(String n)
            {
                this(n,null,null);
            }
            public Node(String n, Node left, Node right)
            {
                data = n;
                leftLink = left;
                rightLink = right;
            }
            public String getData()
            {
                return this.data;
            }
            public Node getLeft()
            {
                return this.leftLink;
            }
            public Node getRight()
            {
                return this.rightLink;
            }
        }
            private Node root;
           
            private void makeTree(String[] array)
            {
                this.root = this.build(array, o);
            }
            private Node build (String[] array, int i)
            {
                if (i >= array.length)
                {
                    return null;
                }
                Node  node = new Node (array[i], build(array, 2*i + 1), build(array,2*i + 2));
                return node;
            }
}

class Compare
{
    Sring returnString = new String("");
    for (int i = 0; i< array2.length; i++)
    {
        boolean containedinone = false;
        for (int j = 0; j < array1.length; j++)
        {
            if (array1[j].equals(array2[i]))
            {
                containedinone = true;
            }
        }
        if (!containedinone)
        {
            returnString = returnString + "," + array2[i];
        }
    }
    if (returnString.equals(""))
    {
        return "SAME";
    }
    else
    {
        return returnString;
    }
   
    boolean compare(BinaryTree a, Tree2 b) {
    Iterator ita = a.iterator();
    Iterator itb = b.iterator();
    boolean result = (a.size() == b.size());
    while(ita.hasNext() && itb.hasNext() && result) {
         if(!ita.next().equals(itb.next())) {
            result = false;
           }
        }
      return result;
   }
   
     if (!Hashtable2.containsValue(Hashtable.get(key))) {
                System.out.println("Map one has value \"" + mapOne.get(key) +
                        "\" and mapTwo does not.");
                    }
                 
0
perdoname_
Asked:
perdoname_
1 Solution
 
krakatoaCommented:
A lot of your statements (for example the for loops around line 161) are not in class methods, you just have them in the class. There's a typo on the word String too in line 160 - t missing. And more.
0
 
gatorvipCommented:
It might help if you actually post what you intend to do in the code.

At a glance, the Compare class is very messed up (like krakatoa says, you just have floating statements not belonging to any method in particular). In the very first class, ReadStringsWithArrayList, you call a method Compare() that does not exist in that class. I'm not sure what the second class, ReadStringsWithArrayList2, is supposed to do. The class Hashtable has a single method, makeHashTable, that takes no parameters and just seems to use an array out of thin air.
0
 
perdoname_Author Commented:
Thanks for the answers

Actually i want to have two separate classes which they take as input strings and write strings to array and to array2 respectively. In other words the two parts of code below. After that i want to pass the elements of the two arrays in a binary tree and to a hashtable
So i created another two classes for binary trees: BinaryTree and BinaryTree2 which they have a makeTree method each [the code in snippet].  I do also have the code for hashtable but it returns an error "cannot find Symbol  - Hashtable(int)"  [its the last part of the code snippet]. And then i want to have a compare class which will compare array - array2, binaryTree - binaryTree2 , hashtable - hashtable2. [i have that code already]

ReadStrings [class 1st]

import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.*;
public class ReadStrings
{
    public static void main( String [ ] args )
    {
        String [ ] array = getStrings( );
        for( int i = 0; i < array.length; i++ )
            System.out.println( array[ i ] );
   
        }

    public static String [ ] getStrings( )
    {
        BufferedReader in = new BufferedReader( new InputStreamReader( System.in ) );
        String [ ] array = new String[ 5 ];
        int itemsRead = 0;
        String oneLine;

        System.out.println( "Enter string: " );


        try
        {
            while( ( oneLine = in.readLine( ) ) != null && !oneLine.equals( "" ) )
            {
                if( itemsRead == array.length )
                    array = resize( array, array.length * 2 );
                array[ itemsRead++ ] = oneLine;
               
            }
        }
        catch( IOException e )
        {
            System.out.println( "IO Exception" );
        }
       
        System.out.println( "Done" );
        return resize( array, itemsRead );
    }


    public static String [ ] resize( String [ ] array, int newSize )
    {
        String [ ] original = array;
        int numToCopy = Math.min( original.length, newSize );

        array = new String[ newSize ];
        for( int i = 0; i < numToCopy; i++ )
            array[ i ] = original[ i ];
        return array;
    }
     
}


ReadStrings2 [CLASS 2nd]

import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.*;
public class ReadStrings2
{
    public static void main( String [ ] args )
    {
        String [ ] array2 = getStrings( );
        for( int i = 0; i < array2.length; i++ )
            System.out.println( array2[ i ] );
   
       
       
        }


    public static String [ ] getStrings( )
    {
        BufferedReader in = new BufferedReader( new InputStreamReader( System.in ) );
        String [ ] array2 = new String[ 5 ];
        int itemsRead = 0;
        String oneLine;

        System.out.println( "Enter any number of strings, one per line; " );
        System.out.println( "Terminate with empty line: " );

        try
        {
            while( ( oneLine = in.readLine( ) ) != null && !oneLine.equals( "" ) )
            {
                if( itemsRead == array2.length )
                    array2 = resize( array2, array2.length * 2 );
                array2[ itemsRead++ ] = oneLine;
               
            }
        }
        catch( IOException e )
        {
            System.out.println( "IO Exception" );
        }
       
        System.out.println( "Done" );
        return resize( array2, itemsRead );
    }


    public static String [ ] resize( String [ ] array2, int newSize )
    {
        String [ ] original = array2;
        int numToCopy = Math.min( original.length, newSize );

        array2 = new String[ newSize ];
        for( int i = 0; i < numToCopy; i++ )
            array2[ i ] = original[ i ];
        return array2;
    }
     
}



   

//BinaryTree [class 1st]
class BinaryTree
{
        public class Node
        {
            private String data;
            private Node leftLink;
            private Node rightLink;
            public Node(String n)
            {
                this(n,null,null);
            }
            public Node(String n, Node left, Node right)
            {
                data = n;
                leftLink = left;
                rightLink = right;
            }
            public String getData()
            {
                return this.data;
            }
            public Node getLeft()
            {
                return this.leftLink;
            }
            public Node getRight()
            {
                return this.rightLink;
            }
        }
            private Node root;
            private int o;
            
            public void makeTree(String[] array)
            {
                this.root = this.build(array, o);
            }
            private Node build (String[] array, int i)
            {
                if (i >= array.length)
                {
                    return null;
                }
                Node  node = new Node (array[i], build(array, 2*i + 1), build(array,2*i + 2));
                return node;
            
            }
}
 
//BinaryTree2 [class 2nd]
public class BinaryTree2
{
   
        public class Node
        {
            private String data;
            private Node leftLink;
            private Node rightLink;
            public Node(String n)
            {
                this(n,null,null);
            }
            public Node(String n, Node left, Node right)
            {
                data = n;
                leftLink = left;
                rightLink = right;
            }
            public String getData()
            {
                return this.data;
            }
            public Node getLeft()
            {
                return this.leftLink;
            }
            public Node getRight()
            {
                return this.rightLink;
            }
        }
            private Node root;
            private int o;
            
            public void makeTree2(String[] array2)
            {
                this.root = this.build(array2,o);
            }
            private Node build (String[] array2, int i)
            {
                if (i >= array2.length)
                {
                    return null;
                }
                Node  node = new Node (array2[i], build(array2, 2*i + 1), build(array2,2*i + 2));
                return node;
            }
}
 
 
 
//HASHTABLE
 
public class Hashtable
{
    public void makeHashTable(String [] array)
    {
                Hashtable hashtable = new Hashtable(array.length * 2);
 
                
                
        for (int i = 0; i< array.length; i++)
        {
            hashtable.put(array[i],array[i]);
        }
    }
}

Open in new window

0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now