I'm trying to make a program where it asks for a letter from the alphabet. Lets say I choose the letter "b". The "b" should be shown in quotation marks in the program. I'm trying to learn Java, I know HTML and CSS, but Java is new to me, so go easy.
So in practice:
Choose a letter:
d
abc"d"efghijklmnopqrstuvwxyz
I've figured out how to print the alphabet
import java.util.Scanner;
public class Characters {
public static void main(String[] args) {
char c;
for(c = 'a'; c <= 'z'; ++c)
System.out.print(c + " ");
}
}
(I have added scanner, because I'll ask the user for the letter)
A very concise example:
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner s = new Scanner(System.in);
char c = s.next().charAt(0);
for(char i = 'a'; i < 'z'; i++){
System.out.print(i == c ? "\""+c+"\"" : i);
}
}
}
Use java.util.Scanner's .next() to read the next word, then get the first character of the word.
Sample Run:
a
"a"bcdefghijklmnopqrstuvwxy
Since Java recognises quotation marks for text, you need to add a \ before each caracter to indicate to java that you wish for it to be part of the text. For example, if you want to show "b" with quotation mark, your final string is going to be a\"b\"cdefghijklmnopqrstuvwxyz
First, we would have to read the input char:
Scanner scanner = new Scanner(System.in);
char c = scanner.next().charAt(0); // Get char
Then, I would make sure that our char is in the alphabet:
if(!('a' <= c && c <='z')) {
return; // NOT IN ALPHABET
}
If we got this far, we successfully read a character, which is in the alphabet.
Now, we can print out the modified alphabet.
for(char letter = 'a'; letter <= 'z'; letter++) {
if(letter == c) {
System.out.print("\"" + c + "\"");
}else {
System.out.print(letter);
}
}
Related
The output is fine but the output is in Uppercase letter, I want the output to be lowercase character, what should I do?
import java.util.Scanner;
public class Q2 {
public static void main(String[] args) {
int a[]=new int[26];
Scanner sc = new Scanner (System.in);
String str=sc.nextLine();
for(int i = 0; i<str.length();i++) {
if(str.charAt(i)>=65 && str.charAt(i)<=90) {
a[str.charAt(i)-65]++;
}
else if(str.charAt(i)>=97 && str.charAt(i)<=122) {
a[str.charAt(i)-97]++;
}
}
for(int i=0;i<26;i++) {
if(a[i]>0) {
System.out.print(" "+(char )(i+65)+ "(" + a[i]+")");
}
}
}
}
'A' is 65. 'a' is 97. Use 97 instead of 65 in your final loop.
Alternatively use 'a' directly instead of the number. This makes it more obvious what you're doing in that code.
It may be more straightforward to convert the entire string to lower case to avoid checking for upper case letters.
Also, as suggested earlier using character literals as a instead of 97 would make the code more readable.
Scanner sc = new Scanner(System.in);
int a[] = new int[26];
String str = sc.nextLine().toLowerCase();
for (char c : str.toCharArray()) {
if (c >= 'a' && c <= 'z') {
a[c - 'a']++;
}
}
for (char c = 'a'; c <= 'z'; c++) {
int i = c - 'a';
if (a[i] > 0) {
System.out.print(" "+ c + "(" + a[i] + ")");
}
}
Check out this post with a accepted answer How do I convert strings between uppercase and lowercase in Java?
You could probably do something like this
import java.util.Scanner;
public class Q2 {
public static void main(String[] args) {
int a[]=new int[26];
Scanner sc = new Scanner (System.in);
String str=sc.nextLine();
String lowerCaseInput = str.toLowerCase();
System.out.print(lowerCaseInput);
}
}
I am a beginner in java and am having trouble with printing out userinput with charAt(). I need to create a program that takes userinput and adds "op" before vowels in that text. (example: Userinput -> "Beautiful" would be translated as "Bopeautopifopul") I am struggling to figure how to write this. So far I have come up with this small bit.
import java.util.Scanner;
public class oplang {
static Scanner userinput = new Scanner(System.in);
public static void main(String[] args) {
char c ='a';
int n,l;
System.out.println("This is an Openglopish translator! Enter a word here to translate ->");
String message = userinput.nextLine();
System.out.println("Translation is:");
l = message.length();
for (n=0; n<l; n++);
{
c = message.charAt();
if (c != ' ');
{
System.out.println(" ");
}
c++;
}
}}
I would use a regular expression, group any vowels - replace it with op followed by the grouping (use (?i) first if it should be case insensitive). Like,
System.out.println("Translation is:");
System.out.println(message.replaceAll("(?i)([aeiou])", "op$1"));
If you can't use a regular expression, then I would prefer a for-each loop and something like
System.out.println("Translation is:");
for (char ch : message.toCharArray()) {
if ("aeiou".indexOf(Character.toLowerCase(ch)) > -1) {
System.out.print("op");
}
System.out.print(ch);
}
System.out.println();
And, if you absolutely must use charAt, that can be written like
System.out.println("Translation is:");
for (int i = 0; i < message.length(); i++) {
char ch = message.charAt(i);
if ("aeiou".indexOf(Character.toLowerCase(ch)) > -1) {
System.out.print("op");
}
System.out.print(ch);
}
System.out.println();
import java.util.Scanner;
public class TwoDotSevenNumbaTwo {
public static void main(String[] args) {
// TODO Auto-generated method stub
String input;
int num1, num2, leng;
char word;
Scanner inputscan = new Scanner(System.in);
System.out.println("Give me some love baby");
input = inputscan.nextLine();
leng = input.length();
num1 = 0;
num2 = 1;
while (num1 < leng) {
input = input.replaceAll(" ", "<space>");
System.out.println(input.charAt(num1));
num1++;
}
}
}
I can't seem to figure out how to get the <space> on a single line. I know I can't do it because it is a char but I can't find a way around it.
You could do
for(int i = 0; i < leng; ++i) {
char x = input.charAt(i);
System.out.println(x == ' ' ? "<space>" : x);
}
Once you've stored the input as a String, you can write:
// Break the string into individual chars
for (char c: input.toCharArray()) {
if (c == ' ') { // If the char is a space...
System.out.println("<space>");
}
else { // Otherwise...
System.out.println(c);
}
}
Regex powa:
yourText.replaceAll(".", "$0\n").replaceAll(" ","<space>");
Explanation:
First replaceAll takes every charachter (. = any character) and replaces it with the same character ($0 = matched text) followed by a newline \n, thusly every character is on separate line.
Second replaceAll just replaces every acctual space with the word "<space>"
For java regex tutorial, you can follow this link or use your favourite search engine to find plenty more.
The Captain Crunch decoder ring works by taking each letter in a string and adding 13 to it. For example, 'a' becomes 'n' and 'b' becomes 'o'. The letters "wrap around" at the end, so 'z' becomes 'm'.
This is what I've got after editing it a bit from peoples comments, but now it keeps telling me that output may have not been initialized and I have no clue why... also is there anything else I need to fix in my program?
In this case, I am only concerned with encoding lowercase characters
import java.util.Scanner;
public class captainCrunch {
public static void main (String[] Args) {
Scanner sc= new Scanner(System.in);
String input;
System.out.print("getting input");
System.out.println("please enter word: ");
input= sc.next();
System.out.print(" ");
System.out.print("posting output");
System.out.print("encoding" + input + " results in: " + encode(input));
}//end of main
public static String encode(String input){
System.out.print(input.length());
int length= input.length();
int index;
String output;
char c;
String temp= " ";
for (index = 0; index < length; index++) {
c = input.charAt(index);
if (c >= 'a' && c <= 'm') c += 13;
else if (c >= 'n' && c <= 'z') c -= 13;
output= temp + (char)(c);
}
return output;
}
}
It's called ROT13 encoding.
http://en.wikipedia.org/wiki/ROT13
To fix your algorithm you just need:
public static String encodeString (String input) {
StringBuilder output = new StringBuilder();
for (int i=0;i<input.length;i++) {
char c = input.charAt(i)
output.append(c+13); // Note you will need your code to wrap the value around here
}
return output.toString();
}
I haven't implemented the "wrapping" since it depends on what case you need to support (upper or lower) etc. Essentially all you need to do though is look at the range of c and then either add or subtract 13 depending on where it is in the ASCII character set.
You don't have any loop iterating over the character of your string. You have to iterate other the string from 0 to string.length().
The output may have not been initialized:
String output = "";
If you don't put = "" then you have never initialized it (it's essentially random garbage, so the compiler won't let you do it).
I've been trying and trying and there is no way.
I want to spend a lowercase letter to uppercase equivalent.
I know and know how to use the method toUpperCase () but I want to do without it.
This is my failing code:
import java.util.Scanner;
public class ConvertLetters {
public static void main(String[] args) {
Scanner stdin = new Scanner(System.in);
System.out.print("enter a lower case letter");
char letter = stdin.nextLine();
int letter2 = 'A' + (letter - 'a');
System.out.println("and we will refund your letter capitalized");
System.out.print("Your letter: " + letter);
System.out.print("upper case equates to:");
System.out.print(letter2);
}
}
You want to finish with a char
char letter = stdin.nextLine().charAt(0);
char letter2 = (char) ('A' + (letter - 'a'));
or
char letter2 = (char) (letter - 32);
I see two problems with this code. First, you assign nextLine(), which is a string. You may want something like
char letter = stdin.nextLine().charAt(0);
instead. Second, you print int, try
System.out.print((char)letter2);
With these changes, I think your code will give you better results.
Naturally, that won't work for non-ascii characters, anyway.