Sort 5 Numbers In Java From High To Low With If Statements?
Sep 16, 2014
I am trying to code a program that orders 5 random numbers from high to low with the basic coding i know (im starting to learn theory of methods... so you can imagine) When i run the code i cant get all 5 numbers ordered but my logic says the code is right although it's pretty confusing.
I know you could code in a simpler way but first i wanna get it as it is right now. When i debug (on my own way cause i dont know how to actually use it) shows line 72 with yellow color.
public class NuevaCalculadora {
import java.util.Scanner;
public static void main(String[] args) {
I'm having trouble with sorting Strings- 3 strings inputted by user, and I would like to output them in alphabetical order. I've used the str.compareToIgnoreCase method, and then I've tried to loop them through a series of if/ else statements. Everything I've been able to find online (including the forums here) has suggested to use the Comparator class, or to put the strings into an array, and sort list- I really would like to stick with just the String class, and its methods .
The program itself works and compiles, but I am getting logic errors that I have been unable to solve. I'm using IntelliJ Idea, and I've ran it through the built in debugger, about 100+ times (not exaggerating, lol) just to see what it's doing in particular scenarios. For instance, I can get c, a, b, to print out as a,b,c correctly, but a,b,c, will print out as b,a,c.
For me this is kind of like a Sudoku puzzle, or a Rubik's cube! Each time I fix one scenario, it breaks another one, so I don't know if there's a(logic) solution to fix all possible scenarios (abc, acb, bac etc... to all print abc) or if possibly I just need more if statements. I've only pasted in the area where I'm having problems (the if statements). I'm a big fan of the "Next Line" syntax.
(Note: please assume the non relevant content- import Scanner class, main method, etc... I didn't want to paste the entire program.)
System.out.println("Enter the first statement: "); //input.nextLine(); string1 = input.nextLine(); System.out.println("Enter the second statement: "); string2 = input.nextLine(); System.out.println("Enter the third statement: "); string3 = input.nextLine();
I've just written a program that generates 100 numbers that range from 0 ~ 25 using arrays, the program calls another method that sorts the even numbers into a separate array and returns the array. I need it to display both arrays, however, when I run my program, the numbers of both arrays are mixed together, and I'm not sure how to separate them.
[ public class Array1 { public static void main(String[] args) { int array [ ] = new int[100]; for (int i = 0; i < array.length; i++) { array[i] = (int) (Math.random() * 26);
I am working on my homework, and everything is fine. But I was wondering if I can fix one thing. basically it prompt from a user for a text file name and save the content as an array. some part of my code is here:
public class arraysort{ static BufferedReader kb = new BufferedReader(new InputStreamReader(System.in)); public static void main(String[] args) throws Exception { System.out.print("Enter the first name of the file: "); String input = kb.readLine(); int[] firstArray = readFile(input); firstArray = bubbleSort(firstArray);
Code below. I am not sure if my logic is correct. I want to prompt a user to enter registration numbers from 100 to 1000, store the numbers in an array then sort them.
import java.util.Scanner; class regnumber { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.println("registration number ", 100,1000);
Basically it prompt from a user for a text file name and save the content as an array. some part of my code is here:
public class arraysort{ static BufferedReader kb = new BufferedReader(new InputStreamReader(System.in)); public static void main(String[] args) throws Exception { System.out.print("Enter the first name of the file: "); String input = kb.readLine();
[Code] ....
As you you may figure, it works if I put numbers separated by each lines. I was wondering if I can change it so that it works when I put numbers separated by space rather then each lines.
I'm supposed to write a program, which reads float numbers from a file, puts them in an array and sorts the array. After that I'm suppose to add the numbers so that when I add the 1 and 2 number of the array, I'm suppose to save the sum on the position of number 1, then I add number 3 and 4 and save the sum on position 2 etc. Also if my array has an uneven number of floats it's suppose to add the last 3 and not 2 numbers in the last iteration. The problem is the method throws an ArrayOutOfBounds Exception but I can't seem to find my mistake.
That's the second method. The first one just stores the sum in another variable and then returns it. Also is there a way in that I can Scanner/File/array etc. and initialize the array only once so I don't write the same code two times like it is now.
package sumOfFloats;
import java.io.File; import java.io.FileNotFoundException; import java.util.Arrays; import java.util.Scanner; public class SumOfFloats { public static float sumFloats() throws FileNotFoundException{
import java.util.ArrayList; import java.util.Random; public class NumSorting { public static int numOfComps = 0, numOfSwaps = 0; public static void main(String[] args)
[Code] ....
What is wrong with my code in this sorting program? It won't copy the random generated numbers to the sort method. How to get the random generated numbers to copy to the sort method. Below is what the program is displaying.
I have been assigned to write a program that has a user input random numbers then the program is to sort them in different ways. I have the coding for the different sorts but, I have an error saying that I am missing a return statement in the "Bubble" method. I am trying to return "arr[i]" in the "for loop" which gives me this error, and when I try to take the "return arr[i]" outside of the "for()" loop the error reads the it cannot locate variable "i".
import java.awt.* ; import java.awt.event.*; import javax.swing.*; public class SwingSorts extends JFrame implements ActionListener { JRadioButton bubble; JRadioButton selection;
I am trying to Extract the ranges of Variables from a Text File. I extracted lines of the forms X in 1..10 Y in 12..50 Z in 0..19 / 66/ 95..100 Where X in 1 ..10 states that X takes values from set 1 to 10 Similarly for Y and for Z its a Union of different ranges of the values (0 to 19, union 66,union 95 to 100)
I want to Map these Variables to their respective sets using Hashmap where Key is Variable name and value will be a Set. My Hashmap Signature is HashMap> hm=new HashMap>();
I am trying to write a code that asks the user to enter a temperature for each day of the week and then it prints out the high temperature, the low temperature, and the average temperature. This is the code I have so far:
import java.util.*; import java.text.DecimalFormat; public class Lab10 { public static void main(String[] args) { java.util.Scanner input = new java.util.Scanner(System.in); int high, low, i, sum; double avg; [Code] ....
There are a few issues that I am having.
1. I can't for the life of me figure out how to make it print the low temperature 2. I had it running perfectly until I changed one thing and now I get a compile error saying that the last 5 statements are unreachable. (lines 32-37)
This program will not give me the average high temperature for the week and I cannot figure out why! Every time it only spits out "0" whereas the average low temperature works great..
import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Temps_Grid_Keenan extends JFrame { private static final long serialVersionUID = 1L; private static final int WIDTH = 400; private static final int HEIGHT = 500;
how can I got about sorting an array that contains more than one value in a single element. Such as my array below has 4 values under one element. I know how to sort elements with single values however, slightly confused on this.
import java.util.Scanner; import java.util.Arrays; class Mobile {
when I am programming let say in VB using Visual Studio, finally I build .exe file that can be run on all Windows by double click.For Java I am using Eclipse and to run those apps I am using run from Eclipse.How I can create a sort of "executable" for my Java app that I would be able to run it by file click on Windows or Linux?
I got this code from wikipedia when trying to learn about the radix sort algorithm now I understand that the algorithm sorts by significant digits but it's the code that I'm not too sure about for instance the series of for loops at the bottom what exactly is going on there and why is it mod by 10? Also why are there three different integer arrays a, b, and bucket?
public static void radixsort( int[] a, int n) { int i; int digit = 1; int[] b = new int[n+1]; for (i = 1; i < n; i++) if (a[i] > a[0]) a[0] = a[i];
I have been working on getting the merge sort working, I have a complteted code but am not getting the right results... Here is my code and the results are "876323149" ...
public class MergeSort { private static int[] local; // for use in copying private static int[] a; public static void main(String[] args) { int[] test = {2,3,1,4,7,8,6,3,9};
We have a webservices application that uses Java 1.6.0_43, Spring 3.2.3, CXF 2.6.9 and deployed to Jboss 5.0.1 GA in a LINUX x86_64 centos box. It essentially uses apache httpclient (4.2.2) to call internal services and returns the results back to customers. The application has been running fine for a year or so until early this month when all of sudden, it loaded about 300K classes in a very short time during our regression tests and saturated the CPU usage ever since. Hence the application is no longer responding.
I have been trying to troubleshoot the problem for a while. Tried visualvm, dynatrace. thread dumps. heap dumps... None of them is very effective in capturing what are the classes that are loaded so many times and what path triggered that.
I've playing around with linked lists and methods for sorting. So far I've tested the iterative sort, insertion sort, quick sort and they all worked perfectly. Now, I am trying to implement merge sort that would take a linked list of jobs and sort them according to their priority. I found a few solutions on the web, of which I am trying to implemented this one: LeetCode.
My system is a simple one, I do have a linked list of print jobs, each of which has the priority. The following code should sort my print queue and return the link node of the first sorted element. Here's the code.
//defining a job that has priority public class Job { private int priority;
[Code]....
The problem I've been trying to solve is that I am getting the stack overflow at line
ListNode<T> h1 = mergeSort(left);
meaning that I am getting into a loop somewhere down through the process of breaking the linked list into half, half or halfs and so on.
I have to count the comparisons in the quick sort. I have done it by using a global variable. But I am stuck in doing it recursively.
This is my code.
import static java.time.Clock.system; import java.util.Arrays; public class test { /** * The swap method swaps the contents of two elements in an int array. * * @param The array containing the two elements. * @param a The subscript of the first element. * @param b The subscript of the second element. * */
I have to write the Insertion Sort Algorithm using Java codes and at the same time find the time of execution for different sizes of array, filled with random numbers. If I try to show the numbers inserted into the array randomly, they don't appear at the console.
import javax.swing.JOptionPane; public class Insertion { public static void main(String[]args){ int SizeArr = new Integer(JOptionPane.showInputDialog("Enter the size of teh array")).intValue(); int [] r= new int [SizeArr]; {for(int d=0; d<r.length; d++)
I am a beginning programmer and was learning how to make an XML. It was for a simple game and only needs to hold the highscores of three different levels. Here is what I coded:
try { OutputStream fout= new FileOutputStream("highScores.xml"); OutputStream bout= new BufferedOutputStream(fout); OutputStreamWriter out = new OutputStreamWriter(bout, "8859_1");
[code]....
My issue is that I run this code every time I create the program and the scores are reset to 0. How can I make a small change so that I only run this block of code and create the XML the first time the program is run?