NullPointException - Creating Pairs From Arrays

Oct 9, 2014

So in my java class we are suppose to ask for how many names are going to be entered, create an array of that length, and then store that many inputted names. We then take those names, alphabetize them, and create a set of pairs. For example, it might look like this:

How many people are there? 4
Please enter the names of those people:
Dana
Bob
Alice
Charlie

There are 6 possible pairings. They are:
Alice & Bob
Alice & Charlie
Alice & Dana
Bob & Charlie
Bob & Dana
Charlie & Dana

We are also suppose to account for less than two names (both 1 and 0 as well as negatives).

I keep getting a null point exception error and I can't figure out why. Even debugging in Eclipse doesn't give any pointers except that it is originating in line 76.

import java.util.Scanner;
import java.util.Arrays;
class partners
{
public static int numNames;
public static int numPairs;

[Code] .....

View Replies


ADVERTISEMENT

Pantry Class NullPointException Error

Nov 13, 2014

The program runs a NullPointException Error when I run the MixedFruit method.How do I fix that?

import java.util.Scanner ;
public class Exercise1
{
public static void main ( String[] args )
{
Scanner scan = new Scanner (System.in) ;
String inChars;

Jam goose = new Jam( "Gooseberry", "7/4/86", 12 );
Jam apple = new Jam( "Crab Apple", "9/30/99", 8 );
Jam rhub = new Jam( "Rhubarb", "10/31/99", 16 );

[code]....

View Replies View Related

Creating A Scanner And Putting Input Into Arrays

Apr 15, 2014

I'm trying to read user input from the terminal and separate the input into separate arrays depending on if the user input is an integer, scanner, or a string. The terminal should keep asking the user for input until the user types "quit".

import java.util.*;
public class arrayScanner {
public static void main(String[] args) {
ArrayList<Integer> intList = new ArrayList<Integer>();
ArrayList<Double> doubleList = new ArrayList<Double>();
ArrayList<String> otherList = new ArrayList<String>();

[code].....

View Replies View Related

Creating Methods Using Arrays - Fraction Class

Sep 27, 2014

I am trying to get the average of 3 different fraction arrays. I made a fraction class and I made methods such as read() and average() in this new class.

package fractions;
import java.util.Scanner;
import java.util.Arrays;
public class FractionArrays {
public static void main(String[] args) {
Fraction completeFraction = new Fraction(5,6);

[Code] ....

I was wondering if there was any way to use the arrays I created in the read method in the average method. If I find that out I should be able to do it on my own. Is there a way to make the arrays public to use in different methods?

View Replies View Related

Creating Merge Sort For String Arrays

Mar 31, 2014

I'm having a problem printing out the descending order of my array. The array order goes like (Title,Studio,Year). I try to create to ints with the compareTo method but when the program is run the I get array out of bounds. Could the answer possibly be that in order not not have the out of bounds error, to create a for loop inside of the while?

public class Movie2
{
// instance variables
private int year;
private String Title;
private String Studio;

[code]...

View Replies View Related

Creating Palindrome Program - Arrays And For Statements?

Feb 14, 2015

I am trying to create a program that reads a sentence, such as: "abba is running to the radar" scans this sentence, and then prints out all the palindromes. I am running into issues with my arrays and for statements. Here is my code:

static String palindrome, backwardsLower,
palindromeLower, palindromeClean, backwards2, backwards = "";
static String[] words;
static int counter;
public static void main(String[] args) {
palindrome = JOptionPane.showInputDialog("Please enter a phrase. " +

[Code] ...

I am aware that there is a few "useless" variables in there at the moment, I will clean them up (as well as some useless statements, I see those too). The issue comes at about line 17. The variable backwards REMOVES all the spaces from the array, so when it comes time to compare the strings, it is comparing individual words to the ENTIRE string, thus no words will ever be a palindrome.

View Replies View Related

Reading Pairs Of Lines From A File?

Jul 19, 2014

This what the data file looks like -

5165168416516988798484984984984984
9898798798798

1556516846516518498
51688484979789798798798491

The problem was to make code which will combine consecutive lines which contain some data into one line and print it. I made a solution that works.

import java.io.File;
import java.io.FileNotFoundException;
import java.util.NoSuchElementException;
import java.util.Scanner;
public class Filez {
public static void main(String[] args) {
File file = null;

[code]...

View Replies View Related

Parsing Text File That Contains Pairs

May 9, 2015

I thought of processing line by line using scanner and then split each line in the ":" . But then i stuck, i only need the right part of the ":" so that i can save it to a class e.g. John Doe, 133456593, married etc.

Java Code: Name: John Doe
TRN: 133456593
Status: Married
Income: 22500
Receipts:

Receipt ID: 1
Date: 25/2/2014
Kind: Basic
Amount: 2000
Company: Hand Made Clothes
Country: UK
City:London
Street: Cambridge
Number: 10

Receipt ID: 2
Date: 28/2/2014
Kind: Entertainment
Amount: 500
Company: Floca Cafe
Country: Italy
City: Rome
Street: Piazza
Number: 4 mh_sh_highlight_all('java');

View Replies View Related

Which Iterator To Be Use To Create Instances From Feature Value Pairs (Mallet API)

Mar 12, 2015

I am tring to run LDA to generate some topics from txt files as the following one:

Document1 label1 forest=3.4 tree=5 wood=2.85 hammer=1 colour=1 leaf=1.5
Document2 label2 forest=10 tree=5 wood=2.75 hammer=1 colour=4 leaf=1
Document3 label3 forest=19 tree=0.90 wood=2 hammer=2 colour=9 leaf=4.3
Document4 label4 forest=4 tree=5 wood=10 hammer=1 colour=6 leaf=3

Each numeric value in the file is an indication of the number of occurrences of each feature (e.g., forest, tree) multiplied by a given penalty. To generate instances from such a file, I use the following Java code:

String lineRegex = "^(S*)[s,]*(S*)[s,]*(.*)$";
String dataRegex = "[p{L}([0-9]*.[0-9]+|[0-9]+)_=]+";
InstanceList generateInstances(String dataPath) throws UnsupportedEncodingException, FileNotFoundException {
ArrayList<Pipe> pipeList = new ArrayList<Pipe>();
pipeList.add(new Target2Label());

[Code] .....

I then add the so-generated instances to my model using the instruction model.addInstances(generatedInstances). The resulting output is described below.

It contains errors caused by the instruction model.addInstances(generatedInstances). Debugging my code showed me that the alphabet associated to the model is null. Am I using the wrong iterator?

name: document1
target: label1
input: TokenSequence [forest=3.4 feature(forest)=3.4 span[0..10], tree=5 feature(tree)=5.0 span[11..17], wood=2.85 feature(wood)=2.85 span[18..27], hammer=1 feature(hammer)=1.0 span[28..36], colour=1 feature(colour)=1.0 span[37..45], leaf=1.5 feature(leaf)=1.5 span[46..54]]
Token#0:forest=3.4 feature(forest)=3.4 span[0..10]

[Code] ....

View Replies View Related

JSP :: Generate Hashtable And Display Its Key Value Pairs Back To Browser - How To Prevent Timeout

Jun 24, 2004

I have a jsp page that generate a hashtable and display its key-value pairs back to the browser. The problem is that it takes on an average about 15 minutes to build this hashtable, and as a result, I always get a timeout error. What can I do to avoid getting the timeout error without changing the server configuration for timeout

View Replies View Related

Creating Final Arrays With Final Elements

Aug 2, 2013

I want to create a final array with final elements
 
{code}
final int[] array = {0, 1, 2, 3, 4, 5};
{code}
 
But here only the reference of the array is final , not it's elements ... So how can I do that??

View Replies View Related

What Is The Difference Between Regular Arrays And Multi Dimensional Arrays

Jun 28, 2014

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.

View Replies View Related

Constructor For Arrays

Apr 23, 2015

I'm wonder about the issue of constructor for arrays. Let say I have a class tablica, and one component int[] tab. If I get it right until now tab is nothing more than empty reference to some unexisting array?

import java.util.Random;
class tablica{
int[] tab;
tablica (){ // i wish it was constructor

[code]....

Then, I'm trying to build the constructor for class tablica. What can be the parameter of such constructor? Is it fields of array? It is simple forf for basic variable

- I liken values defined in constructor with those global defined in class. But how to do it with array component tab.

If I create array object in main method then how can I use this constructor?

View Replies View Related

Resizing 2D Arrays

Sep 19, 2014

How do i increase the size of a 2D array from 2X2 to 4X4 where only the boundary elements are zeroes and the rest of them remain the same?

View Replies View Related

If / Else Statement With Arrays?

Jan 28, 2015

In the program below I populated three arrays: student id, name, and GPA. The findStudent() method attempts to match users input with the input stored in the studentID array using an if/ else statement. When I use the if/ else statement the else always executes regardless if the IDs match? I am trying to get JOptionPane.showMessageDialog(null, "Incorrect entry"); to print only if the IDs don't match.

Java Code:

//FILE: StudentIDArray.java
import javax.swing.*; //Used for the JOption dialog boxes
import java.util.*; //Used for Scanner input
public class StudentIDArray {
boolean nameFound = true;
final int numOfElements = 2; //Final int to control to loop for data

[code]....

View Replies View Related

Arrays Outside Of Methods

Aug 22, 2014

I'm working on a side project, which will eventually hopefully be a Pokedex, and I've just been going to it at the end of every chapter and using the stuff I've learned to work on it.So I just read chapter 3, which is all about variables and teaches how to use arrays.

my question is, does an array have to be declared inside a method? Because I'm trying to create an array inside a class without any methods and without the main, and I continuously get errors. Here's a quick working of my code that won't compile.

class blah {
blah a[] = new blah[7];
a[0] = new blah();
}

The error message focuses on a[0] = new blah(); Telling me the 0 should be a ], the = is an illegal start of type, so on and so forth. The program compiles completely fine if it's within a method, like this:

class blah {
void a() {
blah a[] = new blah[7];
a[0] = new blah();

}

}

Or if I have public static void main (String[]args); But I'm trying to practice working outside of main.So does an array have to be within a method,

View Replies View Related

Returning Values From Arrays

Nov 22, 2014

I have created a class and a matrix of doubles (or at least, I think I have, that's partly what I want to verify).I need to return the values of the array,Here is my class:

public class example{
double[][] Position=new double[2][11];
double calculate(){
for (int time=0;time<=10;time=time+1){
Position[1][time]=time;
Position[2][time]=time+1;
double A=Position[2][time];
return A;
}
}
}

I am getting the error: "This method must return a result of type double", though to me it looks like I am returning double (A).

View Replies View Related

Filling Arrays With A For Loop?

Mar 15, 2015

I am having trouble conceptualizing how to fill arrays. I understand that I can declare one and number it's elements, and it stores values of a certain data types. I don't really understand beyond that. Like how I would fill and use data in it.

I can be more specific with an assignment I have for school, but need to get started understanding how to fill arrays with data and then how to use that data.

View Replies View Related

Random Generator And 2D Arrays?

Sep 21, 2014

So im making this ghost game where i display an 8x8 filled with 0s and a randomly generator five 1s in there I can get it to display 0s and add 1s, however sometimes the 1s that are randomly generated sometimes go on the same spot making it look like there are only four 1s. How would i go about fixing that?

package Grade12;
 import java.util.Random;
 public class Ghost {
 public static void main(String[] args) {
 Random generator = new Random();
 int gameboard [][] = new int [8][8];
int randomx, randomy, counter = 0, sum = 0; 
for(int row = 0; row < 8; row++){
for(int col = 0; col < 8; col++){ 
(gameboard[row][col]) = 0;
 }

[code]...

View Replies View Related

Get Values From Arrays Rather Than Hashcode?

Mar 4, 2014

The assignment goes like this...Write a Payroll class that uses the following arrays as fields:

employeeID - An array of seven integers to hold employee identification numbers. The array should be initialized with the following numbers:

5658845452012578951228777541
845127713028507580489

hours - An array of seven integers to hold the number of hours worked by each employee.payRate - An array of seven doubles to hold each employee's hourly pay rate.wages - An array of seven doubles to hold each employee's gross wages.The class should relate the data in each array through the subscripts.

For example, the number in element 0 of the hours array should be the number of hours worked by the employee whose identification number is stored in element 0 of the employeeID array. That same employee's pay rate should be stored in element 0 of the payRate array. In addition to the appropriate accessor and mutator methods, the class should have a method that accepts an employee's identification number as an argument and returns the gross pay for that employee.Demonstrate the class in a complete program that displays each employee number and asks the user to enter that employee's hours and pay rate. It should then display each employee's identification number and gross wages.Input Validation: Do not accept negative values for hours or numbers less than 6.0 for a pay rate.

My problem with this program is that everytime I try to print the employee ID's or the wages, I get hashcode or something like it (#[I1a77cdc or something like that). I tried using the toString method, but it lists all of the values, when I'm trying to display one at a time. Here is the code for the class:

// moduleArray class
public class moduleArray {
final int NUM_EMPLOYEES = 7;
int[] employeeID = {5658845, 4520125, 7895122, 8777541, 8451277, 1302850, 7580489};
int[] hours = new int[NUM_EMPLOYEES];

[code]...

This is the demo program to list the ID's. I've been messing with it for some time, and right now I just want it to display values.

import java.util.Scanner; 
public class moduleArrayDemo {
public static void main(String[] args) {
final int NUM_EMPLOYEES = 7;
int[] ID = new int[NUM_EMPLOYEES];

[code]...

View Replies View Related

2D Arrays - Word Search?

Dec 14, 2014

Okay, so the assignment was creating a word search for the given array. I've created all the code necessary, but I've ran into trouble. This problem occurs with the Up-Forward, Up-Backward, Down-Forward, and Down-Backward sections. If I only set one of these to run, it works. But if I have all 4 set at the same time, it errors out on the first one that runs.

public class P_WordSearch {
public static void main(String[] args) {
char[][] puzzle = {
{'B','O','O','G','S','R','O','W','I','N','G'},
{'E','B','L','G','N','I','M','M','I','W','S'},
{'L','C','E','A','T','I','P','U','P','I','S'},
{'C','M','I','N','C','A','X','Y','O','S','N'},

[Code] ....

View Replies View Related

Unable To Concatenate Two Arrays

Dec 15, 2014

I am trying to concatenate two arrays but on on compiling it is showing

Java Code: import static java.util.Arrays.*;
class Mergesort
{
public static void main(String...s)
{
Mergesort r=new Mergesort();
int x[]={1,2,3,4,5,6};
int y[]={9,8,7,15,14,13,12,11};

[Code]...

View Replies View Related

Error When Using Arrays And Methods

Jan 4, 2015

I am receiving one error when using arrays and methods. It's at the bottom of the code and I have commented what the error is .

import java.io.*;
import javax.swing.*;
public class CEO_PROGRAM_PartB
{
public static void main (String[] args) throws IOException

[code]....

View Replies View Related

Working With Parallel Arrays

Dec 5, 2014

The assignment was to create 3 parallel arrays to make a student database.The first array will contain 4 digit student id's, the second a string array with student names, and the third array is student gpa's. The user is to receive a dialog box asking to enter the student id, and if the id is correct the user is to see the student name and grade. If the user input does not match any value in the student id array, the user is to receive a message stating invalid id. Here is the code I have so far.

For some reason no matter what the user enters, the information for the last array entry is displayed.

public class parallelStudent
{
public static int sequentialSearch(int[] array, int value)
{
int index; //loop control variable
int element; //element the value is found at
boolean found; //flag indicating search results

[code]...

View Replies View Related

Printing Out Intersection Of Two Arrays

Nov 24, 2014

We had to use these methods with the given parameters and code them correctly. We cannot use any built in java methods for sets.

import java.util.Scanner;
public class setPractice {
public static Scanner kbd;
public static final int MAXSIZE = 20;
public static void main(String[] args) {
kbd = new Scanner(System.in);
int[] setA = new int[MAXSIZE];
int[] setB = new int[MAXSIZE];
int[] intersect = new int[MAXSIZE];

[Code] ....

This is the input given to the program:

How many numbers will be in the 1st set: 3 Enter list of integers for 1st set: 12 3 2

The ascending order for 1st is: 2 3 12

How many numbers will be in the 2nd set: 4

Enter list of integers for 2nd set: 2 3 6 1

The ascending order for the 2nd set is: 1 2 3 6

The intersection of the two sets is: 0 0 0 //Program not correctly print intersection of two sets

The difference of A-B is: //Program is not printing the difference either...

View Replies View Related

Stuck On Two Dimensional Arrays

Apr 13, 2014

I am having trouble grasping 2 dimensional arrays .I have a certain problem:

String[][] words = {
{"Red", "Blue", "Green"},
{"Orange", "Purple". "Yellow"}

[code]....

I ran the code and got OrangePurpleYellowRedBlueGreen.

View Replies View Related







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