ISBN Checker- No Loops [closed] - java

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I'm trying to make a ISBN checker but it's not working. An example would be the user inputs 013601267 and return 0136012671. I'm not understanding what the problem is. No loops.
Any help would be great.
import java.util.*;
public class ISBN{
public static void main(String[]args){
Scanner in=new Scanner(System.in);
System.out.print("Enter 9 digit ISBN");
//variables//
int d1=in.nextInt();
int d2=in.nextInt();
int d3=in.nextInt();
int d4=in.nextInt();
int d5=in.nextInt();
int d6=in.nextInt();
int d7=in.nextInt();
int d8=in.nextInt();
int d9=in.nextInt();
int d10=(d1*1+d2*2+d3*3+d4*4+d5*5+d6*6+d7*7+d8*8+d9*9) %11;
//keyboard
if (d10==10){
System.out.print("ISBN"+d1+d2+d3+d4+d5+d6+d7+d8+d9+"X");}
else if(d10 !=10); {
System.out.print("ISBN"+d10);}
}
}

When someone enters their ISBN, are they entering it with spaces in between?
nextInt() will retrieve the integers separated by spaces, so it is likely that d1 is receiving the entire nine integers.
If you are entering them one at a time, then it should work.
Either enter the digits with spaces in between them, or each on their own line. Scanner will take care of the rest.
Note:
If you don't want the user to have to enter the digits one-by-one, try taking their input as:
String digits = in.nextLine();
You can reference each digit in that string with digits.charAt(0) etc.
int d1=Integer.parseInt("" + digits.charAt(0));
and so on. This will convert the single character digits.charAt(0) to your integer for the formula.
Hope this helps!

Related

Print the integers from 1 to a number given by user [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 2 years ago.
Improve this question
I'm doing the Java MOOC by Helsinki University. Stuck on the following problem:
Write a program which prints the integers from 1 to a number given by the user.
Sample output
Where to? 3
1
2
3
The code below outputs the expected results but is not accepted as valid. Any suggestions or pointers are welcome, thank you!
import java.util.Scanner;
public class FromWhereToWhere {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Write your program here
System.out.println("Where to?");
int userInput = Integer.valueOf(scanner.nextLine());
int start = 1;
while (start <= userInput) {
System.out.println(start);
start++;
}
}
}
Most likely the system that tests your program is stuffing values into standard input (System.in) with spaces, and assumes that you will read with .nextInt().
If that's not it, double check the program description; what is supposed to happen if I enter -1? 0? 1985985410395831490583440958230598? FOOBAR?
If it doesn't say, then presumably the verifier won't throw those inputs at you (if it does, file a bug with the MOOC provider, the course itself needs fixing if that is the case), but if it does, you're going to have to code those rules in, probably.
This shouldn't be it, but to exactly mirror the desired result, it's System.out.print("Where to? "); - note, no ln, and a trailing space.
You did not check if the user input is valid, I would suggest starting off with the following:
check if userInput is a valid number (includes numeric characters).
check if userInput is larger or equal to 1.
your answer is ok but can be optimized to the beginners levels if that is what your teacher is expecting because:
you can get an int directly from scanner, no need to use the wrapper class Integer.
you can use another loop ... a for loop
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Write your program here
System.out.println("Where to?");
int userInput = scanner.nextInt();
System.out.println("ok!");
for (int i = 1; i <= userInput; i++)
{
System.out.println(i);
}
}
Just try by Removing
System.out.println("Where to?");
with
System.out.print("Where to?");

