Equivalent Arrays - Comparing 2 User Inputs To Determine If Same
Mar 4, 2015
I have been stuck on this for the past 2 hours, basically we have to compare user input, put user input into an array and compare for equivalency. An example of the program:
Array 1 & 2 are equal
Array 1 & 3 are not equal
Array 1 & 4 are not equal.
This cannot be done by importing Java.util.Array at all!! (my research only find this to compare arrays ) which is why I am having trouble starting off. My code thus far:
import java.util.Scanner;
public class Lab07b {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter Integer 1:");
int a = input.nextInt();
Is it possible to compare 2 arrays as below if they have same size?
Int array[][] = new int[10][10]; array1[I] == array2;
Or assign the whole array to another array? array1[I] = array2[I];
Suppose I have 2 arrays, 1 is old, and 1 is new.
I will do the calculation to update the new array.
Then, I will compare between the 2 arrays.
If they are the same, I will stop the calculation. If they are not the same, I will assign the new array to the old array. Then, I will do the calculation again until the old array and the new array are the same.
As I do not want to write a loop to compare and assign the value to the arrays. May I compare or assign the values to the whole array directly.
Write a java program that will ask a user to input grades until the user inputs the sentinel value -1. The program should test each input to determine whether 0<=grade<=100. If the grade entered is within this range, the program should print "Grade accepted;" if not, the program should print "Invalid input".
I’ve been working on this problem for a couple days already but I came to a point where I don’t really know what to do. Basically I’ve got two arrays filled with int values (0, 1, and 2), which I get from Class B through their respective getMethods.
1 stands for black and 2 stands for something else.
The program counts how many times the value 1 (if chosen color is black) occurs in both arrays and then compares the both counts. If it detects any difference the program does something else, otherwise it waits.
public class A { private static boolean finished; public A() { B objectClassB = new B(); int[] numbers = objectClassB.getNumbers(); int[] numbers2 = objectClassB.getNumbers2(); String color = objectClassB.getColor();
[Code] ....
Here the class B.
public class B { private int[] numbers = new int[] { 2, 2, 0, 2, 1, 0, 1, 2, 1, 1, 0, 2, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 2, 1 }; private int[] numbers2 = new int[] { 2, 2, 0, 2, 1, 0, 1, 2, 1, 1, 0, 2, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 2, 0 }; private String color = "black"; public B() { } public int[] getNumbers(){
[Code] .....
this would be the Output:
[2, 2, 0, 2, 1, 0, 1, 2, 1, 1, 0, 2, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 2, 1] [2, 2, 0, 2, 1, 0, 1, 2, 1, 1, 0, 2, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 2, 0] number of black tokens of first array: 8 number of black tokens of second array: 7 the number of black tokens has changed
The actual problem is that I will be getting one array at a time (meaning: the array int[] numbers from Class B updates itself and might change its values). I just declared and initialize both arrays for illustration purposes.
That would mean I need to hold the array’s values the first time I get them in a temporary variable until I get the values of the updated array. Then I could use them in the method –numberOfRelevantElements- and check if any changes have occurred.
What would be the best approach for doing this? I though of inserting the different counts that I get into a queue and then comparing these values one after the other. But I’m not really sure if that would work.
The international Olympics Committee has asked you to write a program to process the data and determine the medal winners for the pairs figure skating. You will be given the following file: Pairs.txt
Which contains the data for each pair of skaters. The data consists of each skater's name, their country and the score from each of eight judges on the technical aspects and on the performance aspects. A typical record would be as follows:
Smith Jones Australia 5.0 4.9 5.1 5.2 5.0 5.1 5.2 4.8 4.3 4.7 4.8 4.9 4.6 4.8 4.9 4.5
The final score for each skater is the sum of the average of the two categories of score. Design a class to hold the above data and the final score. Read the data in from the file and calculate the final score for each pair. Sort the array of objects , and display the results on the screen in order, giving special prominence to the medal winners.
Here is my CLass:
public class Skaters { private String name1; private String name2; private String country; private double [] arrTech = new double [8]; private double [] arrArt = new double [8]; private double score;
[code]...
how do i print my 2 arrays using the file? i got the name1, name2 and counntry to print but im stuck on printing the scores that are stored in the arrays. Also, i have to print the average for each array. one is for techniques and the other one is artistic. as you can see i already wrote the code for it but im stuck in printing it.
/* Purpose: To write the methods and the rest program. The program should fill a 4 X 4 2 dimensional array with random numbers between 100 and 200. The program should then determine and print the largest and smallest values within the array using two Methods Largest and Smallest. The program should then determine and print the number of values within the array that are even using a function called Even. The program should also enter a loop that will only terminate when the user inputs a -1 as a choice. The loop should continue to ask the user to guess a number that was randomly generated. The program should call the Findit function to determine if the number was in the array. The program should print out the values in the array when the user selects a -1 and then terminate. */
import java.util.Scanner; import java.util.Random; public class LNFI_2DArray { public static void main(String[] args) { int guess; int[] array = new int[4];
[Code] ....
I just had this code working, then all of a sudden i was hit with a 'keyboard leak' error code.
I have a LoginScreen class and one other class in my project. I want use user inputs (Username And Password) on other class if users details is true. I am stcuk at this point because when user attempt to Login connection is succesfully and other frame is coming to screen but i can reach user details anymore. Let me explain with codes;
LoginScreen class
/* * 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. */ package deneme3; import java.sql.Connection;
[code]....
You can see Button Action in MainProgramWindow " System.out.println( ??? );" How i can retrieve users inputs from this class ?
We are writing our own classes and methods. My instructor has provide the code
/*------------------------------------------------------------------------- import java.util.*; public class Assignment5 { public static void main (String[] args) {
[Code].....
I am having trouble with the question counter. I need the counter to increase when c, d or e are entered. I think I need to set up a if or while loop but I'm not sure how to setup the variables. This is what I have for the counter so far.
I realized that I was not filling the array with anything but now I'm having issues filling it correctly. It's only filling the array with three of the user inputs ....
import java.util.Scanner; public class DriverSort { public static void main(String[] args) { Scanner scan =new Scanner(System.in); Sorter sorter = new Sorter(); int choice; // variable which says which sorting algorithm to use
I need to generate a league table in Java based on results provided in the shape of user input. I have the 6 teams in an array. I need to ask the user how many wins, draws, losses each team has had.
for (String element : teams) { System.out.println( "Please enter the number of wins, draws and losses for " + element); int[] wins = new int [6]; int[] draws = new int [6]; int[] losses = new int [6]; }
How do I pull 3 separate values from the user and then store each value in a separate element of each array? so if one team wins 3, draws 2 and loses 1 - I need to put 3 into the wins array, 2 into the draws array and 1 into the losses array? I then need to total all this in a table.
I'm working on an assignment for my Java Programming class that requires me to work with Multiple Classes.
Overview of the assignment: Create a "Database" that allows a user to input information about a College Class.
My program has three Classes:
The information that must be tracked in the Course Class is: - Section Number - Instructor - Classroom - Students (an ArrayList of class Student)
The information that must be tracked in the Student Class is: - Name - Major - GPA - Hours
The Driver class will use the main method to print the user a prompt asking for inputs, then display all of the user's inputs after he/she wished to exit the program.
I'm having no trouble with asking the user for inputs and then checking the validations for the inputs, I'm currently struggling with return a String that houses all of the user's inputs for the variables above. The information must be returned in a method called toString(). Both the Student and Course Class must have a toString() method that returns the information in the class (value of the variables), I must then call upon these methods from the main method of the Driver Class to print them back to the user.
The toString methods within the Student & Course Classes are at the bottom of the code
To Summarize: What I need to do within the toString() methods to return the values the user has inputted for the various variables? Such as.. Should I pass something through the method? Do I need the this keyboard to pass through the variable again? I just fear that I'm currently misunderstanding a very critical part of programming.
My Driver Class
import java.util.Scanner; public class Driver { Course courseInputs = new Course(); Student studentInputs = new Student(); public static void main(String[] args) {
import java.util.Scanner; public class CountStringNumbers { // show the number of Strings that were numbers . . .. public static void main(String []args){ int count = 0; int countNumbers=0; Scanner input=new Scanner(System.in);
[code]...
can count the numbers from 0-9 if the user puted but if it is 10 it doesnt count it , and i need to write all the numbers to have that option. . . . Is there an easy way that includes all the numbers? like if(word.equals(number)){<<<???? (numbers) i know htat doesnt work but is there anything similar ?? ??
countNumbers++; } System.out.println("Type Something , , , and to end the the termination type STOP"); word=input.nextLine(); count++; } System.out.println("You wrote "+count+" lines wich from those lines "+countNumbers+" were numbers and the programe terminated "); } }
I have an assignment where I'm supposed to write a program where the user inputs a month between 1-12 and then the right amount number in every month. I've tweaked the code a bunch of times, but I keep getting this error message:
insert "}" to complete StatementKALENDER_3.java insert "}" to complete BlockKALENDER_3.java
The code:
import java.util.*; import java.util.Scanner; public class KALENDER_3 } public static void main(String[] args){ Scanner scanner = new Scanner (System.in); System.out.print("Skriv ett manadsnummer: ");
[code].....
I think I have just forgot to put in a few { } symbols, maybe an open loop?
I'm developing a Swing application for the first time. To test my application, I have currently hardcoded some text labels and fields in a JPanel, so that a user can enter input details for a maximum of 3 segments. But this limits the maximum number of segments to 3. However, the requirement is that when the user enters the number of segments (could be greater than 3 as well), corresponding number of input sets need to be taken.
Screenshot attached, with values entered.
Basically I want to know how I should go about taking the inputs from the user.
I was tasked with building a program that counts vowels in a phrase or word. I have this working properly, but the problem arises in the next situation. Here is my code:
Scanner userInput = new Scanner(System.in); int userDecision; String Choice; String Phrase; int vowelCount = 0; System.out.println(" This program will count the number of vowels in it.
[Code] ....
As you can see, I used a 'for' statement to declare how long the program should run. However, I need the user to be able to say when it ends. For example, at the end of the program it must say "There are ______ number of vowels in your word. Please press '1' to run the program again, or any other number to exit."
I understand a do-while loop is needed here, but every time I try and create an adequate loop with no logic errors, I either break my existing code, or the program does not work correctly.
Basically, my question is, what is the best way to perform this task? Is a do-while loop necessary, or is there a better way?
Write a program that detects Fibonacci numbers. Prompt the user to input a positive integer. Upon input, the program will determine if the number is either a Fibonacci number or not. If a Fibonacci number, then the order of the number in the sequence must be output. If not a Fibonacci number, then the Fibonacci numbers above and below it (including their order in the sequence) must be output. Once it finishes, the program will prompt the user for a new number. The program will exit if the user enters a string (such as “quit”) instead of an integer. Use the sample output file, fib-seq-det.txt, to view a sample session
This is my project, I wrote a programs that tells you if the input number is a fibonacci number or not. For some reason it only works for some Fibonacci numbers but not all of them.
import java.util.Scanner; public class While { public static void main(String[] args) { System.out.println("Welcome to the Fibonacci Sequence Detector "); Scanner in = new Scanner(System.in); System.out.print("Please input a number for analysis: "); int input = in.nextInt();
I am having an issue with using FileWriter to print some text to a text file. In the following code, I am supposed to be able to print the initial attributes and the user changed inputs into a file but all I am getting is the memory locations of the objects I created in one long line.
package project3final; import java.io.*; import java.util.*; public class Project3Final { static class Instrument { char [] stringNames = {'E', 'A', 'D', 'G', 'B', 'E'}; private final String instrumentName;
I am trying to do is get a user to input their name into the system. Once the user has inputted the name into the system, the system should check to see if the name entered matches "alice". If this is true then the system should print "welcome to the system alice". If it is not alice it should not do anything.
Here is the code I have so far... The fault/ error seems to be with the sInput string in the IF statement.
import java.io.*; public class Hellohuman { public static void main (String[] args) { try { InputStreamReader isr = new InputStreamReader(System.in);
Following is the code for my assignment where a user inputs his full name in a string from keyboard. It should be done using BufferedReader. The program will give his initials as output.
############################
For example: User inputs-> Ram Kumar Das
Program Returns-> R.K.D.
I am written the following piece of code. It does not return the correct output. If "Ram Kumar Das" is given as input, it gives R.K.K. as output. Arrays and objects are strictly not allowed.
Java Code:
import java.io.*; public class Assignment { static int i=0; static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); public static void main (String args[]) throws IOException
Logic her eis when user selects option1, it asks for user to enter name and as soon as user enters name, it should compar name to existing names in txt file. I have user while loop and for loop but for some reason, it doesnt compare properly as there seems to be some mistake in looping and it just read first line or you can say single line rather than comparing it with all lines in txt file. i have attached votes.txt file with this. Also, if user doesnt exists infile, it will ask user to enter vote as yes or no and add it to file and then count total number of Yes and No votes from file and compare them.
my votes.txt file looks as below with two columns namely (name and vote).
Hiren No samir yes bob no rikul no master yes patrick no summer yes bhanja no
I'm trying to learn Java and my current project is to write a short program to determine the factorial of a number entered by the user. I haven't looked. There may be a method that will do it, but I want to use a for loop specifically.
What I have compiles just fine. I'm actually pretty thrilled just with that. Here is what I have:
class factorial { public static void main( String[] args) { Scanner scan = new Scanner(System.in ); int num; int product = 1;
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
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 am new to using arrays. I need to collect user input for book title, author, and # of pages... store that in an array... and then I'm going to need to be able to sort that array. The dialog boxes come up prompting the user for 5 sets of title, author, and # of pages, but when I try to display that information, it isn't working. I am assuming this means that it's not storing the information correctly... so I want to get this corrected before I even try to sort??
import javax.swing.*; import java.util.*; class LibraryBookSort { public static void main(String[] args) { LibraryBook[] someBooks = new LibraryBook[5];
I was just wondering about the equivalent of c function getchar() in java? I am trying to assign letters to array elements through input stream. A simple code in java that matches this code:
How to make sure that a variable passed to a method isn't altered by the method? I know in C++ you can do something like
void aMethod(const Object &item) { ........ }
I know that you can stop a variable from being reinitialized in java by doing this
void aMethod(final Object item) { ......... }
However, that won't stop it from calling a setter on the item or changing something in it. Is there some other keyword out there that can do this? I just found that java DOES recognize the const keyword but that it really is useless.
So, any practical way that const can be further approximated in java beyond using final?