Numbers Inputted By User And Sorting Them Out In Ascending Order
Nov 12, 2014
I am writing a program that is supposed to take 3 numbers inputted by the user and sorting them out in an ascending order. I believe my code is correct but can't figure out why the program isn't behaving as expected.
import java.util.*; //Required to use the scanner console
public class Week3_Programming_Problem
{
static Scanner console = new Scanner(System.in); //Allows for user input
I am writing a program that grab user input number which represent beats per minute separated by commas. It then parses the numbers and reorders them from smallest to largest and then outputs the average, medium, maximum and minimum number all in separate lines. I am having trouble getting the array to sort the input from smallest to largest. It is currently only working for 3 numbers inputted. Anything more will not reorder it.
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */
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++);
I am trying to make a programm that reads a word and display if the letters are in ascending order or not in ascending order(All kind of letters..capitals..etc). I have made a code but its not 100% correct.
My code:
class Main { public static void main(String args[]) { System.out.print( "#Enter text : " ); String text = BIO.getString(); while ( ! text.equals( "END" ) ) { if (text.equals("END"))
I am trying to make a program that reads a word and display if the letters are in ascending order or not in ascending order(All kind of letters, capitals ). I have made a code but its not 100% correct. My code:
class Main { public static void main(String args[]) { System.out.print( "#Enter text : " ); String text = BIO.getString();
Write a Java program that asks the user to store 10 numbers in an array. You should then print the array in the order entered and then print in reverse order. However, instead of putting all the information in the main, you should use a class called PrintIt. The PrintIt class should have an instance variable that is an array to hold the numbers. You should have a method that will print the array in the order entered. You will also need a method to print in reverse oder. Then create a tester class that asks the user to enter 10 numbers and puts them in an array.
So far I've got this.
public class printIt { int i; int [] numArr = new int [10]; public printIt () { i = -1;
[Code] .....
Output:
Enter a number:
8 34
Forward:
8 34
Reverse:
34 8
end
what i want as an number is being able to print out 10 numbers but this only lets me do two numbers. Why is that so?
tfrreee i want to display given series in ascending order in java prog here is an error : E:Javajdkin>java ascend 505671Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 7 at ascend.main(ascend.java:9)
Java Code:
class ascend { public static void main (String args[]) { int s[]={ 50,71,81,21,7}; int i; for(i=0;i<=s.length;i++)
How do u copy all the elements in an array eg A into another array eg B? This is the question:
An array A contains integers that first increase in value and then decrease in value,
For example, 17 24 31 39 44 49 36 29 20 18 13
It is unknown at which point the numbers start to decrease. Write efficient code to code to copy the numbers in A to another array B so that B is sorted in ascending order. Your code must take advantage of the way the numbers are arranged in A.
This is my program:
This is the error message:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException at java.lang.System.arraycopy(Native Method) at Quest30.CopyAndSortArray.main(CopyAndSortArray.jav a:16)
I wrote displayAscending() and displayDescending() methods to this double linked list and it is not working at all. Logically it seems fine to me. I positioned the head in the beginning in the ascending method; created a variable named data1 as an auxiliar variable so it can store the values that are going to be moved; and moved the values. Same thing for the descending method but instead of the head I put the tail and move left the list, instead of right.
import java.util.*; class node { int data; node left; node right; node(int d, node l, node r) { data = d;
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); }
i need to sort these runners into time order shortest first, the classes both work i just cant get the method sortRunnerList() to work
Java Code:
public class Runner implements Comparable <Runner> { /* static variables */ private static int nextNumber; // To be added by students in Question 3, part (i)(a) and part(iv)(a) /* instance variables */
private int number; // runner's number private String name; // runner's name
I am trying to create a java program to sort an array in ascending and descending order. Here is the program I created :
import java.util.Arrays; import java.util.Collections; public class ArraySort { public static void main(String [] args) { int [] num = {5,9,1,65,7,8,9}; Arrays.sort(num);
[Code]...
BUT I GET THE FOLLOWING EROOR ON COMPILATION
ArraySort.java:12: error: no suitable method found for reverseOrder(int[]) Arrays.sort(num,Collections.reverseOrder(num)); ^ method Collections.<T#1>reverseOrder(Comparator<T#1>) is not applicable
The purpose of this program is to translate a user inputed sentence into morseCode. It seems like everything is right to me, but I'm simply not getting output! What am I doing wrong, or what should I add/change?
Here is the main class:
public class MorseCode { public static String myInput; public static String[] myMorse; public static String myUserInput; public static char[] myAlph = {'A','B','C','D','E','F','G','H','I','J','K','L','M', 'N','O','P','Q','R','S','T','U','V','W','X','Y','Z'}; public MorseCode()
I've been working on this problem for a while now and continue to get an error when I try to subtract one user inputted integer from another. It seems to compile fine for adding, dividing, and multiplying. Why it might be making that error and how to resolve it? (As an aside, I have no idea if I did the whole program right but am just trying to figure out why a declared int would come back with an error it's a string.)
import java.util.Scanner; public class Calculate2 { public static void main(String[] args) { int firstInt; int secondInt; int choice;
In my account driver I am trying to get the user inputted account number to get the account by account number. In my code
System.out.println("Which Account number: "); int account = scan.nextInt(); ac.get(account-1);
This works if my accounts are numbered incrementally starting with one, I want it to match the inputted account number
System.out.println("Account number: "); int num = scan.nextInt();
I am thinking a for loop is probably needed. Here is my code:
public class AccountDriver { public static void main(String[] args) { Scanner scan = new Scanner(System.in); ArrayList<Account> ac = new ArrayList<>(); boolean more=true; boolean again=false;
how to sort my text file. So far I have been able to read the text file and print it back out, but I am unsure of how to go about sorting it. Must print the colors (in the order of the rainbow first) and if the colors are the same compare the size (bigger is more important)The values I have to sort are written as such in the text file:
blue 18 blue 10 red 27 yellow 4
public class Rainbow{ private String color; private int size; public Rainbow(String color, int size){ this.color = color; this.size = size;
[code]....
I would know how to sort it if it was supposed to be alphabetical order or there were only numbers, but I can't seem to figure out how to sort it when there are strings and integers
public class ReverseOrder { public static Scanner input = new Scanner(System.in); public static void main(String[] args){ int[] numbers; double[] reverse;
I have problem with sorting my numbers from smallest to biggest and i need to not have repeating numbers, I am stuck. I tried using Arrays.sorts(lottery) but didn't work. and i don't know to but make the numbers not repeat in the same line.
package lottonumbers;
public class LottoNumbers { public static void main(String[] args) { int[] lottery = new int[6]; for (int numoftimes=0;numoftimes<5;numoftimes++){
trying to write a program that takes a user inputted number and converts it to a binary number.
Here's what I have:
package com.java2novice.algos; import java.util.Scanner; public class Converter { static Scanner console = new Scanner(System.in); public void printBinaryFormat(int number){ int binary = console.nextInt();