import java.util.Scanner;
public class Ariete25Programs {
public static void main(String[] tin) {
char again = 0;
int choose;
if(choose == 7) {
char again7 = 0;
do
{
System.out.println("---Vowels and Consonants---");
String line;
System.out.println("Enter word: ");
line = sc.nextLine();
String vowels = " ", consonants = " ", digits = " ", spaces = " ";
for(int i = 0; i < line.length(); ++i)
{
char ch = line.charAt(i);
if(ch == 'a' || ch == 'e' || ch == 'i'
|| ch == 'o' || ch == 'u' || ch == 'A' || ch == 'E' || ch == 'I'
|| ch == 'O' || ch == 'U')
{
vowels+=line.charAt(i);
}
else if((ch >= 'a'&& ch <= 'z'))
{
consonants+=line.charAt(i);
}
}
System.out.println("Vowels: " + vowels);
System.out.println("Consonants: " + consonants);
System.out.println("Try Again? (Y/N)");
again7 = sc.next().charAt(0);
if(again7 == 'n') {
System.out.println("Menu? [y/n]");
again = sc.next().charAt(0);
}
else if(again7 == 'n') {
System.out.println("Thank you for using the programs!");
System.exit(0);
}
}
while(again7 == 'y');
then the result when you choose 7 becomes like this
-----Vowels and Consonants-----
Enter word:
Try again?(Y/N)
can someone help me? I can't seem to figure out the problem in my codes. It keeps on skipping.
I guess you do something like choose = sc.nextInt(); you should try choose = Integer.parseInt(sc.nextLine());
Please replace sc.next().charAt(0) with sc.nextLine().charAt(0).
Documentation:
Scanner.next()
Scanner.nextLine()
Moreover else if(again7 == 'n') is wrong since you already did the same check few lines before, so you'll never execute the code in that else if block
do
{
System.out.println("---Vowels and Consonants---");
String line;
System.out.println("Enter word: ");
line = sc.nextLine();
String vowels = " ", consonants = " ", digits = " ", spaces = " "; // digits and spaces are not used
int end = line.length();
for (int i = 0; i < end; ++i) {
char ch = line.charAt(i);
if(ch == 'a' || ch == 'e' || ch == 'i'
|| ch == 'o' || ch == 'u' || ch == 'A' || ch == 'E' || ch == 'I'
|| ch == 'O' || ch == 'U') {
vowels += line.charAt(i);
}
else if ((ch >= 'a'&& ch <= 'z')) {
consonants += line.charAt(i);
}
}
System.out.println("Vowels: " + vowels);
System.out.println("Consonants: " + consonants);
System.out.println("Try Again? (Y/N)");
again7 = sc.nextLine().charAt(0);
if (again7 == 'n') {
System.out.println("Menu? [y/n]");
again = sc.nextLine().charAt(0);
if (again == 'n') {
System.out.println("Thank you for using the programs!");
break;
} else {
// display Menu
}
}
} while(again7 == 'y');
Related
import java.util.*;
import java.lang.String;
public class counter
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int space = 0,vowel = 0,chara = 0,i;
System.out.println(" Enter String ");
String s =in.nextLine();
for( i = 0; i < s.length(); i++)
{
char ch = in.next().charAt(i);
if(ch == ' ')
space++;
if(ch == 'e' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')
vowel++;
else
chara++;
System.out.println("Number of Vowels = "+vowel);
System.out.println("Number of Spaces = "+space);
System.out.println("Number of Char = "+chara);
}
}
}
What is the problem? I have put three counters to count. I am coding in Eclipse and whenever I check the console I am not able to count the characters. It is just accepting inputs and not doing anything else.
Remove char ch = in.next().charAt(i);, and replace the other instances of ch with s.charAt(i)
The first charAt check should also be a, you have e twice.
Then move System.out.println... outside of the loop.
Online Demo
Just a couple of typo errors. Change your code with,
String s = in.nextLine().toLowerCase();
And
char ch = s.charAt(i);
And
if(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')
In your code you are using lowercase letters to compare with the user input. So you should convert the user input to lower case first. Your current code ignores all uppercase vowels(E, A, I ...). Use toLowerCase().
By using in.next() the Scanner is waiting for an input. Since you have already taken an input using nextLine() you can use that.
Next one is obviously a typographical error. Vowels are A, E, I, O, U.
You should change s = in.next().charAt(i);to String s =in.nextLine() and put System.out.printlnpart outside of for loop.
Also there is double 'e' (with the help of #Ted Hopp):
ch == 'e' || ch == 'e' to ch == 'e' || ch == 'a'
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int space = 0,vowel = 0,chara = 0,i;
System.out.println(" Enter String ");
String s =in.nextLine();
for( i = 0; i < s.length(); i++)
{
char ch = s.charAt(i);
if(ch == ' ')
space++;
if(ch == 'e' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')
vowel++;
else
chara++;
}
System.out.println("Number of Vowels = "+vowel);
System.out.println("Number of Spaces = "+space);
System.out.println("Number of Char = "+chara);
}
I'm new to Java and programming in general. This is baffling me, and I feel like I may just need to scrap what I have and go a different route, but I don't know what route to take.
Below is the code I've written for translating a line from the user into braille. It works! Unfortunately, it works vertically. How can I, after using three lines to create the braille, make the next braille character appear next to the previous one instead of below it?
import java.util.*;
public class Program4
{
public static String code1 = ". |";
public static String code2 = " .|";
public static String code3 = "..|";
public static String code4 = " |";
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
System.out.println("Program 4 by Ross Walker");
System.out.println();
String s;
System.out.println("Please give a line to translate");
s = keyboard.nextLine();
for(int i = 0; i < s.length(); i++)
{
char c = s.charAt(i);
if(c == '1' || c == 'a')
{
//new Program4().letterA(s.charAt(i));
System.out.printf("%s%n%s%n%s%n", code1, code4, code4);
System.out.println();
}
if((c == '2' ) || (c == 'b'))
{
System.out.printf("%s%n%s%n%s%n", code1, code1, code4);
System.out.println();
}
if((c == '3') || (c == 'c'))
{
System.out.println(code3);
System.out.println(code4);
System.out.println(code4);
System.out.println();
}
if((c == '4') || (c == 'd'))
{
System.out.println(code3);
System.out.println(code2);
System.out.println(code4);
System.out.println();
}
if((c == '5') || (c == 'e'))
{
System.out.println(code1);
System.out.println(code2);
System.out.println(code4);
System.out.println();
}
if((c == '6') || (c == 'f'))
{
System.out.println(code3);
System.out.println(code1);
System.out.println(code4);
System.out.println();
}
if((c == '7') || (c == 'g'))
{
System.out.println(code3);
System.out.println(code3);
System.out.println(code4);
System.out.println();
}
if((c == '8') || (c == 'h'))
{
System.out.println(code1);
System.out.println(code3);
System.out.println(code4);
System.out.println();
}
if((c == '9') || (c == 'i'))
{
System.out.println(code2);
System.out.println(code1);
System.out.println(code4);
System.out.println();
}
if((c == '0') || (c == 'j'))
{
System.out.println(code2);
System.out.println(code3);
System.out.println(code4);
System.out.println();
}
}
}
}
I've only written up to j in the alphabet. I'll continue if this is the only way I can get it done, but I'd like your help to do it the right way if possible.
I have an assignment to do. I need to convert alphabets to telephone numbers. Add hyphen after the 3rd number and after every subsequent 4 numbers. Then I need to allow user to keep on entering input.
I've already gotten 3/4 of my codes done. It's not working. I'm only stuck with the part to keep prompting user for input.
This is my java code so far. Please help me and tell me what's wrong with my code. I'm new to Java, please be nice. Thank you!
public static void main (String[] args)
{
Scanner input = new Scanner (System.in);
System.out.println("Enter letters: ");
String phonenumber = input.next();
String phone = phonenumber.replaceAll("\\s", " ");
int count = 0;
int prevCount = 0;
while (!phone.equals("#")){
for (int i = 0; i < phone.length(); i++)
{
char letter = Character.toLowerCase(phone.charAt(i));
if (letter == 'a' || letter == 'b' || letter == 'c')
{
System.out.print(2);
count++;
}
if (letter == 'd' || letter == 'e' || letter == 'f')
{
System.out.print(3);
count++;
}
if (letter == 'g' || letter == 'h' || letter == 'i')
{
System.out.print(4);
count++;
}
if (letter == 'j' || letter == 'k' || letter == 'l')
{
System.out.print(5);
count++;
}
if (letter == 'm' || letter == 'n' || letter == 'o')
{
System.out.print(6);
count++;
}
if (letter == 'p' || letter == 'q' || letter == 'r' || letter == 's')
{
System.out.print(7);
count++;
}
if (letter == 't' || letter == 'u' || letter == 'v')
{
System.out.print(8);
count++;
}
if (letter == 'w' || letter == 'x' || letter == 'y' || letter == 'z')
{
System.out.print(9);
count++;
}
if (count == 3 || count - prevCount == 4)
{
System.out.print('-');
prevCount = count;
}
}
System.out.println();
System.out.println("Enter letters: ");
input.nextLine();
}
}
use a loop:
while (true)
{
System.out.println("Enter letters: ");
String phonenumber = input.next();
if something break;
or
while ((String phonenumber = input.next())!=null)
{
// ...
System.out.println("Enter letters: ");
}
Your problem is here :
System.out.println();
System.out.println("Enter letters: ");
input.nextLine(); // you forgot to assign the String returned by nextLine
// into your phone variable
Beside that, you should reset your counters in each iteration of the while loop.
while (!phone.equals("#")) {
int count = 0;
int prevCount = 0;
... your for loop ...
System.out.println();
System.out.println("Enter letters: ");
phone = input.nextLine();
}
Write a class with a constructor that accepts a String object as its argument. The class should have a method that
returns the number of vowels in the string, and another method that returns the number of consonants in the string.
Demonstrate the class in a program that performs the following steps:
The user is asked to enter a string.
The program displays the following menu:
a: Count the number of vowels in the string
b: Count the number of consonants in the string
c: Count both the vowels and consonants in the string
d: Enter another string
e: Exit the program.
The program performs the operation selected by the user and repeats until the user selects e, to exit the program.
When the user enters a string and checks for consonants, or vowels, or both once, it works just fine.
But when it is passed again, the program doubles the value.
Thoughts?
Class:
public class CounterClass
{
String string;
int vow = 0;
int con = 0;
char letter = ' ';
int enter = 0;
/**
* This is a Constructor
* It accepts a string object.
*/
public CounterClass(String aString)
{
this.string = aString;
}
/* #returns an int
* This method checks for char and then adds it to a vowel counter */
public int getVowels(String aString)
{
int length = aString.length();
for( int i = 0; i < length ; i++)
{
char c = aString.charAt(i);
if (c == 'a' || c == 'e' || c == 'i'|| c == 'o'
|| c == 'u' ||c == 'A' || c == 'E' || c == 'I'
|| c == 'O' || c == 'U')
{
vow++;
}
}
return vow;
}
/* #returns an int
* This method checks for char and then adds it to a con counter */
public int getConsonants(String aString)
{
int length = aString.length();
for( int i = 0; i < length ; i++)
{
char c = aString.charAt(i);
if (c == 'B' || c == 'C' || c == 'D'|| c == 'F'
|| c == 'G' ||c == 'H' || c == 'J' || c == 'K'
|| c == 'L' || c == 'M'|| c == 'N' || c == 'P'|| c == 'Q'
|| c == 'R' ||c == 'S' || c == 'T' || c == 'V'
|| c == 'W' || c == 'X'|| c == 'Z' || c == 'b' || c == 'c' || c == 'd'|| c == 'f'
|| c == 'g' ||c == 'h' || c == 'j' || c == 'k'
|| c == 'l' || c == 'm'|| c == 'n' || c == 'p'|| c == 'q'
|| c == 'r' ||c == 's' || c == 't' || c == 'v'
|| c == 'w' || c == 'x'|| c == 'z')
{
con++;
}
}
return con;
}
}
Main Program :
import javax.swing.JOptionPane;
public class NumberCounters
{
public static void main(String [] args)
{
//Declaring variable for later use.
String input = " ";
String letters = " ";
char letter = ' ';
StringBuilder sb = new StringBuilder();
//This creates an instance of CounterClass
CounterClass string = new CounterClass(input);
//Tests
System.out.println(string.getVowels(input));
System.out.println(string.getConsonants(input));
input = JOptionPane.showInputDialog("Please enter a string"); // Getting a string input from the user.
//Does a loop until the user selects 'e'
do{
//sets 'letters' to the inputdialog from the menu
letters = JOptionPane.showInputDialog(
"a: Count the number of vowels in the string\n" +
"b: Count the number of consonants in the string\n" +
"c: Count both the vowels and consonants in the string\n"+
"d: Enter another string\n" + "e: Exit the program");
letter = letters.charAt(0); // letters is a string so I used charAt to grab the first letter from the input.
switch(letter)
{
case 'A':
case 'a': JOptionPane.showMessageDialog(null, "You have " + string.getVowels(input) + " vowels in your string");
break;
case 'B':
case 'b': JOptionPane.showMessageDialog(null, "You have " + string.getConsonants(input) +
" consonants in your string");
break;
case 'C':
case 'c': JOptionPane.showInputDialog(null, "You have " + string.getConsonants(input)
+ " Consonants and " + string.getVowels(input)
+ " vowels in your string");
break;
case 'D':
case 'd': input = JOptionPane.showInputDialog("Please enter a string");
break;
case 'E':
case 'e':
System.exit(0);
}
}while( letter != 'e');
}
}
try setting your values back to 0 in each method you could also remove the getConsonants method and just place an else(example of edited code below)
public int getVowels(String aString)
{
//set values to back to 0 before checking
vow = 0;
con = 0;
int length = aString.length();
for( int i = 0; i < length ; i++)
{
char c = aString.charAt(i);
if (c == 'a' || c == 'e' || c == 'i'|| c == 'o'
|| c == 'u' ||c == 'A' || c == 'E' || c == 'I'
|| c == 'O' || c == 'U')
{
vow++;
}
else if(c == '1' || c == '2' || c == '3' || c == '4' || c == '5' || c == '6' || c == '7' || c == '8' || c == '9' || c == '0' ||){
//in case you add numbers put here
}
else
{
con++;
}
}
return vow;
I'm having trouble with this simple exercise. What I have to do is to take the vowels from the string.
This returns all the vowels in the string, but what I want is that if there are multiple letters of same vowel, just return one.For example, using the string "aaa eee iii" should give "a e i".
public static void getVowels(char aChar, String aString){
System.out.print("Your string has the following vowels: ");
for (int i = 0; i < aString.length(); i++){
if ((aString.charAt(i) == 'a') || (aString.charAt(i) == 'e') || (aString.charAt(i) == 'i') || (aString.charAt(i) == 'o') || (aString.charAt(i) == 'u')) {
aChar = aString.charAt(i);
System.out.print(aChar + " ");
}
}
}
I would recommend either adding each vowel found to a HashSet<Character>, or calling aString.contains() with each vowel in turn. You can also use aString.toLowerCase() so that you only have to check for lowercase vowels.
Edit your code as follows:
public static void getVowels(char aChar, String aString)
{
System.out.print("Your string has the following vowels: ");
String vowels="";
for (int i = 0; i < aString.length(); i++)
{
if ((aString.charAt(i) == 'a') || (aString.charAt(i) == 'e') || (aString.charAt(i) == 'i') || (aString.charAt(i) == 'o') || (aString.charAt(i) == 'u'))
{
if(!vowels.contains(String.valueOf(aString.charAt(i))))
vowels+=aString.charAt(i);
}
}
for(int i=0;i<vowels.length();i++)
System.out.print(vowels.charAt(i)+" ");
}
EDIT :
Alternatively,
public static void getVowels(char aChar, String aString){
System.out.print("Your string has the following vowels: ");
char vowels[]={'a','e','e','o','u'};
for (char vowel : vowels)
{
if(aString.indexOf(vowel)>=0)
{
System.out.print(vowel+" ");
}
}
}
Why are you doing for loop? Just check String.IndexOf() and if that character is present print it.
You need to have a string where you keep on adding unique vowels checking before hand whether it exists. The below program will clear your doubt.
public class TestWovel {
public static void main(String[] args) {
String vowel = "aaaeeeiiizncnzcxjswdmmnmxcuuooo";
String uniqueVowels = "";
for(int i=0;i<vowel.length();i++){
char vowelFound = vowel.charAt(i);
if((vowelFound == 'a' || vowelFound == 'e' || vowelFound == 'i' || vowelFound == 'o' || vowelFound == 'u') && (uniqueVowels.indexOf(vowelFound) == -1)){
uniqueVowels+=vowelFound;
}
}
System.out.println(uniqueVowels);
}
}
You could use an integer array whose indexes are ASCII codes. When you see a vowel, check its count in the array. If the count is 0, print the vowel and increase the count. For example, 'a' would be stored in arr[97]:
public static void getVowels(String aString) {
int[] arr = new int[128];
char c;
System.out.print("Your string has the following vowels: ");
for (int i = 0; i < aString.length(); i++){
c = aString.charAt(i);
if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u') {
if (arr[c] == 0) {
System.out.print(aString.charAt(i) + " ");
arr[c]++;
}
}
}
}