1

I am working on a small school assignment, in which I make a way to bubblesort an array of 3 < N < 16 values. I have to bubblesort the array from standard input via prescribed methods. I have completed the bubblesort part with no syntax errors whatsoever and exactly how the assignment told us to, but the input is giving me a slight problem. In theory my code should work and work as intended, but I am getting this error Cannot find symbol - method readInt(java.lang.String).

I have asked my teacher for assistance and help but the only thing he is doing is telling me that I have to code an applet and panel, but that is not how I intend to tackle this assignment.

The code (I use BlueJ):

import java.io.*;
import java.util.*;
import java.io.Console;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.util.ArrayList;
import java.io.DataInputStream;
import java.io.DataOutputStream;
/**
 * Made by me
 */

public class Bubblesort
{
    private int[] a;

    public Bubblesort(int howLong) 
    {
        a = new int[howLong];
        if (howLong < 3 || howLong > 16) 
        {
            System.out.print("The length of the Array must be 3<N<16");
        }
        else 
        {
            for(int i = 0; i < a.length; i++) 
            {
                a[i] = Console.readInt("Add a value to the Array: ");
                System.out.print(a[i] + " ");
            }
            System.out.println();
        }
    }

    public void printList() 
    {
        for(int i = 0; i < a.length; i++) 
        {
            System.out.print(a[i] + " ");
        }
        System.out.println();
    }

    public void swap(int begin, int end) 
    {
        int r = -1;             
        int s = -1;

        int x;        
        int y;

        int ax;        
        int ay;

        for(int i = (end-begin); i > ((end-begin)/2); i--) 
        {
            x = begin + s;  
            y = end + r;  

            ax = a[x];      
            ay = a[y];      

            a[x] = ay;           
            a[y] = ax;

            r--;                
            s++;                

        }

        printList();
    }

    public void sort() 
    {
        printList();
        int k = a.length;
        for(int i = 1; i < a.length; i++) 
        {
            int gr = 0;
            for(int j = 1; j < k; j++) 
            {
                if(a[j] > a[gr]) 
                {
                    gr = j;
                }
            }
            swap((gr + 1), k);
            k--;
        }
    }
}

I know I added probably a lot of unnecessary imports but I have passed the point of having no idea what I am supposed to do...

The error is in this line:

for(int i = 0; i < a.length; i++)       
{
    a[i] = Console.readInt("Add a value to the Array: ");
    System.out.print(a[i] + " ");
}

I am pretty sure someone around here knows the fix for this OR knows how to accomplish the same thing as I have in mind with a different piece of code.

Thnx in advance ;)

1 Answers1

1

The class Console does not have the method readInt.

It has a method called readLine() and readLine(String fmt, Object... args).

To achieve what you intend you can do the following:

System.out.println("Add a value to the Array: ");
a[i] = Integer.parseInt(Console.readLine());

Or you can also refer to this answer in order to use Scanner.

Community
  • 1
  • 1
aribeiro
  • 3,854
  • 4
  • 27
  • 41