d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > How To Do This?
Add Reply New Topic New Poll
Member
Posts: 31,313
Joined: Mar 25 2009
Gold: 0.00
Sep 16 2020 02:57pm
Code
package com.ferfykins.exercises;

import java.util.Scanner;

public class OtherWay {

public static void main(String[] args) {
System.out.println("Enter a string of numbers mixed with letters, we will remove the letters!");
Scanner scan = new Scanner(System.in);


char[] mixedCharacters = scan.nextLine().toCharArray();


for (int i = 0; i < mixedCharacters.length; i++) {

if(mixedCharacters[i] == "[a-zA-Z]")

}


}
}















basically i'm trying to adjust whatever is in the character array...I wanna remove anything that's not a number
Member
Posts: 1,304
Joined: Jul 8 2012
Gold: 0.00
Sep 16 2020 03:08pm
Code
import java.util.Scanner;
public class hello{

public static void main(String[] args) {
System.out.println("Enter a string of numbers mixed with letters, we will remove the letters!");
Scanner scan = new Scanner(System.in);


char[] mixedCharacters = scan.nextLine().toCharArray();

char[] nArray = new char[mixedCharacters.length];
int z = 0;
for (int i = 0; i < mixedCharacters.length; i++) {

if(!Character.isLetter(mixedCharacters[i]))
{
nArray[z] = mixedCharacters[i];
z++;
}

}

for (int i = 0; i < nArray.length; i++)
{
System.out.println(nArray[i]);
}


}
}



Something like this?

Input: 3ac
Output: 3

Input: 4abby2bill1
Output:
4
2
1

Might want to use Character.isDigit(x) I was just going with your original plan, and it will avoid special characters: $ ! etc..

This post was edited by Kippet on Sep 16 2020 03:17pm
Member
Posts: 31,313
Joined: Mar 25 2009
Gold: 0.00
Sep 16 2020 04:35pm
Quote (Kippet @ Sep 16 2020 04:08pm)
Code
import java.util.Scanner;
public class hello{

public static void main(String[] args) {
System.out.println("Enter a string of numbers mixed with letters, we will remove the letters!");
Scanner scan = new Scanner(System.in);


char[] mixedCharacters = scan.nextLine().toCharArray();

char[] nArray = new char[mixedCharacters.length];
int z = 0;
for (int i = 0; i < mixedCharacters.length; i++) {

if(!Character.isLetter(mixedCharacters[i]))
{
nArray[z] = mixedCharacters[i];
z++;
}

}

for (int i = 0; i < nArray.length; i++)
{
System.out.println(nArray[i]);
}


}
}



Something like this?

Input: 3ac
Output: 3

Input: 4abby2bill1
Output:
4
2
1

Might want to use Character.isDigit(x) I was just going with your original plan, and it will avoid special characters: $ ! etc..


that's perfectly what i was looking to do, thanks!!
Member
Posts: 1,304
Joined: Jul 8 2012
Gold: 0.00
Sep 16 2020 05:05pm
No problemo, the nArray is going to be longer than it needs to be, so maybe instead for printing use:

Code
for (int i = 0; i < z; i++)
{
System.out.println(nArray[i]);

}
Member
Posts: 31,313
Joined: Mar 25 2009
Gold: 0.00
Sep 21 2020 03:52pm
Quote (Kippet @ Sep 16 2020 06:05pm)
No problemo, the nArray is going to be longer than it needs to be, so maybe instead for printing use:

Code
for (int i = 0; i < z; i++)
{
System.out.println(nArray[i]);

}


is that bad practice leaving nArray with extra slots? if so, how would i adjust it? Thanks again :)
Member
Posts: 1,304
Joined: Jul 8 2012
Gold: 0.00
Sep 21 2020 06:35pm
Quote (ferf @ Sep 21 2020 04:52pm)
is that bad practice leaving nArray with extra slots? if so, how would i adjust it? Thanks again :)


Perhaps, but that's a product of using an array since the size has to be declared and it can't be changed after. You could use any mutable data structure for the second array, one that would allow an .append() function. (For example: a list of chars).

But at a lower level thought process the trade offs are:

Would you rather have a few extra bytes of a primitive data type or would you rather use a larger object to avoid that?

Furthermore, if you include that part of code in a function, once the function reaches its end the array will be removed from memory. So you wouldn't have to worry about that amount of unused array-space taking up space in memory.



This post was edited by Kippet on Sep 21 2020 06:42pm
Member
Posts: 31,313
Joined: Mar 25 2009
Gold: 0.00
Sep 23 2020 04:22pm
Quote (Kippet @ Sep 21 2020 07:35pm)
Perhaps, but that's a product of using an array since the size has to be declared and it can't be changed after. You could use any mutable data structure for the second array, one that would allow an .append() function. (For example: a list of chars).

But at a lower level thought process the trade offs are:

Would you rather have a few extra bytes of a primitive data type or would you rather use a larger object to avoid that?

Furthermore, if you include that part of code in a function, once the function reaches its end the array will be removed from memory. So you wouldn't have to worry about that amount of unused array-space taking up space in memory.



Gotchya, thanks man :)
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll