I'm making a program to demonstrate Exception Handling. (Note: the code was written inside towards out). The user is prompted to input two values (integers) and then the program will divide those values and output the result.
I then added the "try" and "catch keywords for exception handling-instead of the program shutting down if an error occurs, the words "you cant do that" will appear
I then added a while loop to allow the user another chance to input usable values. Of course, I made sure to make a termination to the loop as well.
Everything works as expected if the user inputs e.g 8 divided by 0. in that case exception occurse, the user is told "you cant do that", is then prompted to start over and so forth until he gives usable integers. then the program continues to the termination. However, if the user inputs a String , e.g 9 divided by xyz, then the program goes into an endless loop
My question is:
1) why does a String cause an endless loop ( I have a theory)
2)why does the loop occur as soon as I put in an incorrect value- is the while loop somehow in extant? somehow always waiting? (hard to picture it)
3) lastly, what would be the correct code to handle and exception caused by a String (as opposed to being caused by impossible to execute math)
Here is the program
package inschool;
/*
*GOAL: to allow for an exception to occur without causing the entire program to stop.
*NOTE: the coding was written from the center outwards (e.g first "System.out.println", then "try", then "do")
*/
import java.util.*;
public class L82_ExceptionHandling {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
I was learning looping in Java and decided to try this code and then I encountered an endless loop of 2s. why is that? I believe this line count=count++ caused it.
class ForDemo{ public static void main(String []args){ int count; for(count=2;count<=5;count=count++) System.out.println("count is:"+count); System.out.println("Done!"); } }
I've used Netbeans IDE to create a simple Swing JFrame container, class NewJFrame.
NewJFrame calls another JFrame with dialog in it. Problem is there is an endless call made to it. Here's the issue relevant piece of code:
//Code for calling class package p; public class NewJFrame extends javax.swing.JFrame implements ActionListener { ......... public NewJFrame() { initComponents(); //IDE auto generated code for binding
[Code] ....
I searched but i cannot understand why run class is getting called multiple times. I have attached the screen prints for both the screens. 2nd screen print(of called class) is very shallow since screen was blinking unstopped due to multiple calls, but i have attached a faint image of it.
I've been stuck on this one for a while. If you scroll down to the while loop there is some code that calculates the angle of a triangle created by two circles colliding. The problem is, The angle between the hypotenuse / x axis, and the angel between the hypotenuse / y axis never go above 65 degrees and i have no clue why?
I have an apache server with rest APIs accepting Json object and produce Json object.testing the sever using Chrome advanced rest client show no problems.I then tried to create a jave client, using org.jboss.resteasy:
public T sendPostRequest(String url, T input) throws Exception { ClientRequest request = getNewClientRequest(url); ObjectMapper om = new ObjectMapper(); final String body = om.writeValueAsString(input); request.body(MediaType.APPLICATION_JSON, body); final ClientResponse<String> responseObj = request.post(String.class); validateResponseString(url, responseObj); String responseString = responseObj.getEntity(String.class); responseObj.releaseConnection();
How to get used to program control statements. This is an attempt to write a program that returns the number of days between two dates. Sadly, it give incorrect values.
public class DaysBetweenDates { public static boolean isLeapyear(int year) {//This method will determine if a year is a leap year or not boolean isTrue=false; if(year%4==0 && year%100!=0) isTrue=true; else if(year%400==0) isTrue=true; return isTrue;
I am using the calendar component of the primefaces and I am using the attribute pattern="MM/dd/yyyy.But when I print the values in my MBean along with the selected date event the timeZone (i think) is printed as Sun Mar 29 00:00:00 IST 2015 I do not understand why its printed even though I have set the pattern.Part of my code :
Here is my code so far. I am trying to get the WHILE LOOP to work so the user inputs a number, the if statement prints the output and then it returns to ask for another number and goes again and again looping :
import java.util.Scanner; public class ifwhileloop { static Scanner sc = new Scanner(System.in); public static void main(String[] args) { double nmbr;
I am having a problem with code that I've written, the problem occurs when I try to prompt for user input at the end of the first loop. What happens is the println fires but the prompt never does and it exits the loop.
Java Code:
import java.util.Scanner; public class Lab3test { public static void main( String[] arguments ) { //Variable declaration
I have to take user input and then count how many times each number that the user input and print each one out. For some reason, I can't even get the for loop statement to print and it's pretty much the same as my other program except for the loop which is a little different.
//User inputs numbers between 1 and 100, program counts how many of each integer is and ends with a 0
import java.util.Scanner; public class occurrence { public static void main(String[] args) { //scanner/values Scanner input = new Scanner(System.in); int number = 0; int c = 0; //array count
What I am trying to do here is allow input to loop until 0 is entered for the product number. When 0 is entered, it should then dump the total for each individual product. I've tried it about a dozen different ways and have yet to be able to get the loop to function as intended. The way I have the code below, the loop will not function at all (where as before it looped, but never finished).
import java.util.Scanner; public class Sales { public static void main(String[] args) { double total1=0.0; double total2=0.0; double total3=0.0; double total4=0.0; double total5=0.0; int product;
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.
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.
Here's the code: it's while loop inside a for loop to determine the proper length of a variable:
for (int i = 0; i < num; i++) { horse[i]=new thoroughbred(); boolean propernamelength = false; while (propernamelength==false){ String name = entry.getUserInput("Enter the name of horse "
[code]....
I was just wondering what was going on here -- I've initialized the variable, so why do I get this message? (actually the carat was under the variable name inside the parentheses.
I have everything else working. My problem is that when i type "quit" to close the outer loop. It still runs the inner loop. The National Bank manager wants you to code a program that reads each clients charges to their Credit Card account and outputs the average charge per client and the overall average for the total number of clients in the Bank.
Hint: The OUTER LOOP in your program should allow the user the name of the client and end the program when the name entered is QUIT.In addition to the outer loop, you need AN INNER LOOP that will allow the user to input the clients charge to his/her account by entering one charge at a time and end inputting the charges whenever she/he enters -1 for a value. This INNER LOOP will performed the Add for all the charges entered for a client and count the number of charges entered.
After INNER LOOP ends, the program calculates an average for this student. The average is calculated by dividing the Total into the number of charges entered. The program prints the average charge for that client, adds the Total to a GrandTotal, assigns zero to the Total and counter variables before it loops back to get the grade for another client.Use DecimalFormat or NumberFormat to format the numeric output to dollar amounts.
The output of the program should something like this:
John Smith average $850.00 Maria Gonzalez average $90.67 Terry Lucas average $959.00 Mickey Mouse course average $6,050.89 National Bank client average $1,987.67
Code:
public static void main(String[] args) { Scanner scan = new Scanner(System.in); String name = ""; int charge = 0; int count = -1; int total = 1; int grandtotal = 0; int average = 0;
I am trying to write a program that will generate a QR Code from an input text and also display some information about the input/output bits. So far I have created the frame and what to do next. And I'm not sure if I am on the right track since my level of programming is not that great. By the way, I am using zxing libraries from GitHub. I know, there are plenty of generators online for the QR Code, but that is not what I am looking for. As you can see on the attached image, I am more interested in the efficiency of encoding 2D data. Also, I noticed that almost all the online projects regarding 2D codes are for Android. Which is not very useful.
So I was going to try to create a program that prompts input and creates a file (That didn't exist before) with that input as name.Then, the program prompts inputs after stating questions such as 1 + 1, then if the user inputs an answer, put "Question # = Correct "or" Wrong.Code SO Far:
Java Code:
import java.io.BufferedReader; import java.io.File; import java.io.InputStreamReader; public class File_Read { public File_Read() {//File_Read is the Interactive object
[code]....
So that it puts the Correct or Wrong into the file.
class Client{ public static void main(String []args){ Bike R1=new Bike(5.0, 60.0,30.0);//create bike object with params Bike R2=new Bike();//without params System.out.println(R1.increaseSpeed());//calling methods System.out.println(R1.maxDistance()); System.out.println(R2.increaseSpeed()); System.out.println(R2.maxDistance()); } }
How to convert this program from a while loop to a for loop.
import java.util.Scanner; public class LongDivision { public static void main(String arguments[]){ Scanner input = new Scanner(System.in); System.out.println("Enter the dividend: ");
I am trying to make a program add values from a loop. So what its supposed to do is search through tokens on an imported file. The file lists State, Capital, and then capital population. Then take the population string, turn it into numbers, and then do stuff with the numbers. First I'm supposed to find the Highest and lowest population of the places in the file (which I did without problem), but the finally thing is I'm supposed to add each found population to the last so I can find the average of the populations.
I just cannot seem to grasp how to do that. I THINK I'm supposed to some how store the given value into a variable, but how do I get that variable to add to the new value?
like...? Get token -> a b = a c = a + b
or wait no.....
Java Code :
import java.io.*; import java.util.Scanner; public class CapPopS { public static void main(String[] args) throws IOException { File stateCAP = new File("state-capital-2004population.txt"); if (!stateCAP.exists())
My teacher wants me to make a program that counts the amount of steps a student takes a day. The program asks for other info such as name, age, college. However I need to write a loop that will allow the user to enter how many ever steps they took and convert them to miles.how exactly to make the steps entered by the user within the loop be their own individual days like monday tuesday etc. Like the loop will ask how many steps did you take monday.. tuesday.. etc for each time it runs.
package StudentInfo; import java.util.Scanner; public class studentinfo { public static void main (String [] args){ Scanner scan = new Scanner(System.in);
I need to write a program that measures how long it will take someone to make a million dollars if he is being paid $5.75 an hour, but the pay rate is increase by 0.2% each week after the third week.
This is the first time I've ever gotten an infinite loop with a FOR loop. This program is supposed to let you enter five integer numbers and draw a bar chart based on those numbers. After the fifth number is entered, guess what? It wraps back around to zero again and starts over! Why the bleep doesn't it stop? The code is below: