High Scores On Table - Shifting Elements In Array To The Right
Dec 7, 2014
I'm trying to write a program that asks a user how many high scores they want on a table, then the users types the inital highscores and is repeatedly asked to place more high scores on the table, which if larger than any existing high score, will take its place and shift the other scores down.
Although for the shifting and inserting of that next score the code just doesnt seem to be working, the insertScore function is where im getting the main exception, and im not sure why?
import java.util.Arrays;
import java.util.Collections;
import java.util.Scanner;
public class HighScores {
public static void main(String[] args) {
[Code] .....
View Replies
ADVERTISEMENT
Aug 28, 2014
I am trying to figure out how to report the high and low scores with names from a file. The format is like:
6352 J@me$$
663843 BOBBBB1
etc...
This is my code so far.
import java.io.*;
import java.util.*;
public class Assignment1
{
public static void main(String[] args)throws IOException
[code]....
View Replies
View Related
May 22, 2015
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?
View Replies
View Related
Dec 6, 2014
I am stuck on what to put in my functions for this question: 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
here are my functions the insertScore is missing because I am having troubles with it.
public static void initialiseHighScores (int[] scores, int size)
{
for (int i = 0; i < size; i++)
{
scores [i] = 0;
}
}
public static boolean higherThan (int[] scores, int size, int newScore)
{
[Code]...
View Replies
View Related
Sep 29, 2014
a project I am working on. Its a program that creates a singly linked list that stores names and high scores and prints them. For some reason it is printing an entry extra times. Also my remove function is not working properly
GameEntry class:
package project;
public class GameEntry implements Comparable<GameEntry> {
private String name;
private int score;
public GameEntry(String n, int s) {
name = n;
[Code]...
View Replies
View Related
Feb 24, 2014
So I have this 2D array that contains some Object foo.
[f] [f] [f][f]
[3][3][3][3]
[b][b][b][b]
[x][x][x][x]
[ ][ ][ ][ ]
[ ][ ][ ][ ]
[ ][ ][ ][ ]
I want to shift all the rows down one unit, starting for the rows containing x. So the x row is first shifted to row 4. then the b row shifted to where the x row was, row 3 and so on. My question is:
1. How can I check if the next row is empty
2. How do I shift a row down
Here's my attempt:
Java Code:
//board is the 2D array defined elsewhere
public void shiftRowDown(){
int counter = 0;
int rowCounter = 0;
for (int i = 0; i < this.board.length; i++) {
[code]....
Is this correct.
View Replies
View Related
Dec 20, 2014
i am a french student in France and i am studying informatics . For validating my term at the beginning of January i have a projet in JAVA to do called YAHTZEE. It's a game which i have to code WITHOUT USING AN OBJECTS. I already started the coding, i did all the beginning stuff ( to call the number and the names of players, to roll the 5 dices, i made the code for all the scoring points too(rules) ) but i can't do the table score which updates the new scores after each turn ( 3 turns in total) .
View Replies
View Related
Feb 5, 2015
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class array
{
public static void main(String[] args)
[Code] ...
Is there a way to write this, where, alpha is one array.
Write a program that declares an array "alpha" of 50 elements of type "double". Initialize the array so that the first 25 elements are equal to the square of the index variable and the last 25 elements are equal to three times the index variable. Output the array so that 10 elements per line are printed.
If I have an array of 50 integers, can I break that to read in lines of 10?
View Replies
View Related
Jan 12, 2015
I am trying to make a 2d array that keeps track of comparison counts. what I have so far works ok but writes over the previous elements with 0. can't seem to find where I am re-initializing the previous elements.
//this is one of my fill sort arrays
public void fillSelectionArray(int index, long countSum) {
//rand = new Random( );
//for ( int i = 0; i < listsize; i++) {
selectionList[ index -1] = countSum;
// }
[Code] ....
I know I am missing something just not sure what.
View Replies
View Related
Mar 12, 2014
Assignment: Given an array of scores sorted in increasing order, return true if the array contains 3 adjacent scores that differ from each other by at most 2, such as with {3, 4, 5} or {3, 5, 5}.
scoresClump({3, 4, 5}) → true
scoresClump({3, 4, 6}) → false
scoresClump({1, 3, 5, 5}) → true
Codingbat AP-1 3rd question
public boolean scoresClump(int[] scores) {
for (int i = 0; i < scores.length; i++) {
int a = scores[i];
int b = scores[i + 1];
int c = scores[i + 2];
if (Math.abs(b-a) + Math.abs (c-b) <=2)
return true;
}
return false;
}
I got it right for some of the input, but not one of them. I tried to use the for loop and if statement on a specific input that I got wrong:
{4, 5, 8}
scores[1] - scores[0] = 1
scores[2] - scores[1] = 3
I suspect it has something to do with the for loop, but I don't see the problem with it. It should work, shouldn't it?
But anyway, here is the error for {4,5,8} :
Exception:java.lang.ArrayIndexOutOfBoundsException : 3 (line number:7)
View Replies
View Related
Mar 8, 2014
Given an array of scores, return true if each score is equal or greater than the one before. The array will be length 2 or more.
scoresIncreasing({1, 3, 4}) → true
scoresIncreasing({1, 3, 2}) → false
scoresIncreasing({1, 1, 4}) → true
Java Code:
public boolean scoresIncreasing(int[] scores)
{
for (int i = 0; i< scores.length-1; i++) {
if (scores[i] < scores[i+1] || scores[i] == scores [i+1]) {
return true;
} else {
return false;
}
}
return false;
}
Unsure what the problem is, it will always return True for some reason...
View Replies
View Related
Mar 24, 2015
I am taking the Class Algorithms and Datastructures and got an assignment for Lab that really throws me off. The goal is to create an Array out of a given CSV file, implement several Methods that get the size of array, etc.
I am still stuck in the first part where the CSV has to be imported into the Array. My problem is that I need a mechanism that figures out the needed size for the Array, creates the array, and only then transfers the data from the CSV.
The list consists of the following wifi related values:
MAC-Adress, SSID, Timestamp, Signalstrength.
These are on the list, separated by comma. The Columns are each of these, and the rows are the four types of values making up the information on a certain wifi network.
The catch is, we are not allowed to use any of the following:
java.util.ArrayList
java.util.Arrays
and any class out of java.util.Collection.
So far I used the BufferedReader to read in the file and tried to implement the array, but I get an arrayindexoutofboundsexception.
Below is my Code (Its still an active construction zone):
public class WhatsThere {
public WhatsThere(String wifiscan) throws IOException {
}
public static void main(String[] args) throws IOException {
// WhatsThere Liste = new WhatsThere(String wifiscan);
String[][] arrayListe = new String[0][0];
[Code] ....
View Replies
View Related
Jan 22, 2015
When I insert: title, category, year, artist in 4 Strings And when I press "enter" i put those 4 in a array "large", and then when I can start a new music insert with 4 new string elements and add those in the large array..So: I have an array "large" with the length 19 (or so..)(max ~100 or so)Then I what to: get,in a new array "title",the elements:0,4,8,12,16 (from the large array) and put them in a scrollable list. And when I select one element in the title array I then whant to get 3 remaining elements from this. And put it in a array called selection.
So the "large" and the "title" arrays must be dynamically sizes... or be copied to a larger sized..
1 can it be done with arrays?
2 how do dynamically change the size of for example the array "title"?
3 how do I receive the elements 0,4,8,12,16 and so on, (to (
large.length-3) and add it to the title array?
View Replies
View Related
May 4, 2015
What is the sum of the array ar elements after executing the following code?
int[] ar = {1,2,3,4,5};
for (int i=0; i<ar.length-1; i++)
if (i%2==0) ar[i]=i;
My professor says the answer is 12 i dont know how he got that answer
View Replies
View Related
May 17, 2014
I cant get this code to work if wont find the right min and range and its printing out my max three times . This is what I got
public static void main(String[] args)
{
double [] Rainfall = new double[12];
Rainfall[0] = 77.4;
Rainfall[1] = 56;
Rainfall[2] = 61.8;
Rainfall[3] = 62.1;
Rainfall[4] = 57.1;
[Code] ....
View Replies
View Related
Apr 23, 2014
Any way to shift in a range from 0-9 when I already have the shift value.
The reason I am asking this is because I am writing a telephone validation program and I got most of it complete and all I need to do now is the shift an encrypted phone number given to me by the user, and shift it however many times my shift value is.
Example: I am trying to get this phone number, 545-319-8712 to become 212-086-5489. The shift value is 3. So basically since the phone number given to me is 3 numbers higher than the phone number I am trying to get, so if the first number I receive from the user is higher than 2 then I would shift the number the user gave me down by the shift value I have already gotten.
5 shift down 3 = 2, 4 shift down 3 = 1, etc. But I also want to know how I can make a number like "1" to shift down 3 to become 8. This is the range; 0, 1, 2, 3, 4, 5, 6, 7, 8, 9.
If I have to shift a number down 4 spots and I get the number 1 from the user than I want to get the number 1 to first down four times to become 7.
[1] -> 0 -> 9 -> 8 ->[7]
Basically if I have to shift a number down 4 and the number is less than or equal to 3 then I want it to continue from 9 .
Then just reverse the steps if I have to shift a number up, USER gives me "090", i want "212" I shift the number up by 2.
View Replies
View Related
Mar 29, 2015
I have two arrays
private ReservedBook[] reservedBooks;
private LibrarySystem[] libraryBooks;
The library array has two books and I want to copy one of them to the reserved books when you type in the ISBN
public void borrowBook(String ISBN)
{
int i = 0;
if(numberOfBooks < MAX_BOOKS-1)
{
if(libraryBooks[i].getBookISBN().equals(ISBN))
{
for(i=0;i<MAX_BOOKS-1;i++)
reservedBooks[i] = libraryBooks[i];
}
else System.out.println("There is no such book");
}
else System.out.println("You have reached the maximum number of allowed books");
}
It shows me error: incompatible types - LibrarySystem cannot be converted into ReservedBook. How can I fix it?
View Replies
View Related
Oct 27, 2014
So I am trying to create a code that searches if a word is square free. The user inputs a word (into an array) and then the code is suppose to see if it is square free. A word being square free means that the word doesn't contain any consecutive sub words. For example, "abcabc" is not a square free word because abc is repeated, but "abcdabc" is a square free word because there is a "d" separating the "abc".
So far I have this :
import java.util.Scanner;
public class A3Q2 {
public static void main(String[] args) {
// part (a) of the main
Scanner keyboard = new Scanner(System.in);
[Code] ....
I've been trying to experiment with different ways such as checking to see if there any duplicate elements such as,
public static char isSquareFree(char[] word){
for(char i = 1; i < word.length; i++) {
if(word[i] == word[i - 'a']) {
System.out.println("Duplicate: " + word[i]);
}
}
return word; }
}
And other methods but I'm just not getting it.
View Replies
View Related
Feb 24, 2014
This piece of java software I am responsible for is running throughout numerous locations in my country, there is only 1 location which has trouble with the dates being displayed incorrectly, birthdates of clients are displayed as a day earlier. It seems pretty safe to say it is a problem on their end... But still, they're asking me for an explanation as to why java would treat a date differently in this rare case.
The date (a birthdate) comes from an oracle database, it is a date without a time (trunced date). It enters the application as a member of the client-class, it is NOT being set or changed after that. It goes through several layers in the application, then ends up in a GUI class which unwraps it from the "client"-object and puts a SimpleDateFormat (new SimpleDateFormat("dd-MM-yyyy")) on it. My first and only hunch was the timezone settings, but that seems to be OK!
View Replies
View Related
Oct 15, 2014
I have tried to print array elements using standard print statement. I am getting errors. How to print them. Here is my code:
class arrayEx1{
public static void main(String args[]) {
int a[]=new int[3]; //Declaring Single Diomentional Array
a[0]=10;
a[1]=20;
a[2]=30;
int total=a[0]+a[1]+a[2];
System.out.println("Values stored in a[0],a[1],a[2]elements are :" + a[0] a[1] a[2]);
System.out.println("Total values of a[0],a[1],a[2]elements is :"+ total);
}
}
if i give comma (,) in between above print stament (print statement 1) stil i am getting errors.
View Replies
View Related
Mar 20, 2014
public class werek4d {
public static void main(String[] args) {
int counter = 1;
int[] anArray = new int[101] ;
for (int i = 0; i <= 99; i++){
anArray[i] = i + 1;
System.out.println(i + ": " + anArray[i] + " ");
[Code] ....
My aim is to generate a lists containing 1 to 100. I will then count the number of integers divisible by 3. After doing so, I want to delete the integers that are NOT divisible by 3 in the lists. I tried doing it, but I seem to keep on getting the same lists.
View Replies
View Related
Apr 1, 2014
I am working on a assignment that has to do with array lists, it mainly has to do with adding new elements, telling then where it is it located, if the memory block is empty , ect. so far i have been having problems with my indexOf method which should display the array cell number that a input element E is in, and if it is not in there it should display a -1.
public class MyArrayList<E>
{
private E[] data_store = (E[])new Object[2];
private int sizeofa = 0;
private void resize()// makes the array list bigger if need {
E[] bigspacemaker = (E[])new Object[data_store.length * 2];
for(int x = 0 ; x< sizeofa ; x++)
[Code] ....
Error message:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 512
at MyArrayList.indexOf(MyArrayList.java:28)
at MyArrayListDemo1.main(MyArrayListDemo1.java:26)
View Replies
View Related
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
Oct 27, 2014
This is my code for the driver class:
import java.util.*;
public class SavingsAccountDriver {
public static void main(String[] args) {
System.out.printf("%10s%10s%10s%10s%10s" , "Month" , "Account #" , "Balance" ,
"Account #" , "Balance");
[Code] .....
Here is the output I get :
Month Account # Balance Account # Balance
----- --------- ------- --------- -------
0 10002 2000.00 10003 3000.00
1 10002 2002.00 10003 0.00
2 10002 2004.00 10003 0.00
3 10002 2006.01 10003 3030.00
4 10002 2008.01 10003 0.00
5 10002 2010.02 10003 0.00
6 10002 2012.03 10003 3060.30
7 10002 2014.04 10003 0.00
8 10002 2016.06 10003 0.00
9 10002 2018.07 10003 3090.90
10 10002 2020.09 10003 0.00
11 10002 2022.11 10003 0.00
12 10002 2024.13 10003 3121.81
Why is it not showing those months updated balance? I feel like I'm pretty close. I guess I should specify that this is supposed to calculate monthly interest for one account and quarterly interest for the other. This is what the output should look like.
Month Account # Balance Account # Balance
----- --------- ------- --------- -------
0 10002 2000.00 10003 3000.00
1 10002 2002.00 10003 3000.00
2 10002 2004.00 10003 3000.00
3 10002 2006.00 10003 3030.00
4 10002 2008.01 10003 3030.00
... ... 12 ... ...
The quarterly interest is the one that is giving me trouble.
View Replies
View Related
Jul 16, 2014
I have the simple table below:
currency amount
€ 2.0
$ 4.0
£ 5.0
How could I store the currency and amount in an array? A 2x2 array would do this but how to store them and retrieve them is the challenge. For example, I have a method that asks the user for two inputs, the currency and the amount and using the array as a chat table where I could map the currency to the the currency input entered by the user, I could do some calculations with the amount entered by the user. how I could represent the 2x2 array?
View Replies
View Related
Dec 18, 2014
I was trying remove duplicates element from my array without using collection API but i didn't got any output from my code.Although it is compiled successfully but on execution it didn't give any output. I guess there must be some problem in function Duplicate
Java Code:
class Union {
public static void main(String...s) {
Union M=new Union();
int x[]=new int[]{1,0,1,4,10,10,10,3,567,4,3,33};
int y[]=new int[]{5,4,5,4,5,4,2,3,3,1,0};
int []w=M.merge(x,y);
[Code] .....
View Replies
View Related