charat

Check Neighbours in a String with Java

眉间皱痕 提交于 2021-01-29 09:18:37
问题 in a string like "phone" i want to know the neighbour of the character 'o' in this case 'h' and 'n' i tryed with a String Iterator but that gives me either before or after and with charAt() i will be out of range by -1 or endless loop String s = textArea.getText(); for( int i = 0; i < s.length(); i++) { char ch = s.charAt(i); char tz = s.charAt(i--); System.out.print(ch); if(ch == 'n') { System.out.print(tz); break; } } 回答1: you could try something like this. Of course, you can still do

Check Neighbours in a String with Java

自闭症网瘾萝莉.ら 提交于 2021-01-29 09:06:28
问题 in a string like "phone" i want to know the neighbour of the character 'o' in this case 'h' and 'n' i tryed with a String Iterator but that gives me either before or after and with charAt() i will be out of range by -1 or endless loop String s = textArea.getText(); for( int i = 0; i < s.length(); i++) { char ch = s.charAt(i); char tz = s.charAt(i--); System.out.print(ch); if(ch == 'n') { System.out.print(tz); break; } } 回答1: you could try something like this. Of course, you can still do

java program that takes a word and randomizes the letters and creates an anagram

早过忘川 提交于 2020-02-25 13:52:34
问题 I need to create a program that will take a word without spaces, punctuation, and all lowercase, and rearranges the letters randomly. It needs to have substrings or charAt, I cannot use an array since we have not learned them yet. It also hsa to be different everytime, really n! times I think. This is what I have so far- public static void main(String[] args) { Scanner kboard = new Scanner(System.in); System.out.println("Enter a word that is less than 11 lowercase letters and has no

java program that takes a word and randomizes the letters and creates an anagram

筅森魡賤 提交于 2020-02-25 13:51:10
问题 I need to create a program that will take a word without spaces, punctuation, and all lowercase, and rearranges the letters randomly. It needs to have substrings or charAt, I cannot use an array since we have not learned them yet. It also hsa to be different everytime, really n! times I think. This is what I have so far- public static void main(String[] args) { Scanner kboard = new Scanner(System.in); System.out.println("Enter a word that is less than 11 lowercase letters and has no

Java charAt() String index out of range: 5

点点圈 提交于 2020-02-04 08:07:12
问题 I am trying to figure out "what 5-digit number when multiplied by 4 gives you its reverse?" using this code but I get error: Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 5 at java.lang.String.charAt(String.java:658) at Digits.main(Digits.java:12) public class Digits{ public static void main(String[] args) { int n = 0; int b = 0; String number = Integer.toString(n); String backwards = Integer.toString(b); for (int x = 9999; x < 100000 ; x++ )

How to use something like the charAt for integers

六月ゝ 毕业季﹏ 提交于 2019-12-22 01:16:31
问题 I am trying to make a program that uses three digit numbers to identify items and I am trying to use something like the charAt method for integers or something. Im not too sure. Im a beginner and I apologize i just need help. Also I need a bit of help to use an if statement and relational operators. Im trying to do if the last digit in the number is less than 5 then it is {item} and if its greater than 5 then its this {item}. Something like that. Thank you so much in advance. String number;

How can I check if the char array has an empty cell so I can print 0 in it?

假装没事ソ 提交于 2019-12-21 17:07:31
问题 Code: public void placeO(int xpos, int ypos) { for(int i=0; i<3;i++) for(int j = 0;j<3;j++) { // The line below does not work. what can I use to replace this? if(position[i][j]==' ') { position[i][j]='0'; } } } 回答1: Change it to: if(position[i][j] == 0) Each char can be compared with an int. The default value is '\u0000' i.e. 0 for a char array element. And that's exactly what you meant by empty cell , I assume. To test this you can run this. class Test { public static void main(String[] args

IndexOutOfBoundsException when taking character input from the user

删除回忆录丶 提交于 2019-12-20 07:56:25
问题 In the 15th line ch = s1.charAt(0); , why ch is not getting the 0th word of s1, i.e., the operator . I've tried without using the try-catch method but then the error was regarding the exception and now no exception, no errors but the program don't ask for operator and directly after inputting the 1st and 2nd value , it shows the exception"can't do that" please post your kind replies, thanks import java.util.Scanner; class apples { public static void calcu() { try{ int a, b; String s1; char ch

string index out of bounds error in java (charAt)

不想你离开。 提交于 2019-12-18 08:55:52
问题 Quick question. I have this code in a program: input = JOptionPane.showInputDialog("Enter any word below") int i = 0; for (int j = 0; j <= input.length(); j++) { System.out.print(input.charAt(i)); System.out.print(" "); //don't ask about this. i++; } Input being user input i being integer with value of 0, as seen Running the code produces this error: Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 6 at java.lang.String.charAt(Unknown Source) at

String index value access

天涯浪子 提交于 2019-12-13 08:28:43
问题 This problem is related to String and array in Java. Say I have a String S= abcd. I have an array index[]={2}. My problem is to find out the value from String S at its index position 2 because array index[] hold 2. I have a target array which contain "EF". Now if S contain a at position 2 then it will replaced by "EF". How to access 2 position of S. Is it something like that. S[index[0]] Is this return S[2]? Example: Input: S = "abcd", indexes = [0,2], sources = ["a","cd"], targets = ["eee",