Java Arrays - Entering Lists Only Let To Enter 2 Numbers?
Oct 9, 2014
I am trying to write a code that allows you to input 2 lists and it tells you if they are identical or not. However, when I enter my fist list I can only enter two values and then it asks for the next list. How would I fix this to allow me to enter more than two values?
Also, If the second list is different it has me enter more values that list one.
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// Enter values for list1
System.out.print("Enter list1: ");
int size1 = input.nextInt();
int[] list1 = new int[size1];
for (int i = 0; i < list1.length; i++)
list1[i] = input.nextInt();
// Enter values for list2
System.out.print("Enter list 2: ");
I'm trying to make a program that generates 20 random integers between 1 and 20 and then prints the list of random numbers to the screen. After that, I want to print a different list to screen with the same numbers from the first list only skipping any number that has been already printed to the screen. So two lists are printed to the screen. The first one has 20 random numbers. The second one has those same 20 numbers but only prints the numbers in the first list that aren't duplicated. So if m
y list of 20 random integers contains three 2s and two 14s, only one 14 and one 2 is printed to the second list. Currently, my code generates 20 numbers from 1 to 20 and stores those numbers in an array but I don't know how to print solve the second part of my problem. I don't know how to print the s different list only without duplicate numbers. As a result, my output is nothing because it doesn't print any number from the first list as oppose to skipping only duplicate one.
public void randomNum(){ System.out.println("Twenty random integers: "); int max = 20; // max value for range int min = 1; // min value for range Random rand = new Random(); int[] all = new int[20];
I'm not sure if my understanding of PriorityQueues is correct, so I'm trying to check if my reasoning is valid. I'm supposed to compare the Big-O for arrays and linked lists for the following instructions:
Insert 100 objects having the priorities 1, 2, 3, ... , 99, 100 Big-O for Array: __________ Big-O for Linked List: ___________
Insert 100 objects having the priorities 100, 99, 98, ... , 2, 1 Big-O for Array: __________ Big-O for Linked List(Assume no tail reference): ___________
If my understanding is correct, priority queues take in items randomly with no particular order, but they are removed according to the priority of each element. If what I've said is true, wouldn't that mean that inserting any number of objects would be O(1) for both linked lists and arrays? If the PriorityQueue has no particular order, then wouldnt each add() simply insert something to the next array index/linked list node?
I am trying to do this assignment but I can't get the needed output. Create a program that asks the user how many floating point numbers he wants to give. After this the program asks the numbers, stores them in an array and prints the contents of the array in reverse order.
Program is written to a class called ReverseNumbers.
Example output
How many floating point numbers do you want to type: 5 Type in 1. number: 5,4 Type in 2. number: 6 Type in 3. number: 7,2 Type in 4. number: -5 Type in 5. number: 2
Given numbers in reverse order: 2.0 -5.0 7.2 6.0 5.4
My code:
import java.util.Scanner; public class apples { public static void main(String[] args) { Scanner reader = new Scanner(System.in); double[] numbers; System.out.print("How many floating point numbers do you want to type: ");
The assignment is to: Use arrays to create an applet that lists five of your favorite songs.
The applet should scroll the list of song titles, one at a timeeach song title should start at the top of the applet and scroll to the middle then, scroll off the right hand sideeach new song title should scroll in a different colourthe applet should loop, that is, when it gets to the end of the list, start over again at the beginning of the list
I already have it looping the text with different colors. It's just they all loop at the same time. How to loop them separately. How to switch the program to where the strings will change with content and color on each loop that would work too.
Here is the code I got so far:
import java.awt.*; import java.applet.Applet; public class FavouriteSongs extends Applet implements Runnable { String[] Songs = {"Travelling Man-Chameleon Circuit", "Sing-Ed Sheeran", "It's Time-Imagine Dragons", "I See Fire-Ed Sheeran", "Colder Weather- Zac Brown Band"}; int yPos = 0, xPos = 50; Thread runner;
I have to make a program that prompts the user to enter 10 numbers and at the end it prints out the distinct numbers and then the other numbers that weren't repeated...
I have the part where it prints out the distinct numbers but I stuck on how to make it print out the other numbers that didn't repeat...
import javax.swing.JOptionPane; public class DistinctNumbers { public static void main(String[] args) { String getInput; int input; int[] numbers = new int[10];
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);
Create an integer array with 10 numbers, initialize the array to make sure there are both positive and negative integers. Write a program to generate two arrays out of the original array, one array with all positive numbers and another one with all negative numbers. Print out the number of elements and the detailed elements in each array.
public class problem3 { public static void main(String[]args){ int[] numbers = {1, 2, 3, 4, 5, -1, -2, -3, -4, -5}; for (int i = 0; i<numbers.length;){ if(i>0){ System.out.println(numbers); } else System.out.println(numbers); } } }
How to improve my code. I finally was able to create a program that gives you the corresponding letter grade, when you enter in a numeric grade without using an array. The only issue left is that I have to be able to enter 5 grades at a time, and it give me the letter grade for all 5. I have the programming working, but only am able to enter 1 at a time. I am not sure what kind of loop or if I am supposed to use a loop.
public static void main(String[] args){ { int grade = 0; Scanner input = new Scanner(System.in); System.out.println("Enter : "); grade = input.nextInt(); if (grade >= 90)
This application is supposed to allow a user to enter the names an phone numbers of up to 20 friends, until the user enters "zzz" or 20 names. Then the console is supposed to display the names and have the user enter a name to get the phone number. Here's my code:
import java.util.Scanner; public class PhoneBook{ public static void main(String[] args){ String name; int phoneNumber; final int nameAmount=20;
[Code] ....
I can't seem to get it correct, every time I correct a compiler error, another set of them appear.
I want my function to return an array, with the array holding just the values of data that appear in good.
This is what should be returned:
{3, 5, 3, 2, 3, 3}
What is currently being returned:
{0, 3, 5, 3}
I didn't want to miss any numbers, so I decided to iterate through j for the "good" array, and then just i for the one that I was looking for matching numbers. Perhaps part of the problem is that if the condition is met, it goes to the next iteration of the loop. I'm not sure.
public class Arrays2 { public static void main(String args[]){ int [] data = {8, 3, 5, 3, 7, 2, 8, 3, 3 }; int [] good = {5, 2, 3, 2}; int [] result = f2(data, good); for (int i = 0; i < result.length; i++){
Write a program to maintain a list of the high scores obtained in a game. The program should first ask the user how many scores they want to maintain and then repeatedly accept new scores from the user and should add the score to the list of high scores (in the appropriate position) if it is higher than any of the existing high scores. You must include the following functions:
-initialiseHighScores () which sets all high scores to zero.
-printHighScores() which prints the high scores in the format: “The high scores are 345, 300, 234”, for all exisiting high scores in the list (remember that sometimes it won’t be full).
-higherThan() which takes the high scores and a new score and returns whether the passed score is higher than any of those in the high score list.
-insertScore() which takes the current high score list and a new score and updates it by inserting the new score at the appropriate position in the list
Why do I make private Node<AnyType> next;And why do I have an inner class of Node for a linked list?I had the same topic in C, but there it was somehow easier than in java. Because there you have pointers.
I have two JComboBox the first contains a list of patients, the second a list of antibiotics I want to make a button when I chose an antibiotic and a patient they will be added in my database (sql server)
I'm working with Doubly Linked Lists and using Java Generics..
My nodes looks like this: class DNode<E> { DNode<E> previous; DNode<E> next; E element;
//and all methods inside }
My list of Nodes looks like this: class DLL<E>{ private DNode<E> head; private DNode<E> tail; private int size;
[code]....
As you can see, as arguments they get "E o"...I need to write a program, which from the main function asks the users how long is the list, and after they type it's length, I ask them to start typing the elements (integers)...and this is how my main method is written, but I can't seem to make it work, specialy when I call the "insLast" method,I guess it's because the arguments i'm giving to the function...how to read the elements and write them into the list?
public static void main(String[] args) throws IOException { DLL<Integer> lista=new DLL<Integer>(); BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in)); String s = stdin.readLine(); int N = Integer.parseInt(s); s = stdin.readLine(); String[] pomniza = s.split(" "); for (int i = 0; i < N; i++) { lista.instLast(Integer.parseInt(pomniza[i])); }
import java.io.*; public class pho { public static void main (String args[]) throws IOException { BufferedReader br = new BufferedReader (new InputStreamReader (System.in)); String name = new String [50]; long phn = new long [50]; System.out.println("enter A for adding phone numbers");
how many integers the user wants to use. The user will enter for example a 4. The user inputs a 2, 4, 6, and 10. So then we get our outputs...Then the code will ask if you want to run this program again. My question is, if the user inputs a -1 for example 2, 4, 6, -1....the code will not continue. I wanted to use a while loop, such as while (scores != -1) but it doesn't work.
Enter the amount of integers you want to use4 Intenger # 1 2 Intenger # 2 4 Intenger # 3 6 Intenger # 4 10 You entered 2.0 4.0 6.0 10.0 Average = 5.5 Variance = 8.75 Standard Deviation = 2.96 Do you have another set of numbers?
I am working on allowing a user to enter an ID # using JOptionPane. If the ID is valid, then it will display the ID#, student name, and GPA. If it is not valid, then the window should pop up again to allow the user to enter a valid ID. I have managed to get that far.
I am struggling with once a valid ID # is entered, how do I get the loop to stop? I have tried several different ways, but the code below is the most recent one, and it is still not working!
import javax.swing.*; public class StudentIDArray { public static void main(String[] args) { String enteredID; int idNumber; int x = 0;
I have problem with this applet. the browser gives page.I don't know wether its not loading or if its extremely slow.Also I am confused on the html part.Should I use tag object or applet and for entering class name data,classid or name.
import java.lang.reflect.InvocationTargetException; public class NewJApplet extends javax.swing.JApplet { @Override public void init() { this.add(fesText); this.add(jButton1); this.add(jLabel1); this.add(jLabel2); this.add(jPanel1);
i am writing a program that would be used for ticket sales and their are 100 tickets and a person can only buy 4 at a time. i need my program to not go negative at the end and also i need to stop the user from entering a negative number.
import java.util.Scanner; public class ticketmaster { public static void main(String[] args) { // Variable decoration uses strings , doubles and final doubles. Scanner in = new Scanner(System.in);
i am writing a program that would be used for ticket sales and their are 100 tickets and a person can only buy 4 at a time. i need my program to not go negative at the end and also i need to stop the user from entering a negative number here is the code.
import java.util.Scanner; public class ticketmaster { public static void main(String[] args) { // Variable decorationuses strings , doubles and final doubles. Scanner in = new Scanner(System.in);
I mainly would like to know is a int[4][4] or a int [4*4] is more efficient to use. Which takes more storage? Which requires more to process? that kind of stuff.
If I try CLASSPATH: C:Program FilesMicrosoft JDBC Driver 4.0 for SQL Serversqljdbc_4.0enusqljdbc4.jar I receive a "Could not find or load main class" error.