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
}
}
Related
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!
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.
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.
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
In my application user can enter number between 0 to 999
However, whether he enter "0" or "00" or "000" should understood and treated differently.
All the functions that in know convert the input into single zero "0" before putting it into the variable.
private static void AddAmountChart1D (){
String str_pana;
int int_pana, Amount;
Scanner UserInput = new Scanner(System.in);
System.out.print("Enter the game pana : ");
str_pana = UserInput.nextLine(); /* This line should treat "0", "00" & "000" as different types of strings.
Presently it make all three as "0"
System.out.print("Enter the Amount : ");
Amount = UserInput.nextInt();
int_pana = get_int_pana(str_pana);
ChartAmount1D[int_pana] = ChartAmount1D[int_pana]+Amount;
}
Your purpose is not that much clear to me.
But if you use
UserInput.nextLine();
this will be treated as a String so it'll return the zeros like "0", "00" or "000".
Further if you want the value to be processed as a number simply use
int number = Integer.parseInt(str_pana);
and use the number.
Edit
Please try this and check the results for "0", "00" or "000".
if (str_pana.equals("0")) {
System.out.println("Zero");
}else if (str_pana.equals("00")) {
System.out.println("Zero-Zero");
}else if (str_pana.equals("000")) {
System.out.println("Zero-Zero-Zero");
}
Actually my point is to check the zeroes so that you can set the integer values in the if clause and convert them to respective integer values to be used later.
Read whole line.
Use function
public boolean startsWith("0")
From String class
If it doesn't return true - just parse.
Else count zeros
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 7 years ago.
Improve this question
How to write a java program to get a string as a key board input. Then count the number of letters, digits and spaces available in that string and display the count of letters, digits and spaces.
This is honestly... really, really easy and something that really should be taught to you in an introductory course or something.
Scanner scan = new Scanner(System.in);
String userInput = scan.nextLine();
You will need to put, right after package, import java.util.Scanner. To do the other thing, i.e. counting the number of characters in the String, simply invoke length().
Pass a string as an argument in this method you get count of letter, space, and digit
public void count(String str) {
int space = 0;
int digit = 0;
int letter =0;
for (int f4 = 0; f4 < str.length(); f4++) {
if (Character.isSpace(str.charAt(f4)))
space++;
if(Character.isAlphabetic(str.charAt(f4)))
letter++;
if(Character.isDigit(str.charAt(f4)))
digit++;
}
System.out.println("space = "+space+", digit = "+digit+", letter = "+letter);
}