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.
View Replies
ADVERTISEMENT
Sep 10, 2014
I recently started making some Java games, and now I'm having some troubles,
It seems like I can't place a functioning JTextField on my Graphics2D object, I can place images, even the image of the component itself:
Java Code: Graphics2D g2d = (Graphics2D) this.getGraphics();
JTextField jtf = new JTextField();
jtf.setSize(150, 70);
jtf.setLocation(220, 220);
jtf.print(g2d); mh_sh_highlight_all('java');
How can I make it work? I really need it for character name creation, chat, etc... so making a JDialog isn't an option for me.
View Replies
View Related
Apr 29, 2014
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
}
View Replies
View Related
Nov 19, 2014
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
[code]...
View Replies
View Related
Oct 12, 2014
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..
View Replies
View Related
Apr 15, 2014
I have one requirement where when i click on the attribute i would like to create an action and display the same in OutputText.
Ex:
This is Attribute Description outputText :
<rich:columnGroup id="CMAttributeBrowserDescriptionRow2">
<rich:column id="CMAttributeBrowserDescriptionColumn2" styleClass="selectedObj"
rendered="#{CMBrowserBean.attributePresent}" >
<!--<h:outputText id="SelectedObjectDescription1" render="true" value="#{currentAttribute.value}"/>-->
[b][quote]<h:outputText styleClass="attributeTableFont1" readonly="false" id="attributeTextdescription" disabled="false"/>[/quote][/b]
</rich:column>
</rich:columnGroup>
The action should be for :
<rich:column id="AttributeColumn2" styleClass="parentBrowserColumn">
[b]<a onclick="document.getElementById('CMBrowser:0:CMAttributeBrowserDescriptionForm:attributeTextdescription').value=#{currentAttribute.name}" >[/b]<h:outputText id="AttributeName" title="#{currentAttribute.name}" value="#{currentAttribute.name}"
styleClass="attributeValueFont" /></a>
</rich:column>
I am unable to get the text coming on outputText, when i click on the above attribute.
View Replies
View Related
Mar 26, 2015
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.
import java.util.Scanner;
import java.util.StringTokenizer;
import java.io.*;
[Code].....
View Replies
View Related
Jan 29, 2015
I want to catch the exception in my program is the below way:
class Temp
{
public static void main(String s[])
{
try
{
int x=10/s.length;
System.out.println(x);
[Code] ....
I am expecting my program should give output as "java.lang.ArithmeticException: / by zero" but is giving
Temp.java:11: error: ')' expected
catch (ArithmeticException e | ArrayIndexOutOfBoundsException e)
^
Temp.java:11: error: ';' expected
catch (ArithmeticException e | ArrayIndexOutOfBoundsException e)
^
Temp.java:16: error: 'catch' without 'try'
catch (Exception e)
^
Temp.java:22: error: reached end of file while parsing
}
^
4 error
View Replies
View Related
Mar 18, 2014
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.
View Replies
View Related
Dec 14, 2014
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!
View Replies
View Related
Jan 23, 2015
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());
[code]....
"how many time each possible total occurs"
View Replies
View Related
Aug 6, 2014
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.
View Replies
View Related
Feb 11, 2015
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;
[code]....
View Replies
View Related
Mar 23, 2015
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!!!
View Replies
View Related
Mar 21, 2015
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 {
[code]....
View Replies
View Related
Apr 10, 2014
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".
View Replies
View Related
Mar 30, 2015
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.
View Replies
View Related
Nov 16, 2014
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++) {
[Code]...
View Replies
View Related
Apr 29, 2014
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.
View Replies
View Related
Apr 8, 2014
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.
View Replies
View Related
Nov 17, 2014
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:");
[Code] .....
View Replies
View Related
Apr 25, 2014
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
[Code] ....
View Replies
View Related
Jul 31, 2014
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.
*/
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
[Code] .....
View Replies
View Related
Oct 19, 2014
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.");
}
View Replies
View Related
Dec 17, 2014
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++)
[Code] ....
View Replies
View Related
Apr 17, 2014
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;
[Code] ....
Here's the code:
package kettask2b;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
[Code] ....
View Replies
View Related