Prompt user to enter a social security number in the format DDD-DD-DDDD, where D is a digit. Displays "Valid SSN" for a correct ssn, and "Invalid SSN" otherwise.I have it working I am just looking for other ways to solve this with an array maybe or something simpler. I have used if statements here:
public static boolean checkSSN(String social) { boolean valid = false; // 9 digits and 2 hyphens. First three characters, 5 and 6, and 8, 9, // 10, 11 are digits
From this, I need to extract the statements of variables that do not start with _G . I mean, I need to extract, Y in 1..15 , __X in 1..15 /17/20 but not _G7145 in 10..15 / 16.
I am using regular Expression for this as [^_G]^[A-Za-z0-9_]+ in|ins [-9 -9]..[-9-9] [/[-9-9]..[-9-9]]+
I want to do a simple search and replace regular expression of lines. I am very unfamilar with Java regular expressions, and I'm not sure how to do something as simple as what I want to do. I have lines that look like this...
The reason I want to use regular expressions is because I want to use the replaceAll method of the java.lang.String object. If I use replace I have to convert my strings into char arrays, and my code becomes bulky.
Assuming that x, y, and z are integer variables, which of the following three logical expressions are equivalent to each other, that is, have equal values for all possible values of x, y, and z?
(x == y && x != z) || (x != y && x == z) (x == y || x == z) && (x != y || x != z) (x == y) != (x == z)
None of the three
A. I and II only B. II and III only C. I and III only D. I, II, and III
I selected B, but got it wrong. I really think I need understanding boolean logic. The correct answer says something else but I don't get the logic. Here is the correct answer:
Answer Key : The following model answer has been provided to you by the grader. Carefully compare your answer with the one provided here.
Expression III is the key to the answer: all three expressions state the fact that exactly one out of two equalities, x == y or x == z, is true. Expression I states that either the first and not the second or the second and not the first is true. Expression II states that one of the two is true and one of the two is false. Expression III simply states that they have different values. All three boil down to the same thing. The answer is E.
In exercise 4, I get the same problem:
The expression !((x <= y) && (y > 5)) is equivalent to which of the following?
A. (x <= y) && (y > 5) B. (x <= y) || (y > 5) C. (x >= y) || (y < 5) D. (x > y) || (y <= 5) E. (x > y) && (y <= 5)
Exercise 4 ABCDE Incorrect Score: 0 / 1 Submitted: 2/10/2014 8:21pm Your answer is incorrect. Answer Key
The following model answer has been provided to you by the grader. Carefully compare your answer with the one provided here. The given expression is pretty long, so if you try to plug in specific numbers you may lose a lot of time. Use De Morgan's Laws instead:
!((x <= y) && (y > 5)) !(x <= y) || !(y > 5)
When ! is distributed, && changes into ||, and vice-versa
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?
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.
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 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.
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();
a. Write a Java program to input 10 integer numbers into an array named fmax and determine the maximum value entered. Your program should contain only one loop, and the maximum should be determined as array element values are being input. (Hint: Set the maximum equal to the first array element, which should be input before the loop used to input the remaining array values.)
b. Repeat 1a, keeping track of both the maximum element in the array and the index number for the maximum. After displaying the numbers, display these two messages:
The maximum value is: _________ This is element number __________ in the list of numbers
Have your program display the correct values in place of the underlines in the messages.
c. Repeat 1b, but have your program locate the minimum value of the data entered.
I did parts a and b but for part see i just want to know if i did it correctly or not
import java.util.Scanner; public class MinimumValueArray { public static void main(String[] args) { //Variable Declaration Scanner keyboard = new Scanner(System.in); int size = 10;
[Code] ,.....
When I run it i get this The minimum value is 0.0
The element that holds the value is 0 right away. is this right for the minimum or am i supposed to enter values and it will display the minimum value like in parts a and b wit the maximum? will the minimum just always be 0 or ?
Write a Java program that reads a positive, non-zero integer as input and checks if the integer is deficient, perfect, or abundant.
A positive, non-zero integer, N, is said to be perfect if the sum of its positive proper divisors (i.e., the positive integers, other than N itself, that divide N exactly) is equal to the number itself. If this sum is less than N, the number is said to be deficient. If the sum is greater than N, the number is said to be abundant.For example, the number 6 is perfect, since 6 = 1 + 2 + 3, the number 8 is deficient, since 8 > 1 + 2 + 4, while the number 12 is abundant, since 12 < 1 + 2 + 3 + 4 + 6.
Write a program that reads from the user an integer and reduce it by multiplying its non-zero digits. The result of the multiplication is a number which is to be reduced as the initial one. This process continues until an integer of one single digit is obtained. For example:
Your program should display the number obtained in every iteration.
Sample run1 Enter an integer: 64734502 After iteration 1: 20160 After iteration 2: 12 After iteration 3: 2
Sample run2 Enter an integer: 97737999 After iteration 1: 6751269 After iteration 2: 22680 After iteration 3: 192 After iteration 4: 18 After iteration 5: 8
Write a program that prompts the user to input an integer and then outputs both the individual digits of the number and the sum of the digits.
Now I have a code for spacing out the integers, and I have a separate code for adding the digits. But I am not sure how to merge them together. Both of them work independently
Spacing code: import java.util.*; public class SumoftheIntegers { static Scanner console=new Scanner(System.in); public static void main(String []args) { int num1, test, rem; int counter = 0;
[Code]...
Now the sum of the integers code:
import java.util.Scanner; public class sum { public static void main(String[] args) { // Create a Scanner Scanner input = new Scanner(System.in); // Enter amount System.out.print("Enter an integer: "); int integer = input.nextInt();
so i'm following a java tutorial from the book and it has a few challenge questions. and i'm stucked on one. i think i just don't understand what is it that its asking me. heres the question, Write a statement that reads a user's input integer into the defined variable, and a second statement that prints the integer. assuming scanner is given, and i checked my heading code is ok.
Scanner scnr = new Scanner(System.in); int userNum = 0; System.out.println("What is the product of 8 time 2"); userNum = scnr.nextInt();
Create a method called mirrorImage, which takes two integer arrays as input parameters. You may assume that the two actual parameters have the same length. Your method should return true if the arrays are the reverse of each other. Otherwise mirrorImage should return false.
Examples:
data1:{1,2,3} data2:{3,2,1} ==> true
[code].....
I'm pointing a place outside of the array or something
Goal is to: Write a program that prompts the user to input an integer and then outputs both the individual digits of the number and the sum of the digits.
First I don't know where I made mistakes here, and the only error it finds right now is that str2 was not initialized and I cannot figure out where/when to initialize it.
import javax.swing.JOptionPane; public class DigitsAndSum { public static void main (String[] args) { String str1; String str2; int int1 = 0; int int2 = 0;
I've been working on a question using parallel arrays where the user inputs an integer 1-12 and the output will be the name of the month and the number of days in that month. This is what I have so far
import java.util.*; public class DaysMonth { public static void main (String args[]) { Scanner keyIn = new Scanner(System.in); int[] days = new int[]{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};