What To Do When Exception Occurs That Affects Functioning Of Program
Jun 30, 2014
When an exception occurs that will affect the entire functioning of a program, in other words, the program will not be able to do what it is intended to do, is it best just print the exception to user and run System.exit(1)? And then force the user to run the program again.
So, I don't know how to start off this program. I'm not looking for the final answer, but I don't know two things: 1. the objective, 2. what type of thing do I start it off with? A method? I'm pretty sure it's not a class right because it already has a class?
public class WebFarm { private ArrayList<Server> servers; /* constructors and other methods and instance variables not shown */ } public class Server
[Code] ....
the Instructions:
The ping() method of the Server class returns true if the server is currently functioning normally; otherwise it returns false. Write a definition for the needsAttention() method of WebFarm that returns an ArrayList containing the servers that are not functioning normally:
public ArrayList<Server> needsAttention() { //I can put code here }
I am working on a program that should take an input file as a command line argument and check if that file contains a valid sudoku solution. Right now the main error I am getting is my scanner isn't functioning properly and is throwing inputMismatchexceptions, but Im also not sure about the validity of my functions, and i cannot test them because my scanner isn't working.
import java.util.Scanner;
//This class will read in a possible sudoku solution and test it for validity. If it is invalid, return at least one reason why.
public class SudokuVerifier { //this function checks a row to see if it contains the numbers 1-9 public static boolean checkRow(int sudoku[][], int i) { boolean contains=false; // keeps track if the line contains a certain number for(int k=1; k<=9; k++) { //goes through every number from 1 to 9 for(int j=1; j<sudoku[i].length; j++) { //checks every element in the row to see if it is equal to k
I don't understand that the error occurs when I don't initialize the variable myPoint. Whereas, I initialize the variable myPoint after variable declaration and before place of error.
package enumeration; import java.util.Random; public class Craps {
[Code]....
When I initialize the variable myPoint to zero in its decleration, the error disappear. But why? I have already initialized the variable myPoint in default case before the line of error occured..
I'm trying to read lines from a textfile and count all the words using String Tokenizer. However I keep getting an error "Unhandled exception type FileNotFoundException" on my IDE(Eclipse) referring to lines 13 and 8. When I let the IDE automatically, and I've even tried typing this manually, insert a try-catch block more errors show up concerning inputFile. When I insert the throws FileNotFoundException for each method it compiles but after running it gives me a FileNotFoundException for textfile.txt. What's even more interesting is that when I copy the .java file out of the IDE project folder, place it in a random empty folder on the Desktop with the textfile.txt, and try running it through command line, it gives a NoSuchElementException: No line found.
How to highlight an inputText in JSF 2.0 when a server-side validation error occurs? I have the server side validation,if validation fails, text box color (style) should change. Else it should display in normal color.
I'm trying to write a program that counts the number of times each number in an array occurs. In theory I understand how it work, you take the first element in the array, compare it again the rest of the array and any time it occurs in the array you increment a counter by 1. Then move onto the next element A[1] and so on...
This is what I've done so far :
public static void main(String[] args) { int[] array = {5,6,2,5,2,2,0,1,6}; int count = 0; for(int i = 0; i <array.length; i++) { int val = array[i]; for(int j = i+1; j<array.length; j++)
[Code] .....
I think I'm on the right track as it correctly prints out the frequency of the first 3 elements, but I'm definitely going wrong somewhere as it just doesn't do it right after that!
I am currently truing to make this class instantiate 100,000 dice rolls of 2 dice. And I also need to keep track of how many times each possible total occurs and I am having trouble outputting the result. Right now when I run my code it is just showing the results of each of the 100,000 roles.
public class ltefera_DiceRollTest { public static void main(String[] args) { ltefera_DiceRoll diceRoll = new ltefera_DiceRoll(10); System.out.println("Total # of pips" + " "); diceRoll.printArray(); System.out.println(diceRoll.countDice(2)); System.out.println(diceRoll.isArrayDataValid()); System.out.println(diceRoll.getTotal()); System.out.println(diceRoll.allDifferent());
How should null pointer exception be avoided in a program. Should each and every place of access of a variable be checked for null and if null is there own customized exception should be thrown. Is this approach correct for avoiding null pointer exception.
I am trying to read contents from a file and display them to the user. However, when I enter the file into the program I get the following error: "exception in thread 'main' java.util.MismatchException. What am i doing wrong?
import java.util.Scanner; import java.io.*; public class Project1{ public static void main(String[] args) throws FileNotFoundException { double balance; double item1Price;
I'm new to Java programming (taking a beginner's class) and our assignment is to program a game of Go Fish. I've got all my classes written (Card, CardPile, Player and GoFish). Now the problem is that I'm getting a NullPointerException when I run the GoFish class. The error seems to occur when I ask the program to check whether the player whose current turn it is, has a book (meaning 4 cards of the same value) in their hand. My idea was to run through the values 1 to 13, first checking to see if the value appeared in the player's hand:
if(players[currentTurn].hasCard(hasValue))
And if true, then remove all the cards matching the value from the player's hand, add to a new array of cards and then check to see if this array is of length 4 i.e. a book. If not, return the array of cards to the player's hand.
Here's the error message:
java.lang.NullPointerException at CardPile.searchValue(CardPile.java:88) at Player.hasCard(Player.java:15) at GoFish.main(GoFish.java:98)
It references the following lines of code:
In GoFish.main:
// At the end of each turn, check to see if player currentTurn has made a book. boolean hasBook = false; int hasValue; for (hasValue = 1; hasValue <= 13; hasValue++) { // Runs through player's hand and removes all cards of the same value between 1 and 13 and places in a new array.
[Code] ....
I cannot figure out why I am getting the null pointer exception error!!!
I am creating a simple ArrayList program that would enable one to input their username to it using a scanner. However, i am getting this error: "Exception in thread "main" java.lang.NullPointerException
at home.Members.addUser(Members.java:16) at home.Main.main(Main.java:14)"
Here is the code! :
Main.java class Java Code: import java.util.Scanner; public class Main {
I'm trying to have the program create a .txt file for me but it is not working properly. It just keeps printing the exception error "The file could not be found" and exits the program.
// This program asks the user for input for a name, phone number and notes up to 200 entries. // It stores every contact in a file. Type 'h' for help while running this program.
import java.util.Scanner; import java.io.*; public class Phonebook { static Entry[] entryList = new Entry[200]; static int numEntries; public static void main(String[] args) {
[Code] ....
After running the code, I go into my workspace folder and I see that the file was created. What to do because running the program will always print "could not find the file".
I'm having issues with JButtons and painting methods, whenever I click a grid on the board, the buttons in the JPanel disappear. Here is the board class:
package GUI; public class Board extends JPanel implements Observer { /** * */ // private static final long serialVersionUID = 1L; private final int boardSizeX; private final int boardSizeY; private final int L = 20;
[Code] ....
I know the code is messy in places, this will be fixed later, just need some pointers on how to solve the JButton issue.
I have problems getting the right number of times for each number of the array. Here is my code:
public static void main(String[] args) { int[] a = { 3, 13, 5, 9, 13, 6, 9, 13, 2, 3 }; int num = 3; int count = numbers(a, num); for (int i = 0; i < a.length; i++) {
Write a recursive Java method countDigitx that takes two integer numbers as input (a number n and digit x) and returns the number of times digits x occurs in number n:
For example if n=23242 and x=2 then the method should return 3 that is the number of times the number 2 found in n.
I first time tried Java web service sample example using eclipse with axis2. First i wrote the provider class. Then by clicking create Web Service from the provider class It will show the following error "IWAB0014E Unexpected exception occurred." I have attached the screenshot for reference.
So I have re-written the code but it is still not running correctly. Any number i type in it throws an exception, also i need the program to add the totals that i type in and then once i type -1 into the prompt button list all the number i typed in and give me the average.
import java.awt.*; import java.awt.event.*; import javax.swing .*; import javax.swing.text.*; public class Averages extends JFrame { //construct components JLabel sortPrompt = new JLabel("Sort By:");
I am working on a magic square program. My program compiles. However, when I enter the square dimension it does not select the correct file. The error says "java.io.FileNotFoundException." It looks like it inserts 0 instead of the entered dimension.
import java.util.*; import java.io.*; public class Trial2 { public static int size, row, col; public static void main(String[]args)throws java.io.IOException
The game runs fine, however I have to include how many times each letter that is guessed occurs within the word. For example, if the guess is e, the program should print out: The character e occurs in 3 positions. How would I go about doing this??
/* * A program that plays simple word guessing game. In this game the user provides a list of words to the program. * The program randomly selects one of the words to be guessed from this list. * The player then guesses letters in an attempt to figure out what the hidden word might be. * The number of guesses that the user takes are tracked and reported at the end of the game. */
In my cs class, we have to write a program that throws an exception if the user enters a negative number, the program should prompt the user to enter only positive numbers and then let them retype the number. But everytime I throw an exception, my program gets suspended.
What am I doing wrong, or is there no way to continue a program if an exception is thrown? Does the program automatically get suspended when an exception is thrown?
try{ do { N = kb.nextDouble(); if(N<0) { throw new NegativeArraySizeException(); } else j++; } while(j==0); fill.size(N); } catch(NegativeArraySizeException e) { System.out.println("The number you entered must be positive. Please try again."); }
My program is user input 20 char and the program will print the most common. So I use another int arr which count the number appears in the original array. i know its not so Effective but I don't know why it run but it stop in the middle. I got this code :
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 20 at Ex2.common(Ex2.java:39) at Ex2.main(Ex2.java:23)
import java.util.Scanner; class Ex2 { public static void main(String[] arg) { Scanner reader = new Scanner (System.in); char[] arr=new char[20]; System.out.println("Please enter 20 chars:"); for (int i=0;i<20;i++)
I've got a nasty nullpointer that I have tried to resolve to no avail as of yet. The program should prompt for a listings.txt file and take its info and write to a report file. Here's the stacktrace:
run:
Input file: listings Exception in thread "main" java.lang.NullPointerException at java.io.Writer.<init>(Writer.java:88) at java.io.PrintWriter.<init>(PrintWriter.java:113) at java.io.PrintWriter.<init>(PrintWriter.java:100) at kettask2b.PropertyListingsReport.main(PropertyListingsReport.java:34) Java Result: 1
Some adjustments that I have attempted are:
BufferedWriter pwfo = null; for (int i = 0; i < args.length; i++) { String string = args[i]; pwfo = null;