Placing Numbers Inside Array

Apr 3, 2015

"on each step of the loop you can calculate an entire year as opposed to a single month, otherwise you can calculate monthly, but instead of printing, create an array and fill it up with your solutions, then have a second loop where you add them up by dozens to print yearly values, alternatively, have a counter k that adds up to 12, and two variables for adding up the monthly results, increment these after you calculate the monthly value, and whenever k hits 12, print them and reset to zero. print a final time once you leave the loop. "

int year = loanDurationYears-14;
int[] years= new int[loanDurationYears];
periods = loanDurationYears*12;
for(int i=0;i<1;i++)
{
for(year = 0;year<loanDurationYears;year++)
{
years[year] = year+1;
 
 [code]....

View Replies


ADVERTISEMENT

Placing Randomly Chosen Words From Text File Into 2D Array

Apr 5, 2014

I have an assignment for college that involves placing randomly chosen words from a text file into a 2-d array. I have nearly completed ithowever I am having difficulty with a list of string type.What I have done so far is below,

import java.util.Arrays;
import java.util.List;
import java.util.ArrayList;
import java.io.*;
import java.lang.Math;
 
[code]....

when I try to run an instance of the program(using BlueJ) I get an error saying there is a null pointer exception at line 35 which is wordsForGrid. add (puzzleWords.get(pos));

I am thinking that this is related to the fact that at this line in the loadWordsFromFile method List <String>words=new ArrayList<String>(); I could not create an empty list of string type, i.e List<String>words = new List<String>();

Is this correct? Or am I looking in the wrong place. The textfile I am using contains over 6000 words on a separate line for each if that makes any difference.

View Replies View Related

Numbers Not Displaying Inside Linked List

Apr 26, 2014

Java Code:

class MyLinkedListH1
{
private class Node
{
private Node link;
private int x;
}
private Node first = null;
public void addFirst(int d)
{
Node newNode = new Node();

[code]...

why this isn't displaying the last digits of the linked it actually isnt displaying any numbers in side the linked list.

View Replies View Related

Counting The Frequency Of Numbers Inside A Text File

Apr 7, 2014

I have a source code here that counts the frequency of alphabetic characters and non-alphabetic characters (see the source code below).

import java.io.*;
 
public class letterfrequency {
public static void main (String [] args) throws IOException {
File file1 = new File ("letternumberfrequency.txt");
BufferedReader in = new BufferedReader (new FileReader (file1));
 
[Code] ....

But, let's just say that now I have the following characters in the text file, "letternumberfrequency.txt":
71 geese - 83 cars - 58 cows- 64 mooses- 100 ants- 69 bangles- 90 molehills - 87 noses

The numbers inside that text file would be considered as strings, am I right? But I want to extract the numbers so that I can also be able to count their frequency - not as individual digits but as whole numbers (that is how many "71", "83", "58", "64", etc. are there...). Would using "Double.parseDouble ()" work?

View Replies View Related

Generate 100 Numbers Using Arrays - Sort Even Numbers Into Separate Array And Display Both

Apr 24, 2014

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);

[Code] .....

View Replies View Related

Why Can't Populate Array Of Objects From Inside Constructor

Feb 21, 2015

The experiments will slowly converge towards one big experiment: a simple game. I have just a little interest in games (perhaps I should have more), but making one - even a simple one - should be self rewarding.

However, now to the point.

* The experiment creates an array of rectangle objects.
* The rectangles are painted inside a Frame object at random x,y coords generated by a random number generator
* The rectangles are stationary.
* The rectangles are each assigned their own random colour.
* The array of rectangle objects is created inside the constructor of the class.

The actual code contains various other variables and methods which would distract from a quick analysis, so below is code which has the same logical structure which also fails (instead of array of rectangles, I have used arrays of integers).

import java.util.Random;
/**
* Experiment 14 - see if it works simply - (with integer arrays)
*/
public class TestingArrays {
// instance variables
int N = 10; // the size of the array - 10 elements
int[] a;

[Code]...

View Replies View Related

Placing Mines And Another Point On A Field

Sep 29, 2014

I need to fill up 1/10th of the board with mines but i am only able to successfully put 1! also my goal is not showing up on the game even though i put it as an up arrow.

package Homework4;
import java.util.Random;
import java.util.Scanner;
public class HW4 {
//Creates a new type to be used to create the board

[Code] ......

View Replies View Related

Populating Array Doesn't Work Inside Constructor

Feb 24, 2014

Java Code:

import java.io.*;
import java.util.Scanner;
public class asciiFile {
int height;
int width;
Scanner input;
char[][] poop;
public asciiFile(File f) throws FileNotFoundException{ //constructor

[code]...

The constructor is supposed to take an ASCII file, take the numbers in the file, and populate a 2D array with the numbers in the file.

For some reason, the for loop I use to populate the array works outside of the constructor. When I put it in one of the methods, it runs normally. However, when I keep it in the constructor, I get the following error:

Exception in thread "main" java.lang.NullPointerException

at asciiFile.<init>(asciiFile.java:16)

at main.main(main.java:6)

View Replies View Related

Placing Code On Top Of A Background Image In JSwing

Dec 10, 2013

I'm making a pong game but I would like to use a basketball court image behind my code to give it a prettier look (the ball is also a basketball), but I can't seem to get my code to appear on top of my basketball court image, the image just overlaps my code.

View Replies View Related

Placing Integers In Linked List From Text File?

Mar 30, 2015

Trying to read integers from a text file and place them in a linked list in order. Program is compiling and running fine, just not the way I am intending when I am calling the methods. (Note: You have to enter the file location in the var x if you plan on running this.)

import java.io.*;
import java.util.*;
 public class Node {
public int value;
public Node next;
  Node(int x) {
this.value = x;

[code]....

View Replies View Related

Swing/AWT/SWT :: Updating DefaultTable Model - Placing Text?

Jul 2, 2014

I have been going in circles trying to update a JTable. For now I just need to take input from a textfield after clicking on a JButton and have that text be placed in the JTable. LeftPanel listens, while RightPanel holds the table. Here is what I have so far:

package reminder.views;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class LeftPanel extends JPanel {

]Code] ....

View Replies View Related

Reading Entire Integer Text File And Putting Inside Array?

Apr 5, 2014

I have an assignment on sorting, i kno i can get the sorting down but im having an issue with inputing the 512 ints in a file into an array. the instructor provided us with a file with 4 equal sets of ints. i tried to make my array of size [scan.nextInt()] and it cuts off the last 21 ints. and skips the first int. how can i get all of the integers in the text file into my array? this is what i have so far. if i hard code the array to size 50000 and then try to print the array it compiles but errors out when running it.

System.out.println("Please Enter text file in this format, XXXXX.txt :");
String file =fileName.nextLine();
Scanner scan = new Scanner(new File(file));
int [] data = new int[scan.nextInt()]; <-------here it skips first int
int count= data.length;
for (int i=0; i<data.length-1;i++) {
data[i]=scan.nextInt();
}
System.out.print(Arrays.toString(data));

rst 4 ints in output are: 501, 257, 390, 478...., supposed to be 492,501,390....and last ints are: ....88, 83, 79, 0 and supposed to be :88 83 79 77 76 72 71 71 66 57 56 48 48 41 33 30 23 23 18 17 15 13 9....it replace last ints with 0. why ? and how do i fix this. attached it the text file

View Replies View Related

Placing File In Java Application Accessible For Both Main And Test

Oct 1, 2014

I have style sheet file (e.g. myStyle.xslt). I am trying to decide where to put this file in my web application so that both src and test can access it. All my source in packages inside /src folder and test code in packages inside /test folder.

View Replies View Related

Unable To Print A Loop Inside Array Of Greater Size Than Loop Itself

Jan 27, 2015

I am trying to print a loop inside an array of a greater size than the loop itself. Like for example I have an array of size 7 but it has only 3 elements.

int[] array=new int[7]
array[0]=2;
array[1]=3;
array[2]=4;

now what I want to do is print these three numbers in a loop so that my array[3]=2;array[4]=3;array[5]=4 ...... till the last one. Also the array could be any size and not just 7.

View Replies View Related

Resize Array And Add Numbers

Oct 21, 2014

I need to make a code that will ask the user for an array size and the numbers that go in the array. Then it must ask for a new size and copy the numbers from the first array and add numbers to fill the new array.

import java.util.Scanner;
import java.util.Arrays;
public class Lab07{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("How many numbers do you want to enter?");

[Code] ....

I can make the first array but I am getting stuck on the second this is what I am getting back

How many numbers do you want to enter?
3

Enter the 3.0 numbers now.
7
1.2
9

These are the numbers you have entered.
[7.0, 1.2, 9.0]

How many numbers do you want to enter?
5

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
at Lab07.resizeArray(Lab07.java:27)
at Lab07.main(Lab07.java:18)

how do I get the new array and not the error

View Replies View Related

Storing Set Of Numbers In Array?

Oct 9, 2014

I trying to write a piece of code which takes a set of numbers or even data type and stores in an array (or something more suitable).

For example:

Date N1 N2 N3 N4 N5 Day
10/11/2012 10 09 32 100 15 Thursday
11/12/2013 01 0 0 23 50 51 Tuesday

I'd like to be able to sort them so that if I want to search for one of the entries, I can then create a function which allows me to sort them by date or even return all the numbers by a given date or day, etc....

However, I'd like to be able to set it up so that each set is "linked" meaning that again that I can search by date and it returns everything at that date.

I wanted to use an array but I don't know:

-How to do it?

-Whether this is a suitable approach?

View Replies View Related

How To Add Numbers In A Column In Multidimensional Array

Mar 13, 2015

import java.util.Scanner;
public class ColumnSum {
public static void main(String[] args) {
Scanner s = new Scanner (System.in);
int userpick = 0;
int sum = 0;
int [][] matrix = {{5, 9, 87, 74, 12, 7}, // row 1

[code]...

Right now my code gets all the numbers in a row and adds those up, but I want it to get the numbers in a column and add them instead. The problem is I don't know how to get the userpick (the number that the user picks to determine which column gets added) to be set to that particular column.

View Replies View Related

How To Print All Numbers Of Array In Java

Jan 1, 2015

This is my code

package com.arraydemo;
import java.util.TimeZone;
import java.util.concurrent.TimeUnit;
public class ArrayStructures {
public long[] theArray;
public int arraySize;
public ArrayStructures(int size)

[code]....

and i am getting this output

----------
! 1712 !2156|
----------
! 1713 !4583|
----------
! 1714 !3981|
----------

[HENRY: 6000+ LINES DELETED -- Isn't it a bit ridiculous hard to read when you flood a post with thousands of output lines?]

----------
! 4998 !3094|
----------
! 4999 !836|
----------
12:10:56

I am expecting Number to be print from Index ) but they are not printing ,why?

If I use arraysize like 2000 I am getting all number starting from 0 to 1999 .why?

View Replies View Related

How To Get Sum Of All Numbers In A Row Of 2D Array With Row Being Selected By User

Mar 9, 2015

I just learned about 2D arrays and am still trying to get a grasp on the concepts. I'm a little confused by how you return all the values in a row to add up and display the sum if the row is entered by the user.

import java.util.*;
public class RowSum {
public static void main(String[] args) {
Scanner s = new Scanner (System.in);
int userpick = 0;
int sum = 0;

[Code] .....

View Replies View Related

Program That Use Array To Store 10 Numbers

Oct 16, 2014

i'm trying to write a program that uses an array to store 10 numbers. The numbers should be randomly generated ( Math.random() ), and they should be between 1 and 100 ( 1 and 100 inclusive ). The program should produce an output like the one below:

Element 1 = 23 ( Odd )
Element 2 = 15 ( Odd )
Element 3 = 32 ( Even )
Element 4 - 10 ( Even )
Element 5 - 99 ( Odd )
Element 6 - 1 ( Odd )

[Code]...

I have written code for this but its only showing me 0's after first number can you check whats wrong with my code. my code is.

class even/odd{
public static void main(String[]args){
int y=0;
int z=0;
int[] array= new int[11];
for(int x=1; x <array.length ; x++){
array[x]= (int) (Math.random()* 100);
 
[Code]...

View Replies View Related

Using Do While Loop With Array For Phone Numbers

Apr 20, 2014

I need a phonebook that allows the user to input up to 100 numbers but stop when they want to. I have the code for the array and input and output. I thought I had the do...while loop correct for asking the user if they want to input another name but its not working, even when you input "N" it still keeps going. Here's my code.

import java.util.Scanner;
import java.util.ArrayList;
public class PhoneEntry2 {
int x;
char repeat;
final int maxContacts = 100;
String[] pNums = new String[100];

[Code] ......

View Replies View Related

How Many Numbers In Array Have Value Greater Than Or Equal To 100

Sep 10, 2014

I have an assignment that wants me to write a Java function based on induction to determine how many numbers in an array have a value greater than, or equal to, 100.

I have started with:

Java Code:

int recurseHundred (int [] A, int n) {
//n is the number of elements in the array.
//Base case:
if (n == 1 && n >= 100) return A[0];
//Recurse
int num = recurseHundred(A, n-1);
if (n-1 >= 100) return A[n-1];
else return num;
} mh_sh_highlight_all('java');

I don't think this actually does the trick.

View Replies View Related

How To Shuffle Array Using Random Numbers

Nov 10, 2014

For a project we have to "shuffle" items in an array using random numbers. We are supposed to generate random numbers and use those numbers to exchange array elements. But I am not sure what that means, "exchange array elements". Does that mean you generate 2 random numbers within the length of the array, and then switch the items at those locations in the array?

View Replies View Related

Recursion - Returning Array Of Odd Numbers

Feb 26, 2014

I have this method

Java Code:
public static int[] oddNums(int n){
} mh_sh_highlight_all('java');

When a number is passed in, this method returns an array of odd numbers the amount of the number passed in.

So

Ex: Class.oddNums(5) would return [ 1, 3, 5, 7, 9 ]
Class.oddNums(2) would return [ 1, 3]

View Replies View Related

Array Of Numbers Not Able To Get Correct Average Value

May 26, 2015

I have the following methods:

public static int getSum(int[] data) {
int sum = 0;
for (int i = 0; i < data.length; i++) {
sum += data[i];
}
return sum;

[code]....

The input is the following arary (Its from the Junit test that fails this): [Integer.MIN_VALUE, -1, 0, 1, Integer.MAX_VALUE].I get an average of 0.0 when it should be -0.2.

View Replies View Related

Store Numbers In Array Then Sort

Feb 19, 2014

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);

[Code] ....

View Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved