This program is supposed to accept an integer as an input and display the message that the number is even or odd. The main method calls a Boolean method. Write a method private static boolean iseven(int number and the message is printed from the Main method. This is what I have.
import java.util.Scanner;
public class OddorEven {
int number;
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
[Code] ....
I know the message is not being printed from the Main method. I'm not sure how to do that.
I am attempting to write a program that accepts input of a positive integer, reports each digit and then the sum of all digits. This is what I have so far:
*/ package numbersum; import java.util.*; public class Week4NumberSum { static Scanner console = new Scanner(System.in); /** * @param args the command line arguments
[Code]...
It works if I enter 1234567891 (10 digits) Enter a positive integer: 1234567891 digit: 1 digit: 2 digit: 3 digit: 4 digit: 5 digit: 6 digit: 7 digit: 8 digit: 9 digit: 1
The sum of the digits is 46
But if I enter 11 digits, it fails:
Enter a positive integer: 123456789123 (12) Exception in thread "main" java.util.InputMismatchException: For input string: "123456789123" at java.util.Scanner.nextInt(Scanner.java:2123) at java.util.Scanner.nextInt(Scanner.java:2076) at week4numbersum.Week4NumberSum.main(Week4NumberSum. java:26)
java - Scanner error with nextInt() - Stack Overflow gives me some infor for InputMismatchException - which I find described as In order to deal with this exception you must verify that the input data of your application meet its specification. When this error is thrown, the format of the input data is incorrect and thus, you must fix it, in order for your application to proceed its execution.
I don't understand why the 10 digit integer is OK but the 11 or > digit integer is a mismatch.
Write a function that accepts an array of non-negative integers and returns the second largest integer in the array.
Return -1 if there is no second largest.
The signature of the function is int f(int[ ] a)
Examples:
if the input array isreturn{1, 2, 3, 4}3{{4, 1, 2, 3}}3{1, 1, 2, 2}1{1, 1}-1{1}-1{}-1
In the signature what I understood is, I should write my function with the given signature,
The return type is "int"
method name is "f"
parameter is "a" right ?
Writing my doubts beside the particular line in the code
public static void main() // In the answer why they didn't use the class ?
In main method why they didn't use parameters ?(String[] args)
{ a1(new int[]{1, 2, 3, 4}); // what is "a1" here is it array name ? this line initializing the array ? a1(new int[]{4, 1, 2, 3}); a1(new int[]{1, 1, 2, 2}); a1(new int[]{1, 1}); a1(new int[]{1}); a1(new int[]{}); }
static int a1(int[] a) // what is "a" here parameter ? and "a1" is method name ? why they used the array name and method name same ?
{ int max1 = -1; int max2 = -1; for (int i=0; i<a.length; i++)
I am practicing some basic recursion and I was trying to solve this problem
Write a method sumTo that accepts an integer parameter n and returns the sum of the first n reciprocals. In other words:
sumTo(n) returns: 1 + 1/2 + 1/3 + 1/4 + ... + 1/n
For example, the call of sumTo(2) should return 1.5. The method should return 0.0 if passed the value 0 and should throw an IllegalArgumentException if passed a value less than 0.
This is my attempt to do it , however my output is always a 0.0 , and i do not understand why :
public static double sumTo(int n ){ if(n<0){ throw new IllegalArgumentException(); } else if (n==0){ return 0.0;
Write a java program to accept a string, float and an integer as command line arguments. String value should be displayed. Also the program should display the maximum value of two numbers. Use Integer.parseInt() and Float.parseFloat()
Write a method maxOccurrences that accepts a list of integers as a parameter and returns the number of times the most frequently occurring integer (the “mode”) occurs in the list. Solve this problem using a map as auxiliary storage.
A program to create a class that contains a method that accepts compass directions and displays the same. You need to ensure that the method must accept any one of the directions: NORTH, SOUTH, EAST, WEST, NORTHEAST, SOUTHEAST, SOUTHWEST or NORTHWEST.
The program is to accept a string and display all the palindrome words(the words that are same even if they are reversed-------->mom,madam,malayalam etc.)in the string.
I need to solve this program urgently.
There are no syntax errors but after typing in the sentence,no output is displayed. This is my program....
import java.util.*; class palindrome_test { public static void main(String args[]) { Scanner in=new Scanner(System.in); System.out.println("Enter a sentence"); String usersInput=in.nextLine();
I am trying to create a fortune teller and everything is running fine except the program does not prompt you to answer the questions
Do you like donuts? and What did you have for breakfast?
Here is the code this is in Bluej :
import java.util.Scanner; public class FortuneTeller { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); System.out.println("Welcome to Fortune Teller");
I want to write a program that accepts user input and then prints its average in a serial way.
Suppose i enter 5. I should get result 5 first time then i input 10 then it should return the average of (5 and 10) and then i enter say 20 it should return average of (5,10 and 20) and so on.
Write a menu driven program that either accepts words and their meanings, or displays the list of words in lexicographical order (i.e. as in a dictionary). When an entry is to be added to the dictionary you must first enter the word as one string, and then enter the meaning as separate string. Another requirement - from time to time words become obsolete. When this happens, such word must be removed from the dictionary.
Use the JOptionPane class to enter the information.
Use the concept of linked list to carryout this exercise. You will need at minimum the following classes:
- A WordMeaning class that hold the name of a word and its meaning. - A WordMeaningNode class that creates the node of information and its link field. - A WordList class that creates and maintain a linked list of words and their meanings. - A Dictionary class that test your classes.
For the output, the program should produce two scrollable lists:
- The current list of words and their meanings. - The list of the deleted words. You need not list the meanings, just the words.
So far, I have everything coded except for the remove method, and I am not sure how to code that. I coded the add method already, but now I don't know where to begin with the remove method in my WordList class. My classes are below.
WordMeaning Class:
public class WordMeaning { String name; String definition; WordMeaning(String t, String d) { name = t; definition = d;
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.
I have a program in which I am prompting users for integer values to display in a JFrame. I call the method below to load an array with their input:
Java Code:
public String inputAssembly(){ if (!jtfInput.getText().matches("d")){ JOptionPane.showMessageDialog(null, "Input must be of integer value."); } if (jrbFar.isSelected()){ return jtfInput.getText() + jrbFar.getText();
[Code] ....
Regardless of the input, both messages display (invalid input / got it). I've tried debugging so I know that the values are getting entered and read correct, at least to my knowledge. It is a very simple regular expression, only checking to be sure an integer was entered.
Write a program that reads an integer and displays, using asterisks, a filled diamond of the given side length. For example, if the side length is 4 the program should display.
* *** ***** ******* ***** *** *
I have it where it displays the top half of the diamond. But i cannot figure out how to get it to draw the bottom half.
import java.util.*; public class E616 { public static void main(String[] args) { Scanner input=new Scanner(System.in); System.out.print("Enter number of rows. "); int N=input.nextInt();
Write a program that will read two numbers and an integer code from the keyboard. The value of the integer code should 1, 2, 3, 4. If the value of the code is 1, compute the sum of the two numbers. If the code is 2, compute the difference (first number minus second). If the code is 3, compute the product of the two numbers. If the code is 4, and the second number is zero, compute the quotient (first divided by second). If the code is not equal to 1,2,3,4, display an error message. The program is then to display two numbers, the integer code and the computed result to the screen
here is the code that I have so far:
public static void main(String[] args) { Scanner read = new Scanner (System.in); int num1, num2, code, sum; System.out.println("Please enter a number"); num1 = read.nextInt();
I have to ask how many children's tickets you want to order. When I apply the code below, it just accepts whatever I input, including letters. I imagine it is to do with setting childrensTickets = 0? If I input a letter using the below it accepts - shouldn't it print the error given the input is not >=0?
System.out.print("How many children's tickets do you require? "); int childrensTickets = 0; boolean validChildrenValue = false; while (validChildrenValue == false) { if(aScanner.hasNextInt()) {
So I clipped this out of my Jeopardy game code and made it into its own project for testing. using this code I want to check to see if the input for wage is an integer?
import java.util.Scanner; public class test { /** * @param args */ public static void main(String[] args) { Scanner input = new Scanner (System.in);
Obviously right now if you enter "aflwkj" or some such for the wager, the program terminates. How to make a loop that will keep asking the user for a value for wage until the input is an integer?
System.out.print("To begin, please enter 1 to choose stand tickets or 2 to choose terrace tickets for your party. "); int standOrTerraceTickets = aScanner.nextInt(); while (standOrTerraceTickets != 1 && standOrTerraceTickets != 2) { System.out.print("Invalid input. Please enter 1 for stand tickets or 2 for terrace tickets. "); standOrTerraceTickets = aScanner.nextInt(); }
Okay I thought I had this working properly so that when the user entered anything other than 1 or 2 they would keep getting an error invalid input, however, that only works when the user enters an integer. If the user enters anything other than a number, the program crashes. How does one prevent this from happening?
I am new to java and programming in general. I figured out how to convert an integer input to binary however I am having issues doing the opposite of converting a user input binary number to a decimal.
I need to do this with basic math (or string depending on how I represent the binary) and no functions.
I know how to convert binary to integer on paper but I am having a hard time working it out in java.
I have to write a program that inputs a 5 digit integer from the keyboard and prints if the input number is a palindrome or not. I got it to work, but when my result prints it says "0 is a palindrome" or "0 is not a palindrome". How can I get rid of the 0 and replace it with the number input by the user?
import java.util.Scanner; public class Palindrome { public static void main(String[] args) { int number; int digit; int temp;
I am trying to create a program that first asks the user for an input in the form: condition = value, where condition is a word from the set {limit, deficient, abundant, perfect, prime}, and value is a positive integer. Then it verifies the input. If the input is invalid it prints a message indicating that and terminates. If the input is valid it prints a table with the number of abundant, deficient, perfect and prime numbers less than or equal to N, where N = 1, 2, 3, ..., limit.
My problem is with the input validation. i want it to read the value as a string and verify if it's integer.
Here is my code
import java.util.Scanner; public class Factors { public static void main (String[] args) { Scanner scan = new Scanner(System.in); int n, f, fsum, p=0, a=0, d=0,pe=0,limit=0,abundant=0, deficient=0; System.out.print("Enter stoping condition (condition = value): "); String cond = scan.next();