Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 05-04-2011, 04:34 AM   PM User | #1
Jdiz
New to the CF scene

 
Join Date: May 2011
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Jdiz is an unknown quantity at this point
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..
Jdiz is offline   Reply With Quote
Old 05-04-2011, 03:42 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,751
Thanks: 4
Thanked 2,468 Times in 2,437 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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'); 
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:50 PM.


Advertisement
Log in to turn off these ads.