Trying to get numeric value of uppercase & lowercase in Java [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I'm trying to answer the question that asks for user input in entering three alphabetical letters (eg. a=1, b=2,… A=101, B=102, etc) and then the total sum.
So far I can only get the first lowercase user input working correctly?
Any suggestions would be very helpful.
You can convert the letters from their ascii value
Scanner scnObj = new Scanner(System.in);
System.out.println("Please enter a word");
String input1 = scnObj.nextLine();
char[] arr=input1.toCharArray();
for(int i=0;i<arr.length;i++)
{
int ascii=(int)arr[i];
if(ascii>=97&&ascii<=122)
{ //
total=total+ascii-96; //ascii are set values, a-z = 97-122 etc
}
else if(ascii>=65&&ascii<=90)
{
total=total+ascii+36; //This will convert A-Z values to 101 etc values
}
else if(ascii>=48&&ascii<=57)
{
total=total+ascii-48; //value of the digit to be worth itself
}
else
{
total=total+0; //special character to be worth 0
}
}

How to find the closest element in an array [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I've tried looking for the answer to this question, but I haven't had a lot of luck. Basically I am asked to create the Fibonacci Code, and then allow a user input to look for there input in the sequence. If its in the sequence then it shows what index. If its not then it shows the two closest numbers to it.
So if a user inputs 4, the nearest elements would be 3 and 5 and the indexes would be 4 and 5.
I'm basically struggling with finding the nearest elements. I'm not exactly sure how to do it.
****update****
So I did figure it out thank you
1.Store the previous Fibonacci number in a buffer (you can initialize with -1)
2.update the buffer after every new number is calculated.
3.if the current number is not equal to the new number
3.A check if the number is greater than buffer and less than the new number
3.A.1)If yes, those two are your nearest numbers.
import java.util.Scanner;
public class Fibonacci {
public static void main(String[] args) {
int userInput;
Fibonacci.fibonacciAlgor(5);
}
public static void fibonacciAlgor(int userInput)
{
int i=0;
int buffer=-1;
int x=0,y=1;
System.out.println("Input: " + userInput);
while(i<1000000){
if(x==userInput){
System.out.println("Belongs to sequence: Yes "");
break;
}
else{
if(userInput>buffer&&userInput<x){
System.out.println("Belongs to sequence: No ");
System.out.println("Nearest Elements: " + buffer+","+x);
break;
}
}
buffer=x;
int temp=y;
y=x+y;
x=temp;
i++;
}
}
}

How to clarify whether the user's input was a binary number? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I was required to make a code that accepted a binary number (1's and 0's) and then counted how many ones were in that binary number. My code fulfills this purpose.
The second part of the exercise is this: if the user enters a number that is NOT binary, I must output that there is an error and keep prompting the user until they give a binary number.
Can someone show me how to incorporate this? I have tried several times but cannot make it click. Thanks! Here is my code.
import java.util.Scanner;
public class NewClass
{
public static void main( String [] args )
{
Scanner scan = new Scanner( System.in);
int i = 0, count = 0;
String number;
System.out.println("Please enter a binary number.");
number = scan.next();
String number1 = "1";
while ((i = number.indexOf(number1, i++)) != -1) {
count++;
i += number1.length();
}
System.out.println("There are "+ count + " ones in the binary number.");
}
}
You already know how to find all of the 1's; you should be able to do something similar to find all of the 0's.
If the sum of those two counts is not the length of the string, there must be at least one illegal character in it.
If you use the parseInt method you can do
Integer.parseInt("1100110", 2) returns 102
or in your case
int intVal = Integer.parseInt(number, 2);
as per the javadocs
Throws:
NumberFormatException - if the String does not contain a parsable int.

Why can't I read indefinately doubles using this loop? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I'm trying to read some data from a user and do some very simple calculations with them, but for some reason I can't explain, the program stops after first 2-3 inputs (given they are doubles). Can someone explain this behaviour?
My code:
Scanner in = new Scanner(System.in);
System.out.println("Enter your values, 'q' to quit: ");
double average, input, smallest, largest, range;
average = smallest = largest = range = Double.parseDouble(in.nextLine());
int counter = 1;
while (in.hasNextDouble()) {
input = Double.parseDouble(in.nextLine());
smallest = input < smallest ? input: 0;
largest = input > largest ? input: 0;
average += input;
counter++;
}
Consider this input:
1.23
4.56 7.89
To Scanner this looks like a valid sequence of three doubles on two separate lines. When you call nextLine to obtain the first double, it works fine, because the number occupies the entire string.
When you do the same for the next double, the string that you try to parse looks like this:
"4.56 7.89"
This string is not a valid double, so you cannot parse it.
One approach to deal with this problem is to call nextDouble, and avoid parsing altogether. Pairing up hasNextDouble() with nextDouble() has an advantage of not requiring users to put their data on different lines.
Note: The first call to Double.parseDouble(in.nextLine()) happens without checking that Scanner has next double, so your program could crash on initial bad input.

Categories