Sorting Array First By Name Then If They Are Identical By ID

May 8, 2014

I'm in the process of doing a module grade book project and I have a working program, however I have to sort the array of students created first of all by name and then if they are identical, by student ID. I've looked at the compareTo method on several websites and I still don't know how to implement it in my code.

My main is as follows;

public class CommandLineTest {
public static void main(String[] args){
String moduleTitle=Input.getString("Enter module title: ");
double cwPercentage=Input.getDouble("Enter coursework percentage: ");
double examPercentage=Input.getDouble("Enter exam percentage: ");

[Code] .....

View Replies


ADVERTISEMENT

Identical Array Program

Oct 7, 2014

I am trying to write a program where I input numbers and if both arrays contain the same numbers it prints out identical. If not it prints out not identical. The issue I am having is that it says I am missing return statement at the very end of my program (the last bracket).

I added the else at the very end before the return true and this was the error that occurred. However is I remove the else all it says is lists are not identical even if they are?

import java.util.*;
import java.util.Scanner;
import java.util.Arrays;
public class Tester {
public static void main(String[] args) {
java.util.Scanner input = new java.util.Scanner(System.in);

[Code] ....

View Replies View Related

Two Classes With Identical Constructors?

Jun 3, 2014

Is it possible to combine two classes that I have defined to contain some of the same elements so that NetBeans stops giving me errors? I don't want to get rid of any necessary code, and if both classes are necessary, should I just rename one of them? One class is an ArrayList that I am using to write the information for employees entered to a text file "employee.txt." I also want users to be able to call on this information via employeeID in order to display employee information. The code is the following:

public ArrayList<Employee> getEmployees() {
// if the employees file has already been read, don't read it again
if (employees != null)
return employees;
employees = new ArrayList<>();
if (Files.exists(employeesPath)) // prevent the FileNotFoundException {

[code]....

The other class is a getEmployee class that I previously defined before attempting to read the information from the text file and display it in the console. It is as follows:

private void getEmployees() {
try {
// if the file doesn't exists, create it
if (!Files.exists(employeesPath))
Files.createFile(employeesPath);
BufferedReader in =
new BufferedReader(
new FileReader(employeesFile));

[code]....

View Replies View Related

Methods Of StringBuffer And StringBuilder Are Identical

Apr 4, 2014

" In terms of methods supported by the classes, the methods of StringBuffer and StringBuilder are identical. They only differ in whether the methods are synchronized or not. " - Oracle Certified Associate Java SE7 Programmer Study Guide

What does synchronized mean in this context?

View Replies View Related

Manually Sorting Array Into Another Array

Jan 28, 2015

This piece of code i cannot change

public class Lab
{
public static void main(string args[]) {
int ar[]={7,5,2,8,4,9,6};
int sorted[]=new int[ar.length];

/// my code is right here this i can change i keep getting array required int found i'm not sure what i'm doing wrong i do know i need 2 for loops and an if statement.

[code]for(i=0;i<ar.length;i++){
for(j=i+1;j<ar.length;j++){
if(ar.length[i]>ar.length[j]
this piece of code cannot change
for(int i = 0; i<sorted.length; i++)
{
system.out.println("sorted[" + i + "] = " + sorted[i]);[/code]

View Replies View Related

Sorting 2D Java Array

Sep 16, 2014

I need to sort a 2D array to look like this : [URL] ....

Here is the code that I have.

File 1:
import java.text.DecimalFormat;
public class Assignment1 {
public static void main(String[] args) {
String[][] rainArray
= {
{"January", "3.03"},

[Code] ....

View Replies View Related

Array Sorting Using Two Different Methods

Nov 18, 2014

The following code is supposed to generate random integers and sort array1 and array2 using two different sorting methods. array1 is to be sorted with a "selection sort" method and array2 is to be sorted with the built-in Arrays.sort() method. However, array1 is the one that has a problem. It does not appear to output any values at all for array size of 4000 or more, such as array1[10000]. The assignment is to generate random integers, sort and benchmark the speeds at which array1 and array2 can generate and sort ints at array1[1000] array2[1000] array1[10000] array2[10000] array1[100000] array2[100000]

/**
*
* The following is a sorting and benchmarking program to sort
* array1 and array2 with 1,000 , 10,000 and 100,000 array sizes.
* array1 uses selection sort from section 7.4 of the book
* and array2 uses the built in Arrays.sort() method.

[code]....

I cannot post the output because the amount of data seems to have crashed the two previous posts I made on this topic due to the size of the problem.

View Replies View Related

Sorting Array In Alphabetical Order

Feb 3, 2015

I am trying to sort an array that I have by alphabetical order but I am having problems. Firstly the code that I have used to sort the array may not even do what I need but havn't got far enough to test it yet so go easy on me . I have read in some places when searching how to do this that I would have to create my own bubble sort in order to achieve this but I was hoping that Java had a built in sort method/function. Secondly I lack the knowledge in java to be able to assign an existing array or even a variable to the newly sorted array as I need the unsorted version with the original name and the newly sorted version as another.

code (This is not all of the code, I decided to include only what I thought was relevant):

import java.util.Arrays;
public class Sentence {
private String words[];
public Sentence(String[] words) { this.words = words; }
@Override
public String toString() {
return "Sentence{" +
"words=" + Arrays.toString(words) +

[Code] ....

Is it possible to shorten the sort function to just this?

public String sorted() {
return Arrays.sort(words);
}

View Replies View Related

Sorting Array Of Objects With Strings?

Feb 14, 2015

An array has objects with String firstName, String lastName, int ID, char examType('M' or 'F'), and int score. Every object has both Midterm('M') and Final('F'). The array has to be sorted by the first Midterm- 'M' and immediately followed by the Final ('F') of the same person (object). Im having troubles with coming up with a proper algorithm with gettin the Final- 'F' after Midterm for the same person.

Java Code: public static Exam[] collateExams(Exam[] exams) {
Exam [] r = new Exam[10];
r = exams;
int[] position = new int[10];
int index = 0;
for(int i = 0; i < exams.length; i++)

[Code]...

View Replies View Related

Boolean Array And Sorting Elements Within It

May 24, 2014

If I have a boolean array that contains 30 elements (boolean[] fish), how do I go about isolating every 10 elements to use for something specific?

Say there are 30 types of fish stored within the boolean array and 0-9 are fish found specifically in the Indian Ocean, 10-19 are fish found specifically in the Atlantic, and 20-29 are fish specifically found in the Pacific Ocean. And for those 10 fish [0-9], [10-19], [20-29], each is a different color (red, orange, green, blue, white, black, silver, yellow, purple and gold), where the colors and locations of the fish are enum types Colors and Locations.

How do I go about appointing those characteristics to the fish?

Ex: elements [0-9] are fish from the Indian Ocean and [0] is red, [1] is orange, [2] is green, [3] is blue, [4] is white, [5] is black, [6] is silver, [7] is yellow, [8] is purple, and [9] is gold.

elements [10-19] are fish from the Atlantic Ocean and [10] is red, [11] is orange, [12] is green, [13] is blue, [14] is white, [15] is black, [16] is silver, [17] is yellow, [18] is purple, and [19] is gold.

elements [20-29] are fish from the Indian Ocean and [20] is red, [21] is orange, [22] is green, [23] is blue, [24] is white, [25] is black, [26] is silver, [27] is yellow, [28] is purple, and [29] is gold.

Will I need to appoint those characteristics in the constructor after initializing fish = new boolean[30]?

View Replies View Related

Sorting Array Of Random Integers

Dec 8, 2014

What the fastest way to sort an array of random integers. My instructor told me to look into an algorithm that uses 2n, but that is all he gave me. I am unable to find anything of the sort (pun intended there) on google, in my text, in my other java books or on here. The only thing that I have come up with are things like mergesort with the exception that the indicies of the Array have to be even. The program that I'm writing takes user input for a minimum to maximum range, the amount of numbers to fill the array in that range, and verbose.

I've tried bubbleSort but it takes forever to sort under the conditions below. I have seen a sorting algorithm that uses n (log n) but didn't really understand how that one worked.

Using the 2n algorithm, I should be able to test the range from 1 - 1000 and have it populate an array of of 1,000,000 random integers. It should be able to complete in a matter of miliseconds.

View Replies View Related

Array Sorting - Ascending And Descending Order

Apr 10, 2014

I am sorting an array in ascending and descending order. I am using the nethods in Arrays as below

Arrays.sort(myArray)

I want to print both input and output array in sysout.

Object[] ipArray = [45,8,32,41,11,7];
Object[] opArray;
Object mArray = ipArray;

Arrays.sort(mArray); This is changing the ipArray too ?

How can I get my input array unmodified ?

View Replies View Related

Sorting Array Of Strings From User Input

Apr 3, 2015

I am working on an assignment where I initially have to take as input a number. That number will be the number of strings the user is going to type. As they are typed, they are to be sorted alphabetically, then printed. It took me a while to get this far, and I am stuck on how to properly invoke the other methods in main. There has to be at least three methods: main, one to do the sorting, and one to print the new array. I'm quite sure there are mistakes in my code, but this is definitely a challenge for me. Here is the code. Keep in mind that the methods being invoked are blank on purpose.

I am not allowed to use the Arrays, Arraylist or Collection class. This is basically to test my ingenuity and to see how arrays actually work.

import java.util.*;
public class Sort_as_Inserted {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter number of elements to be sorted: ");
String element_number = input.nextLine();

[Code] ....

View Replies View Related

Merging And Sorting Array Lists Code Is Returning 0s?

Apr 19, 2015

I'm working on an assignment that asks for the user to input 2 lists of numbers and my program will merge and sort the lists using arrays and 2 methods. I think I have most of it down, but I'm not sure how to go about getting the user inputs. In my current code, it's giving me a bunch of 0s instead of a sorted list.

import java.util.Scanner;
public class merge2sortedlists {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);

[code]....

View Replies View Related

Array Sorting In Ascending Order - Displaying Integers

Oct 21, 2014

I couldn't where the problem with this code. The question is : (Write a program that reads in nine integers and sorts the value in the ascending order. Next, the program restores the sorted integers in a 3 x 3 two-dimensional array and display the integers as shown in the result.) And this how i did it:

package test1;

import java.io.*;
import java.util.*;
public class test1{
public static void main(String[] args){
int[] hold = new int [9];
Scanner sc = new Scanner(System.in);
System.out.print("Enter 9 integers, press <Enter> after each");
{
for (int i = 0; i < hold.length; i++);

[Code] ....

View Replies View Related

Sorting Array Of Objects Based On One Of Class String Variables

Apr 8, 2014

I have a school assignment that involves me sorting an array of objects based on one of the class String variables. I am using as the title says a simple selection sort method. The problem I'm having is that when I run the program in debug mode, it never seems to enter the if statement in the inner loop. I would like to say I've tried a number of things to figure it out, but honestly I'm just stumped as to why it's not working.

Here is the code:

public static void sortTransactions(Transaction[] oTransaction){// This is the sorting method, obviously it's not done so it currently just prints to screen.
System.out.println("Successful call to sortTransaction()");
String min = "";
int curInd = 0;
Transaction[] temp = new Transaction[1];

[Code] ....

The output when I check to see if the array is sorted verifies that the array never does get sorted.

View Replies View Related

Selection Sort Method - Sorting Array And Return Result Of Each Step

Nov 20, 2014

This time I am having difficulties with selection sort method, but not with the method itself (I think). So I need to sort an array and return the result of each step.

This is the main code:

public class Functionality {
public static int[][] selctionsort(int[] a) {
for (int i=0; i<a.length; i++) {
int least = i;
for (int j=i+1; j<a.length; j++)

[Code] ....

And this is the Test folder:

import static org.junit.Assert.assertArrayEquals;
 public class PublicTests {
public static void main(String[] args) {
int[] a = new int[] { 35, 7, 63, 42, 24, 21 };
int[][] b = new int[][] { new int[] { 7, 35, 63, 42, 24, 21 },

[Code] ....

Now I am not sure what should I write in return since the 2nd (test) project has int[][] and in my main project I am working with int [].

View Replies View Related

Sorting Array Into Second Array?

Jan 29, 2015

public class lab
{
public static void main(String args[] )
{

[Code]....

//The program does run but when i run it prints out the new array with a 2 at index 0 and zeros for index 1-6. my objective is to have the new array print out the ar{7,5,2,8,4,9,6} array in order. There is some code I cannot change I have pointed out which one I can't change.

View Replies View Related

Sorting With A Comparator

Mar 26, 2014

I am trying to sort an ArrayList of objects with the comparator as I want to sort based on a certain value for each object. I understand I would need to override compareTo() in the objects class, is there any way I can get around also needing to override for all subclasses of the object?

View Replies View Related

Sorting With Two Stacks

Oct 16, 2014

So we have to ask the user to put in a string of letters, and bring those letters in as cars to where there is a storage area and an assembly area, and we have to sort them from there into the assembly area with the smallest (A) at the head. I think I set up my code pretty well, but when I run it, no matter what I put in it returns CBAo. Say I input KATE, it should return TKEA but instead CBAo or if I input JANICE it should return NJIECA but it just returns EDCBAo. Here's my code:

import java.util.Scanner;
import java.util.Stack;
public class carStacksDessart
{
public static void main(String[] args) {
Stack<Integer> storage = new Stack();

[Code] ....

View Replies View Related

Sorting A Map Alphabetically

Nov 30, 2014

I'm trying to create a method that takes a map and sorts it alphabetically. I think I have the logic correct but I'm getting errors when I run it:

private static void sortMapAlphabetically(
Map<String, String> termsAndDefinitions) {
Map<String, String> tempMap = new Map2<String, String>();

[Code].....

View Replies View Related

Sorting Out ArrayList

Nov 26, 2014

I have to sort out my ArrayList but cannot make it work. Here is

Java Code:

Collections.sort(this.users);
User poor = this.users.get(0);
User ritch = this.users.get((this.users.size()-1));
System.out.println("Poorest user has" + poor.getUsername() + poor.getBalance() );
System.out.println("Ritches user has " + ritch.getUsername() + poor.getBalance() + "
"); mh_sh_highlight_all('java');

The Collections.sort(this.users); does not work.

View Replies View Related

JSP :: Sorting Query In JSTL?

Jun 21, 2006

i am doing a code using JSTL to fire a query. everything come fine except the resule is not sorted as desired. i am putting the code below--

String sort_order=(String)request.getAttribute("sort_order");
request.setAttribute("sort_order",sort_order);
<sql:query var="viewQueryj" sql= "select USER_ID, PERMISSION_ID, USER_NAME from administrator order by ?">
<sql:param value="${sort_order}"/>
</sql:query>

now the resule is always sorted by USER_ID. if i want to sort it using USER_NAME i pass parameter from controller to this page in sort_oredr variable which comes fine but the result doesn't sort by name, only by id. if i hardcode USER_NAME in query then the result is as desired.

View Replies View Related

Sorting A List Error?

Jun 12, 2014

I am reading a file and sorting a list, and I cannot figure out why I am getting an error on line 15 that contains the following code

Collections.sort(sortedContributorList, new Contributor());

This is the error I keep getting:

The method sort(List<T>, Comparator<? super T>) in the type Collections is not applicable for the arguments (LinkedList<Contributor>, Contributor)

import java.util.*;
import java.io.*;
public class myhashTable {
  public static LinkedList<Contributor> sortedContributorList = new LinkedList<Contributor>();
 public myhashTable(){

[code]....

View Replies View Related

Searching And Sorting Algorithm

Jan 2, 2015

I have a code that is meant to read a file and organize all the names from least to greatest salaries. It also allows the user to enter a name to find from the file, and the program finds the name and displays it. I have two errors, and I will show the error in my code

View Replies View Related

Sorting From Lowest To Highest

Feb 23, 2014

I'm very new to Java and ran into a problem. My results are not in order and I'm not sure what I'm doing wrong.

My results come out like this instead of being in order from lowest to highest: "77 99 44 55 22 88 11 0 66 33"

Here's what I have:

class ArrayIns {
private long[] a; // ref to array a
private int nElems; // number of data items
//--------------------------------------------------------------
public ArrayIns(int max) // constructor
{
a = new long[max]; // create the array
nElems = 0; // no items yet

[Code] .....

View Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved