Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    May 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Program using one-dimensional array

    The program reads 12 integers into an one-dimensional array and sorts them into ascending order using Selection Sort and prints out the sorted data. Next, the Binary Search method is to be used to locate a particular integer or output a warning message if it is not present on the list, otherwise outputting the location of the integers first occurrance (index).

    I feel as though I understand this but I am sure I am doing it very wrong. Any hints would be greatly appreciated!
    Thank you so much!

    My code:
    /*This program takes a group of numbers inputted by the user and stores them in an array, sorts them via a selection sort, and searches for a particular value via a Binary Search*/

    import java.util.*;
    public class LynchA9
    {
    static Scanner console = new Scanner(System.in);

    inputData(int[] intList) //calling method, InputData
    {

    int[] intList = new int[12]; //declare array of 12
    int listLength = 12;

    System.out.println("Enter 12 integers."); //user inputs data


    for (int i=0; i < listLength; i++)
    {
    intList[i] = console.nextInt(); //storing pre-sorted data
    }

    selectionSort(int[] intList, int listLength) //calling method selection sort
    {
    int i; //initializing variables for selection sort
    int smallestIndex ;
    int minIndex;
    int temp;

    for (i = 0; i < listLength - 1; i++) //loop for selection sort
    {
    smallestIndex = i;
    for (minIndex = i + 1; minIndex < listLength; minIndex++)
    {

    if (intList[minIndex] < intList[smallestIndex])
    smallestIndex = minIndex;
    temp = intList[smallestIndex];
    intList[smallestIndex] = intList[index];
    intList[index] = temp;
    }
    } //end loop
    System.out.print(intList[i] + "");

    outputData(int[] sortedData) //calling method, OutputData
    {
    System.out.println("Data after selection Sort:" + sortedData); //output of sorted data

    binarySearch (int[] intList, int listLength, int searchItem) //calling Binary Search method
    {
    System.out.println("Enter the search item:"); //user inputs search item

    int first = 0;
    int last = listLength -1;
    int mid;

    boolean found = false;

    while (first <= last && !found)
    {
    mid = (first + last) /2;
    if (intList[mid] == searchItem)


    The errors I am getting:

    ----jGRASP exec: javac -g LynchA9.java

    LynchA9.java:8: invalid method declaration; return type required
    inputData(int[] intList) //calling method, InputData
    ^
    LynchA9.java:22: '.class' expected
    selectionSort(int[] intList, int listLength) //calling method selection sort
    ^
    LynchA9.java:22: ';' expected
    selectionSort(int[] intList, int listLength) //calling method selection sort
    ^
    LynchA9.java:22: ';' expected
    selectionSort(int[] intList, int listLength) //calling method selection sort
    ^
    LynchA9.java:44: '.class' expected
    outputData(int[] sortedData) //calling method, OutputData
    ^
    LynchA9.java:44: ';' expected
    outputData(int[] sortedData) //calling method, OutputData
    ^
    LynchA9.java:48: '.class' expected
    binarySearch (int[] intList, int listLength, int searchItem) //calling Binary Search method
    ^
    LynchA9.java:48: ';' expected
    binarySearch (int[] intList, int listLength, int searchItem) //calling Binary Search method
    ^
    LynchA9.java:48: <identifier> expected
    binarySearch (int[] intList, int listLength, int searchItem) //calling Binary Search method
    ^
    LynchA9.java:48: not a statement
    binarySearch (int[] intList, int listLength, int searchItem) //calling Binary Search method
    ^
    LynchA9.java:48: ';' expected
    binarySearch (int[] intList, int listLength, int searchItem) //calling Binary Search method
    ^
    LynchA9.java:78: reached end of file while parsing
    }//end
    ^
    12 errors

    ----jGRASP wedge2: exit code for process is 1.
    ----jGRASP: operation complete.
    Last edited by Jdiz; 05-04-2011 at 10:43 AM.

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,978
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    There methods all need a return type, even if its void.
    Since you used icode instead of code tags, the formatting has been lost so its a little tough to tell looking at this. But to me it appears that several of these methods are nested, which (if I'm not mistaken, since it is valid in some languages) is illegal in java, so that won't compile like that anyway.

    This looks quite incomplete. But you'll need to start it off by declaring the methods properly with a return type, and proper braces for each method.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •