This question already has answers here:
Remove all non alphabetic characters from a String array in java
(9 answers)
Removing all characters but letters in a string
(4 answers)
Closed 4 years ago.
How do you remove all certain elements or characters from an ArrayList?
Let's say I have a scanner object like
Scanner keyboard = new Scanner(System.in);
String userInput = keyboard.nextLine();
And the user inputs something like
Bubblesort.=
If I wanted to save the characters of the String variable to an ArrayList, I'd use a char ArrayList and then load the characters in using a for loop like so
ArrayList<Character> stringArrayList = new ArrayList<Character>();
for (int i = 0; i < userInput.length(); i++) {
stringArrayList.add(userInput.charAt(i));
}
My question is, how do I remove all the characters which aren't letters. Like '.', and '=' so that the ArrayList contains only the word "Bubblesort"?
Related
This question already has answers here:
how to convert an integer string separated by space into an array in JAVA
(5 answers)
Closed 2 years ago.
I am trying to read user input from a scanner into an integer array, I currently have this:
int [] arr1 = {Integer.parseInt(sc.nextLine().split(" "))};
But i am given the error
String[] cannot be converted to String
Any help would be much appreciated :)
sc.nextLine().split(" ") //This returns String[]
int a = Integer.parseInt("") //Integer.parseInt requires one String param.
Try below code:
String input = sc.nextLine();
String[] inputs = input.split(" ");
List<Integer> ints = Arrays.stream(inputs).
map(Integer::parseInt).collect(Collectors.toList());
Check the document:
API: Integer.parseInt
String.split()
This question already has answers here:
Java Class that implements Map and keeps insertion order?
(8 answers)
Closed 3 years ago.
public static HashMap<Character,Double> symbols = new HashMap<>();
public static void getSymbol() {
char ch = ' ';
double pro;
while (ch!='`') {
Scanner scanner = new Scanner(System.in);
System.out.println("Input the character");
ch = scanner.next().charAt(0);
if (ch == '`')break;
Scanner num = new Scanner(System.in);
pro = num.nextDouble();
symbols.put(ch, pro);
}
System.out.println(symbols);
}
}
when I input a,b,c,q in this particular order
the output of HashMap will be : a,q,b,c
HashMap does not maintain insertion order of the elements.
Use LinkedHashMap instead of HashMap implementation if you want to maintain insertion order
refer: https://docs.oracle.com/javase/8/docs/api/java/util/LinkedHashMap.html
This question already has answers here:
How to check if a String contains only ASCII?
(14 answers)
Closed 3 years ago.
I have user input
private Scanner inn = new Scanner(System.in);
String input = inn.nextLine().toLowerCase();
and i need to throw IllegalCharacterException(own exception, created this class already) for not desired input (all numerals and symbols and maybe even other languages)I need only english letters. How can i do that? Thank you.
You can use String.matches(String regex) with regular expressions. E.g.
private Scanner inn = new Scanner(System.in);
String input = inn.nextLine().toLowerCase();
if(input.matches("[a-z]*")) {
// Do some stuff
}
The Regular expression [a-z]* matches any character from a to z (lowercase), without any numbers, symbols or spaces.
This question already has answers here:
Why does String.replace not work? [duplicate]
(3 answers)
Closed 4 years ago.
Whenever I run the java code below it compiles but the line that include the replace method seems to be skipped, such so that the inputted string and the output (newMessage) are the same. Why? variable C and variable D are chars...
import java.util.Scanner;
public class javaencrypt
{
public static void main(String[] args)
{
// define and instantiate Scanner object
Scanner input = new Scanner(System.in);
//prompt user to enter a string
System.out.println("Please enter a string: ");
String message = input.nextLine();
String newMessage = message;
char c=' '; // the character at even locations
char d=' '; // new character
// go throughout the entire string, and replace characters at even positions by the character shifted by 5.
// access the even characters with a for loop starting at 0, step 2, and ending at length()-1
// for( initial value; maximum value; step)
for(int k=0; k<message.length(); k=k+2)
{
c=message.charAt(k);
d=(char)(c+5);
/*
there will always be characters available, because keyboard is mapped on ASCII which is in the beginning of UNICODE
*/
newMessage.replace(c,d);
}
System.out.println("Message replacement is: " + newMessage);
}
}
In Java, Strings are immutable.
An immutable class is simply a class whose instances cannot be modified. All information in an instance is initialized when the instance is created and the information can not be modified.
When you call newMessage.replace(c, d); this does not update newMessage, but rather creates a new String with all chars c replaced with d instead. If you want newMessage to change to include the replacing of c to d, then you need to reassign the variable. This would look like newMessage = newMessage.replace(c, d);
This question already has answers here:
What's the simplest way to print a Java array?
(37 answers)
Closed 7 years ago.
In this program I am trying to return the user input backwards
e.g. Laura would be returned as aruaL, but I am getting [C#11d72ca as my output..
String input = JOptionPane.showInputDialog("Enter name");
Scanner scanner = new Scanner(input);
String name = scanner.next();
scanner.close();
char[] backwards = new char[name.length()];
for(int i = 0; i<name.length(); i++)
{
backwards[i] = name.charAt(name.length()-1-i);
}
JOptionPane.showMessageDialog(null, backwards.toString());
}
There is an easier way to reverse a string:
String reverse = new StringBuilder(name).reverse().toString()
If you don't want to use this method, in order to covert a char[] to a string you would need to do something like:
String reverse = new String(backwards);
Calling toString on an array will call the toString method of the Object. This will return you the hashCode which is what you are getting