This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 8 years ago.
Below is the error:
Exception in thread "main" java.lang.NullPointerException
at CellularData.addCountry(CellularData.java:34)
at CellularData.addCountry(CellularData.java:24)
at TestCSVReader.main(TestCSVReader.java:35)
I keep getting the error above but seem to have trouble fixing it. I am reading from a csv file and displaying the data on the screen. Basically it reads country, the year, and it stats of a cellular data in double. Below is CellularData class:
public class CellularData {
private Object [][]array;
private int year;
public CellularData(int rows, int columns, int year)
{
array = new Object[rows+1][columns+1]; //add +1 because initializes the header.
this.year = year;
for(int i=1;i<=columns;i++)
{
array[0][i] = year++; //increments the year
}
}
public void addCountry(String country, double []num)
{
for(int i=0;i<array.length;i++)
{
if(array[i][0] == null) //checks if the first row is empty
{
//CellularData.addCountry(CellularData.java:24)
addCountry(country, num, i); //inserts the data
break;
}
}
}
private void addCountry(String country, double []num, int row)
{
array[row][0] = country;
for(int j = 1;j<array[row].length;j++)
{
array[row][j] = num[j-1]; //CellularData.addCountry(CellularData.java:34)
}
}
public double getNumSubscriptionsInCountryForPeriod(String country, int sYear, int eYear)
{
double sum = 0;
for (int i = 1; i < array.length; i++) {
if (country.equalsIgnoreCase((String) array[i][0])) { //matches with the parameters passed ignoring CAPS
int start = 1 + sYear - year; //first index position of the year
int end = start + (eYear - sYear); //end position of the year
if (start >= 0 && end < array[i].length) { //checks to see if index position is out of bounds
for (int k = start; k <= end; k++) {
// System.out.println(">> " + country + " adding " + array[i][k]);
sum += (Double) array[i][k]; //sum of the stats
}
}
else {
//returns Error messgae and -1
System.out.println("ERROR : requested year "+sYear+" from "+ country+" is less than starting year "+this.year);
sum = -1;
}
}
}
return sum;
}
public String toString()
{ //prints the array.
for(Object []a: array)
{
for(Object k:a)
{
System.out.print(k + "\t");
}
System.out.println();
}
return " ";
}
}
Here is my csv reader file and this where i read from the file:
public class CSVReader {
String[] countryNames;
int[] yearLabels;
double[][] cellularDatatables;
Scanner scan;
public CSVReader(String filename)// throws FileNotFoundException
{
try{
File file = new File(filename);
scan = new Scanner(file);
scan.nextLine();
String numLine = scan.nextLine();
final int n = Integer.parseInt(numLine.split(",")[1]); //Number is the string portion after the first comma
//Allocate arrays with length n
countryNames = new String[n];
cellularDatatables = new double[n][];
//Read in the header line of years, parse and copy into yearNum
String[] yearHeaders = scan.nextLine().split(",");
final int m = yearHeaders.length-1;
yearLabels = new int[m];
for(int i=0;i<m;i++)
{
yearLabels[i] = Integer.parseInt(yearHeaders[i+1]); //i+1 to skip the first entry in the string arr
}
//Now read until we run out of lines - put the first in country names and the rest in the table
int c = 0;
while(scan.hasNext())
{
String[] inputArr = scan.nextLine().split(",");
countryNames[c] = inputArr[0];
cellularDatatables[c] = new double[m];
for(int i = 0; i < m; i++)
{
cellularDatatables[c][i] = Double.parseDouble(inputArr[i+1]);
}
}
scan.close();
}
catch(FileNotFoundException e)
{
System.out.println(e.getMessage());
}
}
public String[] getCountryNames(){
return countryNames;
}
public int[] getYearLabels(){
return yearLabels;
}
public double[][] getParsedTable(){
return cellularDatatables;
}
public int getNumberOfYears()
{
return yearLabels.length;
}
}
And also TestCSVReader file:
public static void main(String[] args)
{
final String FILENAME = "data/cellular.csv";
CSVReader parser = new CSVReader(FILENAME);
String [] countryNames = parser.getCountryNames();
//System.out.println(countryNames.length);
int [] yearLabels = parser.getYearLabels();
//System.out.print(yearLabels.length);
double [][] parsedTable = parser.getParsedTable();
CellularData datatable;
int numRows = parsedTable.length;
int numColumns =parser.getNumberOfYears();
int startingYear = yearLabels[0];
datatable = new CellularData(numRows, numColumns, startingYear);
for (int countryIndex = 0; countryIndex < countryNames.length; countryIndex++)
{
double [] countryData = parsedTable[countryIndex];
datatable.addCountry(countryNames[countryIndex], countryData);//Error TestCSVReader.java:35
}
System.out.printf(countryNames[0] + " (1960 to 2012): %.2f \n", datatable.getNumSubscriptionsInCountryForPeriod(countryNames[0],1960,2012));
// the output is: Aruba (1960 to 2012): 1170.50
array[row][0] = country;
for(int j = 1;j<array[row].length;j++)
{
array[row][j] = num[j-1]; //CellularData.addCountry(CellularData.java:34)
}
Since the first line doesn't give NullPointerException, we know that array is not null and array[row] is not null. Therefore, num must be null.
Related
In this program, I want the user to type in 20 product names. from the main method. which will pass down to the method named searchProducts. But for some reason, it doesn't work. It only let me type in once, and then it prints out all 16 products.
public static void main(String[] args) {
String[] products= {"Pencil pouch", "pen", "Pencil sharpener", "High lighters", "Markers", "Erasers",
"Binder", "Notebooks", "Index cards", "Folders", "Glue", "Ruler", "Scissors", "Calculator",
"Calendar", "Backpack"};
System.out.println("Unordered list");
displayProducts(products);
sortProducts(products, 16);
System.out.println("");
System.out.println("Ordered list");
displayProducts(products);
}
private static int searchProducts(String[] products) {
Scanner sc = new Scanner(System.in);
String x = sc.nextLine();
System.out.println("Enter name of product: ");
for (int i = 0; i < products.length; i++) {
if (products[i].equals(x))
return i;
}
return -1;
}
private static void sortProducts(String products[],int n) {
for(int i = 0; i < n - 1; i++) {
int minindex = i;
String minStr = products[i];
for(int j = i + 1; j < n; j++) {
if(products[j].compareTo(minStr) < 0)
{
minStr = products[j];
minindex = j;
}
}
if(minindex != i)
{
String temp = products[minindex];
products[minindex] = products[i];
products[i] = temp;
}
}
}
private static void displayProducts(String[] products) {
for(int i = 0; i < products.length; i++){
System.out.println(products[i] + " ");
}
}
The way you pass array parameter is just good.
There are several ways to pass arrays as parameter:
(/*Other params,*/ String[] param) // this way
(String[] param /*, Other params*/) // or
(/*Other params,*/ String param[]) // this way
(String param[] /*, Other params*/) // or
// special case
// only as unique or last param of the params
// because with it you can enter several String params as individuals
(/*Other params, */ String... param)
// arrays of arrays
(String[] param[])
(String[][] param)
(String param[][] )
This is your problem:
sortProducts(products, 20);
...
private static void sortProducts(String products[],int n) {
You pass 20 although your array is 16 sized. So error.
Change this way to not depend on the size.
sortProducts(products);
....
private static void sortProducts(String products[]) { // no size passed
int n = products.length; // read the size from the array
EDIT 1 -------------
In the code below, the user to type in N products. Then the array is printed, sorted and printed. (NOT TESTED)
public static void main(String[] args) {
int N = 20;
String[] products = new String[N];
Scanner sc = new Scanner(System.in);
for(int i=0; i < N; i++) {
System.out.println("Enter name of product "+ (i+1) +" : ");
String x = sc.nextLine();
products[i] = x;
}
System.out.println("Unordered list");
displayProducts(products);
sortProducts(products);
System.out.println("");
System.out.println("Ordered list");
displayProducts(products);
// search block
}
To search in the array, you can do something like that (NOT TESTED):
public static void main(String[] args) {
// ... previous code
// search block in main method
System.out.println("Enter name of product to search, or \"stop\" to stop : ");
String y = sc.nextLine();
while(y != "stop") {
int index = searchProducts(products, y);
if( index == -1 )
System.out.println("Product "+ y +" is not in array");
else
System.out.println("Product "+ y +" is at position "+ index);
System.out.println("Enter name of product to search, or \"stop\" to stop : ");
y = sc.nextLine();
}
}
private static int searchProducts(String[] products, String p) {
for (int i = 0; i < products.length; i++) {
if (products[i].equals(p))
return i;
}
return -1;
}
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 3 years ago.
I made java Selection Sort using Comparable class, File scanner.
In this code, we get txt file's name and store all words in String[] list and show index and stored word.
Finally, we sort this String[] list using Selection Sorting and check how much time was spent. but there's some error code.
This is an AbstractSort class
abstract class AbstractSort
{
public static void sort(Comparable[] a) { };
protected static boolean less(Comparable v, Comparable w )
{
return v.compareTo(w) < 0; // This Line is Error
}
protected static void exch(Comparable[] a, int i, int j)
{
Comparable t = a[i];
a[i] = a[j];
a[j] = t;
}
protected static void show(Comparable[] a)
{
for(int i = 0; i < a.length; i++)
System.out.println(a[i] + " ");
System.out.println();
}
protected static boolean isSorted(Comparable[] a)
{
for(int i = 1; i < a.length; i++)
{
if(less(a[i], a[i - 1])) // This Line is also Error
return false;
}
return true;
}
}
and this is a Selection Sort class which is extends AbstractSort class
class Selection extends AbstractSort {
public static void sort(Comparable[] a) {
int n = a.length;
for(int i = 0; i < n - 1; i++) {
int min = i;
for(int j = i + 1; j < n; j++) {
if(less(a[j], a[min]))
min = j;
}
exch(a, i, min);
}
assert isSorted(a);
};
}
and this is main function
public class HW1{
static String[] resize(int idx, String[] arr) {
String[] temp = new String[idx * 2];
for(int i = 0; i < idx; i++)
temp[i] = arr[i];
return temp;
}
public static void main(String args[]) {
int INIT_LEN = 10000;
long start, end, time;
String[] list = new String[INIT_LEN];
Scanner sc = new Scanner(System.in);
int idx = 0;
try {
System.out.println("File Name?");
String src = sc.nextLine();
sc = new Scanner(new FileInputStream(src));
while(sc.hasNext()) {
String word = sc.next().toString();
if(idx == list.length)
list = resize(idx, list);
list[idx++] = word;
}
System.out.println("1. Total Word = " + idx);
for(int i = 0; i < idx; i++)
System.out.println(i + "idx:" + list[i]);
start = System.currentTimeMillis();
Selection.sort(list); // This Line is also Error
end = System.currentTimeMillis();
time = end - start;
System.out.println("2. Selection Sorted? = true, Time = " + time + "ms");
}catch (FileNotFoundException fnfe) {
System.out.println("No File");
}catch (IOException ioe) {
System.out.println("Can't Read File");
}
}
}
when I run this code, I can see all words are stored int String[] list but there's also error code together.
Exception in thread "main" java.lang.NullPointerException
at AbstractSort.less(HW1.java:8)
at Selection.sort(HW1.java:40)
at HW1.main(HW1.java:84)
I don't know why this error code is occured...
When you call Selection.sort(list) in main, it appears that the list has a length of 10000.
Every element defaults to null.
So if you read in three words your list will look like this:
word1,word2,word3,null,null,null......null
Quick hack so you don't need to resize the array - try making your inner loop in Selection::sort:
for (int j = i + 1; j < n; j++) {
if (a[j] == null) {
break;
}
if (less(a[j], a[min]))
min = j;
}
Or - resize the array appropriately before processing.
Or - use an ArrayList to push words to and then convert to an array if you absolutely must use an array.
I am making a multiple string input random swap without using a temp variable.
But when I input, this happens a few times:
This happens more frequently... (note that the first output is always null and some outputs occasionally repeat)
My code:
import java.util.Arrays;
import java.util.Scanner;
public class myFile {
public static boolean contains(int[] array, int key) {
Arrays.sort(array);
return Arrays.binarySearch(array, key) >= 0;
}
public static void println(Object line) {
System.out.println(line);
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String finalText = "";
String[] input = new String[5];
String[] swappedInput = new String[input.length];
int[] usedIndex = new int[input.length];
int swapCounter = input.length, useCounter;
for (int inputCounter = 0; inputCounter < input.length; inputCounter++) { //input
println("Enter input 1 " + (inputCounter + 1) + ": ");
input[inputCounter] = in.nextLine();
}
while (--swapCounter > 0) {
do{
useCounter = (int) Math.floor(Math.random() * input.length);
}
while (contains(usedIndex, useCounter));
swappedInput[swapCounter] = input[swapCounter].concat("#" + input[useCounter]);
swappedInput[useCounter] = swappedInput[swapCounter].split("#")[0];
swappedInput[swapCounter] = swappedInput[swapCounter].split("#")[1];
usedIndex[useCounter] = useCounter;
}
for (int outputCounter = 0; outputCounter < input.length; outputCounter++) {
finalText = finalText + swappedInput[outputCounter] + " ";
}
println("The swapped inputs are: " + finalText + ".");
}
}
Because of randomality some times useCounter is the same as swapCounter and now look at those lines (assume useCounter and swapCounter are the same)
swappedInput[swapCounter] = input[swapCounter].concat("#" + input[useCounter]);
swappedInput[useCounter] = swappedInput[swapCounter].split("#")[0];
swappedInput[swapCounter] = swappedInput[swapCounter].split("#")[1];
In the second line you are changing the value of xxx#www to be www so in the third line when doing split you dont get an array with two values you get an empty result thats why exception is thrown in addition you should not use swappedInput because it beats the pourpuse (if i understand correctly yoush shoud not use temp values while you are using addition array which is worse) the correct sollution is to only use input array here is the solution
public class myFile {
public static boolean contains(int[] array, int key) {
Arrays.sort(array);
return Arrays.binarySearch(array, key) >= 0;
}
public static void println(Object line) {
System.out.println(line);
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String finalText = "";
String[] input = new String[5];
int[] usedIndex = new int[input.length];
int swapCounter = input.length, useCounter;
for (int inputCounter = 0; inputCounter < input.length; inputCounter++) { //input
println("Enter input 1 " + (inputCounter + 1) + ": ");
input[inputCounter] = in.nextLine();
}
while (--swapCounter >= 0) {
do {
useCounter = (int) Math.floor(Math.random() * input.length);
}
while (contains(usedIndex, useCounter));
// Skip if results are the same
if (useCounter == swapCounter) {
swapCounter++;
continue;
}
input[swapCounter] = input[swapCounter].concat("#" + input[useCounter]);
input[useCounter] = input[swapCounter].split("#")[0];
input[swapCounter] = input[swapCounter].split("#")[1];
usedIndex[useCounter] = useCounter;
}
for (int outputCounter = 0; outputCounter < input.length; outputCounter++) {
finalText = finalText + input[outputCounter] + " ";
}
println("The swapped inputs are: " + finalText + ".");
}
}
i was wondering how can i create a method where i can get the single instance from a string and give it a numericValue for example, if theres a String a = "Hello what the hell" there are 4 l characters and i want to give a substring from the String a which is Hello and give it numeric values. Right now in my program it gets all the character instances from string so the substring hello would get number values from the substring hell too because it also has the same characters.
my code :
public class Puzzle {
private static char[] letters = {'a','b','c','d','e','f','g','h','i', 'j','k','l','m','n','o','p','q','r','s',
't','u','v','w','x','y','z'};
private static String input;
private static String delimiters = "\\s+|\\+|//+|=";
public static void main(String[]args)
{
input = "help + me = please";
System.out.println(putValues(input));
}
//method to put numeric values for substring from input
#SuppressWarnings("static-access")
public static long putValues(String input)
{
Integer count;
long answer = 0;
String first="";
String second = "";
StringBuffer sb = new StringBuffer(input);
int wordCounter = Countwords();
String[] words = countLetters();
System.out.println(input);
if(input.isEmpty())
{
System.out.println("Sisestage mingi s6na");
}
if(wordCounter == -1 ||countLetters().length < 1){
return -1;
}
for(Character s : input.toCharArray())
{
for(Character c : letters)
{
if(s.equals(c))
{
count = c.getNumericValue(c) - 9;
System.out.print(s.toUpperCase(s) +"="+ count + ", ");
}
}
if(words[0].contains(s.toString()))
{
count = s.getNumericValue(s);
//System.out.println(count);
first += count.toString();
}
if(words[3].contains(s.toString())){
count = s.getNumericValue(s);
second += count.toString();
}
}
try {
answer = Long.parseLong(first)+ Long.parseLong(second);
} catch(NumberFormatException ex)
{
System.out.println(ex);
}
System.out.println("\n" + first + " + " + second + " = " + answer);
return answer;
}
public static int Countwords()
{
String[] countWords = input.split(" ");
int counter = countWords.length - 2;
if(counter == 0) {
System.out.println("Sisend puudu!");
return -1;
}
if(counter > 1 && counter < 3) {
System.out.println("3 sõna peab olema");
return -1;
}
if(counter > 3) {
System.out.println("3 sõna max!");
return -1;
}
return counter;
}
//method which splits input String and returns it as an Array so i can put numeric values after in the
//putValue method
public static String[] countLetters()
{
int counter = 0;
String[] words = input.split(delimiters);
for(int i = 0; i < words.length;i++) {
counter = words[i].length();
if(words[i].length() > 18) {
System.out.println("One word can only be less than 18 chars");
}
}
return words;
}
Program has to solve the word puzzles where you have to guess which digit corresponds to which letter to make a given equality valid. Each letter must correspond to a different decimal digit, and leading zeros are not allowed in the numbers.
For example, the puzzle SEND+MORE=MONEY has exactly one solution: S=9, E=5, N=6, D=7, M=1, O=0, R=8, Y=2, giving 9567+1085=10652.
import java.util.ArrayList;
public class main {
private static String ChangeString;
private static String[] ArrayA;
private static String a;
private static int wordnumber;
private static String temp;
public static void main(String[] args) {
// TODO Auto-generated method stub
a = "hello what the hell";
wordnumber = 0;
identifyint(a,wordnumber);
}
public static void identifyint (String a, int WhichWord){
ChangeString = a.split(" ")[WhichWord];
ArrayA = a.split(" ");
replaceword();
ArrayA[wordnumber] = ChangeString;
//System.out.print(ArrayA[wordnumber]);
a = "";
for(int i = 0; i<ArrayA.length;i++){
if(i==wordnumber){
a = a.concat(temp+ " ");
}
else{
a = a.concat(ArrayA[i]+" ");
}
}
System.out.print(a);
}
public static void replaceword(){
temp = "";
Character arr[] = new Character[ChangeString.length()];
for(int i = 0; i<ChangeString.length();i++){
arr[i] = ChangeString.charAt(i);
Integer k = arr[i].getNumericValue(arr[i])-9;
temp = temp.concat(""+k);
}
a = temp;
}
}
Change wordnumber to the word you want to replace each time. If this is not what you have asked for, please explain your question in more detail.
This question already has answers here:
Printing reverse of any String without using any predefined function?
(34 answers)
Closed 8 years ago.
I was asked this in a technical interview. I have no idea whatsoever please please help me.
it goes in infinite loop. I just cant find the correct logic.
not once, but twice i came across this kind of a question, so please help
public static int numberOfCharsInString(String sentence)
{
int numberOfChars = 0,i=0;
while (!sentence.equals(""))
{
sentence = sentence.substring(1);
++numberOfChars;
}
return numberOfChars;
}
public static void reverseSequenceOfWords(String inp)
{
int len=numberOfCharsInString(inp);
char[] in=inp.toCharArray();
int i=0;
for(i=len-1;i>=0;i--)
{
if(in[i]==' ')
{
while(!in.equals("")||in.equals(" "))
{
System.out.print(in[i]+" ");
}
}
else if(in[i]=='\0')
{
break;
}
}
}
public static void main(String[] args)
{
int length=0;
String inpstring = "";
InputStreamReader input = new InputStreamReader(System.in);
BufferedReader reader = new BufferedReader(input);
try
{
System.out.print("Enter a string to reverse:");
inpstring = reader.readLine();
length=numberOfCharsInString(inpstring);
System.out.println("Number of Characters: "+length);
reverseSequenceOfWords(inpstring);
}
catch (Exception e)
{
e.printStackTrace();
}
}
String[] array = "Are you crazy".split(" ");
for (int i = array.length - 1; i >= 0; --i) {
System.out.print(array[i] + " ");
}
Brute forced this so hard lol
public static void main (String args[]){
String input = new Scanner(System.in).nextLine();
input+=" ";
ArrayList<String> words = new ArrayList<String>();
int start = 0;
for(int i=0; i<input.length(); i++){
if(input.charAt(i)==' '){
String toAdd="";
for(int r=start; r<i; r++){
toAdd+=input.charAt(r);
}
words.add(toAdd);
start = i+1;
}
}
for(int i=words.size()-1; i>=0; i--){
System.out.print(words.get(i)+" ");
}
}
I've used String.length() and String.substring()and String.charAt() - I hope that is allowed.
private static class Word {
private final String message;
private final int start;
private final int end;
public Word(String message, int start, int end) {
this.message = message;
this.start = start;
this.end = end;
}
#Override
public String toString() {
return message.substring(start, end);
}
}
private Word[] split(String message) {
// Split it into words - there cannot be more words than characters in the message.
int[] spaces = new int[message.length()];
// How many words.
int nWords = 0;
// Pretend there's a space at the start.
spaces[0] = -1;
// Walk the message.
for (int i = 0; i < message.length(); i++) {
if (message.charAt(i) == ' ') {
spaces[++nWords] = i;
}
}
// Record the final position.
spaces[++nWords] = message.length();
// Build the word array.
Word[] words = new Word[nWords];
for (int i = 0; i < nWords; i++) {
words[i] = new Word(message, spaces[i] + 1, spaces[i + 1]);
}
return words;
}
private String reverse(String message) {
Word[] split = split(message);
String reversed = "";
for ( int i = split.length - 1; i >= 0; i--) {
reversed += split[i].toString();
if ( i > 0 ) {
reversed += " ";
}
}
return reversed;
}
public void test() {
String message = "Hello how are you today?";
System.out.println(reverse(message));
}
prints
today? you are how Hello
Much more minimal but less useful. Only uses length, charAt and substring again:
public void printWordsReversed(String message) {
int end = message.length();
for ( int i = end - 1; i >= 0; i--) {
if ( message.charAt(i) == ' ') {
System.out.print(message.substring(i+1, end)+" ");
end = i;
}
}
System.out.print(message.substring(0, end));
}
The only function i'm still using is the IndexOf function, but that is not that hard to create for yourself.
static void Main(string[] args)
{
string sentence = "are you cracy";
int length = Program.StringLength(sentence);
int currentpos = 0;
List<string> wordList = new List<string>();
int wordCount = 0;
while (currentpos < length)
{
// find the next space
int spacepos = sentence.IndexOf(' ', currentpos);
string word;
if (spacepos < 0)
{
// end of string reached.
word = sentence.Substring(currentpos, length - currentpos);
wordList.Add(word);
wordCount++;
// no need to continue.
break;
}
word = sentence.Substring(currentpos, spacepos - currentpos);
wordList.Add(word);
wordCount++;
currentpos = spacepos + 1;
}
// display
for (int i = wordList.Count - 1; i >= 0; i--)
{
// after first word is display, add spaces to the output
if (i < wordList.Count - 1)
{
Console.WriteLine(" ");
}
// display word
Console.WriteLine(wordList[i]);
}
}
public static int StringLength(String sentence)
{
int numberOfChars = 0;
while (!sentence.Equals(""))
{
sentence = sentence.Substring(1);
++numberOfChars;
}
return numberOfChars;
}