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 2 years ago.
Improve this question
How would you remove a letter from a nested array in Java?
The nested array:
ArrayList arrayList = new ArrayList(
Arrays.asList("H", "e", "l", "l", "o",
new ArrayList(Arrays.asList( "w", "e", "l", "t",
new ArrayList(Arrays.asList( "l", "e", "m", "o", "n", null)),
"c", "e", "l", l", true, "l")),
3, 5, "!", "e") );
You can remove a letter from a nested array using recursion and instanceof.
static void removeLeterNestedArrayTest() {
ArrayList arrayList = new ArrayList(
Arrays.asList("H", "e", "l", "l", "o",
new ArrayList(Arrays.asList( "w", "e", "l", "t",
new ArrayList(Arrays.asList( "l", "e", "m", "o", "n", null)),
"c", "e", "l", "l", true, "l")),
3, 5, "!", "e") );
displayArrayList (arrayList);
System.out.println();
removeLetterNestedArray(arrayList, "l");
displayArrayList (arrayList);
}
static void removeLetterNestedArray(ArrayList arrayList, String s) {
for (Object obj:arrayList) {
if (obj != null && obj instanceof ArrayList) {
removeLetterNestedArray((ArrayList) obj, s);
}
}
for (int i = 0; i < arrayList.size(); i++) {
if (arrayList.get(i) != null && arrayList.get(i).toString().equals(s))
{ arrayList.remove(i); i--; }
}
}
static void displayArrayList (ArrayList arrayList) {
for(Object obj: arrayList) {
if (obj != null) {
if (obj instanceof ArrayList)
displayArrayList((ArrayList)obj);
else System.out.print(obj.toString()); }
else System.out.print("null");
}
System.out.println();
}
Related
I am trying to remove some special char from my string and replace it with other string, i write this method but it not work fine :
private String nameNormalizer(String nameString) {
char[] LETTERS_TO_REPLACE = new char[]{
'À', 'Á', 'Â', 'Ã', 'Ä', 'Æ',
'È', 'É', 'Ê', 'Ë', '&',
'Ì', 'Í', 'Î', 'Ï',
'Ò', 'Ó', 'Ô', 'Ö', 'Œ',
'Ù', 'Ú', 'Û', 'Ü',
'Ç', 'Č',
'Ñ',
'Š', 'ß',
'Ž'
};
String[] LETTERS_REPLACEMENT = new String[]{ //
"A", "A", "A", "A", "AE", "AE",
"E", "E", "E", "E", "E",
"I", "I", "I", "I",
"O", "O", "O", "OE", "OE",
"U", "U", "U", "UE",
"C", "C",
"N",
"S", "SS",
"Z"
};
char[] surnameArray = nameString.toUpperCase().toCharArray();
int i_s = 0;
int k =0;
int len_s = LETTERS_TO_REPLACE.length;
int len_name = nameString.length();
StringBuilder sb_s = new StringBuilder();
while(k < len_name) {
String b_s = ;
while (i_s < len_s){
if((surnameArray[k] == LETTERS_TO_REPLACE[i_s])) {
b_s =(LETTERS_REPLACEMENT[i_s]);
break;
}
else{
b_s = "" + surnameArray[k];
i_s++;
}
}
sb_s.append(b_s);
k++;
}
String newName = sb_s.toString();
return newName;
}
My method replace only the first occurence and after append the original char element on newName ...
How i can match all char on string and edit it?
this is my first post on this platform ,im kinda new to this java programing and also not that good at english :p
My teacher asked for a morse code translator that does morse to letters and vice versa
Here's the code i came up with:
import java.util.Scanner;
public class Morse2 {
public static void main(String[] args){
String[] letras = { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l",
"m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x",
"y", "z"};
String[] MORSE = {
".-" ,"-...","-.-.","-.." ,"." ,
"..-.","--." ,"....",".." ,".---",
"-.-" ,".-..","--" ,"-." ,"---" ,
".--.","--.-",".-." ,"..." ,"-" ,
"..-" ,"...-",".--", "-..-","-.--",
"--.."};
System.out.println("Insira uma frase em codigo morse para uma traducao para texto ou vice-versa");
Scanner in=new Scanner(System.in);
String frase =in.nextLine();
String resp="";
frase=frase.toLowerCase();
String[] paraletras=frase.split("");
String[]paraMorse=frase.split(" ");
for(int i=0;i< paraletras.length;i++){
for(int j=0;j< letras.length ;j++){
if (paraletras[i].equals(letras[j])){
resp=resp+ MORSE[j]+" ";}
}
}
for(int k=0;k<paraMorse.length;k++){
for (int l=0;l<MORSE.length;l++){
if(paraMorse[k].equals(MORSE[l])){
resp=resp+letras[l]+ " ";}}
}
System.out.print(resp);}
}
The class compiles fine but im having some issues with my output,more specifically the order of the output:
e.g My input " a b -.- c "
What i wanted ".- -... k -.-."
What i got ".- -... -.-. k"
I believe that's because i used 2 for cycles instead of 1 but i cant really tell how to do it.Would apreciate some help
Also when the user writes an impossible character like "*" im suppossed to put an "?" in that position and im also strugling on that i dont know if i should use a if else cycle or what
Please help me and thank you everybody ^^
Yes you are correct. It is because you run your loops sequentially.
You need one loop over your input, then check if one piece of input is a letter - take translation from letters array, if it is a MORSE take from Morse array.
Actaully arrays are not the best approach. It is much easier to use one Map where letter will be a key, MORSE will be a value.
then code may look like:
public static void main(String... args) {
String[] letras = { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r",
"s", "t", "u", "v", "w", "x", "y", "z" };
String[] MORSE = { ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--",
"-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.." };
System.out.println("Insira uma frase em codigo morse para uma traducao para texto ou vice-versa");
Map<String, String> decodeMap = new HashMap<String, String>();
for (int i=0; i< letras.length; i++)
{
decodeMap.put(letras[i], MORSE[i]);
}
Scanner in=new Scanner(System.in);
String frase =in.nextLine();
String resp = "";
frase = frase.toLowerCase();
String[] paraletras = frase.split(" ");
for (int i = 0; i < paraletras.length; i++) {
boolean gotValue = false;
for (Entry<String, String> decodeEntry: decodeMap.entrySet()) {
if (decodeEntry.getKey().equals(paraletras[i]))
{
// it is a letter print its MORSE
resp += decodeEntry.getValue();
gotValue = true;
break;
} else if (decodeEntry.getValue().equals(paraletras[i]))
{
// it is a MORSE - print its letter
resp += decodeEntry.getKey();
gotValue = true;
break;
}
}
if (gotValue)
{
resp += " ";
}
}
System.out.print(resp);
}
You have to evaluate every group or chars to guess if it is morse or not.
Example:
First group: 'a'
is morse -> no
is a letter -> yes -> then convert to morse
Second group: 'b'
is morse -> no
is a letter -> yes -> then convert to morse
Third group: '-.-'
is morse -> yes -> then convert to letter
This code could be more efficient, but I think it is easy to understand. I hope this help you to improve your programming skills.
import java.util.Scanner;
public class Morse2 {
public static void main(String[] args) {
String[] LETRAS = {
"a", "b", "c", "d", "e", "f",
"g", "h", "i", "j", "k", "l",
"m", "n", "o", "p", "q", "r",
"s", "t", "u", "v", "w", "x",
"y", "z" };
String[] MORSE = {
".-", "-...", "-.-.", "-..", ".", "..-.",
"--.", "....", "..", ".---", "-.-", ".-..",
"--", "-.", "---", ".--.", "--.-", ".-.",
"...", "-", "..-", "...-", ".--", "-..-",
"-.--", "--.." };
System.out.println("Insira uma frase em codigo morse para uma traducao para texto ou vice-versa");
Scanner in=new Scanner(System.in);
String frase =in.nextLine();
String resp = "";
frase = frase.trim().toLowerCase();
String[] chars = frase.split(" ");
for (int i = 0; i < chars.length; i++) {
String group = chars[i];
String newChar = null;
for (int j = 0; j < LETRAS.length; j++) {
if (LETRAS[j].equals(group)) {
newChar = MORSE[j];
break;
}
}
if (newChar == null) {
for (int j = 0; j < MORSE.length; j++) {
if (MORSE[j].equals(group)) {
newChar = LETRAS[j];
break;
}
}
}
if (newChar == null) {
System.out.println("Group not found: "+group);
continue;
}
resp += newChar + " ";
}
System.out.print(resp);
in.close();
}
}
Sorry to be such a noob,but probably is also because ive been here since 8 hours ago but why is this code giving me only the morse code to the letter 'a' a couple times?
import java.util.Scanner;
public class Morse2 {
public static void main(String[] args){
String[] abecedario = { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l",
"m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x",
"y", "z"};
String[] MORSE = {
".-" ,"-...","-.-.","-.." ,"." ,
"..-.","--." ,"....",".." ,".---",
"-.-" ,".-..","--" ,"-." ,"---" ,
".--.","--.-",".-." ,"..." ,"-" ,
"..-" ,"...-",".--", "-..-","-.--",
"--.."};
System.out.println("Insira uma frase em codigo morse para uma traducao para texto ou vice-versa");
Scanner in=new Scanner(System.in);
String frase =in.nextLine();
String resp="";
frase=frase.toLowerCase();
String[] palavra=frase.split(" ");
for(int i=0;i< palavra.length;i++){
String letra=palavra[i];
String novochar="";
for (int j=0;j<abecedario.length;j++){
if (abecedario[j].equals(letra));
novochar=MORSE[j];
break;
}
if(novochar==""){
for (int j=0;j<MORSE.length;j++){
if (MORSE[j].equals(letra));
novochar=abecedario[j];
break;
}
}
if(novochar==""){
novochar="?";
continue;
}
resp=resp+novochar+" ";
}
System.out.println(resp);
}
}
I want to create method thath generate numbering for Database record in the future.
My rules like this :
[start] - [end]
0001-
9999
A001-
A999
AA01-
AA99
AB01-
AB99
AC01-
AC99
etc...
......
......
ZZZZ
Its look similar using Excel column numbering.
How to create like that, using Java?
Here is my code :
But i confuse in how to check if in the last number like 9999 , A999 etc
public static void main(String [] args) {
String lastSchCode = "9999";
System.out.println(generateSchCode(lastSchCode));
}
public static String generateNextNum(String number) {
int nextNum = Integer.parseInt(number);
String padNextNum = lPadZero(nextNum+1, 4);
return padNextNum;
}
public static String generateSchCode(String lastSchCode) {
String nextSchCode = null;
String [] alphabets = {"A", "B", "C", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
int counter = 0;
for (int i = 0; i < lastSchCode.length(); i++) {
if (lastSchCode.charAt(i) == '9') {
counter++;
}
}
if (generateNextNum(lastSchCode).equals("10000")) {
int num = 9999;
} else {
}
return nextSchCode;
}
Please help. Thank you.
Have a look at this snippet. You have to convert the number based on radix 36.
int[] ints = { 0, 1, 10, 35, 36, 46, 36*36-1, 36*36*36-1, 36*36*36*36-1};
for (int i : ints) {
System.out.printf("int: %7d string: %4s%n", i, Integer.toString(i, 36));
}
output
int: 0 string: 0
int: 1 string: 1
int: 10 string: a
int: 35 string: z
int: 36 string: 10
int: 46 string: 1a
int: 1295 string: zz
int: 46655 string: zzz
int: 1679615 string: zzzz
public static void main(String [] args) {
String lastSchCode = "9999";
System.out.println(generateSchCode(lastSchCode));
}
public static String generateNextNum(String number) {
int nextNum = Integer.parseInt(number);
String padNextNum = lPadZero(nextNum+1, 4);
return padNextNum;
}
public static String generateSchCode(String lastSchCode) {
String nextSchCode = null;
String [] alphabets = {"0","1","2","3","4","5","6","7","8","9","A", "B", "C", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
List<String> alphabetsAsList = Arrays.asList(alphabets);
int counter = 0;
for (int i = lastSchCode.length(); i > 0; --i) {
if(lastSchCode.charAt(i) == '9'){
incrementWith9(lastSchCode,i);
}
else{
String s = lastSchCode.substring(index,index+1);
String incrementedString = alphabetsAsList.get(alphabetsAsList.indexOf(s) + 1);
char[] charArr = lastSchCode.toCharArray();
charArr[index] = incrementedString.charAt(0);
nextSchCode = charArr.toString();
}
}
return nextSchCode ;
}
public String incrementWith9(String input, int index){
char[] ca = input.toCharArray();
ca[index] = '0';
if( index != 0 && input.charAt(index -1 ) == '9'){
incrementWith9(ca.toString(),index -1);
}
elseif(index == 0 ){
return "A000";
}
else{
String [] alphabets = {"0","1","2","3","4","5","6","7","8","9","A", "B", "C", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
List<String> alphabetsAsList = Arrays.asList(alphabets);
String s = input.substring(i,i+1);
String incrementedString = alphabetsAsList.get(alphabetsAsList.indexOf(s) + 1);
char[] charArr = input.toCharArray();
charArr[i] = incrementedString.charAt(0);
nextSchCode = charArr.toString();
}
return nextSchCode;
}
This should work. Or atleast you get the idea right.. ;)
Basically I've made a array for each letter of the alphabet and then added it to a array of JButtons. That works fine however I've now attempted to add on a action listener which I managed to succesfully get working.
BUT, it works as I have 26 if statements to check if each button is pressed hence why I've tried adding a for loop.
Now whenether I press the button it prints out a load of garbage regarding the JbUTTON properties. Where could I be going wrong?
String[] letters = { "Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P", "A", "S", "D", "F", "G", "H", "J", "K", "L", "Z", "X", "C", "V", "B", "N", "M" };
layout.add(scrollBar);
for (int i=0; i < 26; i++)
{
if (i==25)
{
layout.add(spacebar);
spacebar.setPreferredSize(new Dimension(310,50));
spacebar.setBackground(Color.black);
spacebar.setForeground(Color.white);
spacebar.addActionListener(new action());
}
AlphaButton[i] = new JButton(letters[i]);
AlphaButton[i].setPreferredSize(new Dimension(50,50));
AlphaButton[i].setBackground(Color.black);
AlphaButton[i].setForeground(Color.white);
layout.add(AlphaButton[i]);
AlphaButton[i].addActionListener(new action());
}
class action implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String V = screenArea.getText();
for (int i=0; i < 26; i++)
{
if( e.getSource() == AlphaButton[i] )
{
screenArea.setText(V + AlphaButton[i]);
}
}
}
}
If you need to return the letter that was pressed:
screenArea.setText(V + AlphaButton[i].getText);
You need to override the AlphaButton toString method
// your class
public class AlphaButton {
#Override
public String toString() {
return "This is an AlphaButton"; // or whatever output you want it to say
}
}
Why not simply do:
public void actionPerformed(ActionEvent e) {
String V = screenArea.getText();
screenArea.setText(V + e.getActionCommand());
}
I'm trying to check if my input is a consonant. However it tells me at the line below that they are incompatible types (boolean vs String)
if (medeklinkerGeraden = medeklinkers [r]) {
^
public String medeklinkerRaden () {
String medeklinkerGeraden = "";
boolean bevatMedeklinker = false;
System.out.println("U mag een medeklinker gokken!");
medeklinkerGeraden = Input.readString();
String [] medeklinkers = {"b", "c", "d", "f", "g", "h", "j", "k", "l", "m", "n", "p", "q", "r", "s", "t", "v", "w", "x", "z"};
do {
for (int r = 0; r < medeklinkers.length; r++)
if (medeklinkerGeraden = medeklinkers [r]) {
bevatMedeklinker = true;
}
}
while (! bevatMedeklinker);
return medeklinkerGeraden;
}
Refactor like this:
if (medeklinkers[r].equals(medeklinkerGeraden)) {
bevatMedeklinker = true;
}
You must use == instead of =
if (medeklinkerGeraden == medeklinkers [r])