Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
Arrays are not taking in values and setting the values to 0 only by default. I used debugger and saw that nothing changes when i feed in values. I have never experienced this. I am using Java SE13 in Code OSS. OS Arch Linux
import java.util.Arrays;
import java.util.Scanner;
public class A_Little_Elephant_and_Rozdil {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[] arr = new int[n];
// int[] brr = new int[n];
for(int i = 0;i<arr.length;i++){
arr[i] = in.nextInt();
}
// for(int i = 0;i<arr.length;i++){
// arr[i] = brr[i];
// }
// Arrays.sort(arr);
// if(arr[0]==arr[1])
// {
// System.out.println("Still Rozdil");
// in.close();
// return;
// }
// else{
// for(int i = 0;i<n;i++){
// if(arr[0] == brr[i])
// {
// System.out.println(i+1);
// in.close();
// return;
// }
// }
// }
in.close();
}
}
Most probable cause of your problem is the commented for loop in which you are doing
arr[i] = brr[i];
brr[i] will be zero in each iteration of the loop because you never set any values in brr array. So after second loop executes, all the values inside arr will be overwritten with zeroes.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I'm trying to make a ReverseString program. It's only returning one word only. I would like a full sentence.
import java.util.Scanner;
public class ReverseString {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String word = input.next();
String reverse = "";
for (int i = word.length() - 1; i >= 0; i--)
reverse += word.charAt(i);
System.out.println(reverse);
}
}
You can use the reverse method of the StringBuilder/StringBuffer class.
Something like :
String reversedString = new StringBuilder(input.nextLine()).reverse().toString();
Or if you want a more low-level approach you could use a Stack push every character in it and pop it one by one to get the reversal of it.
public String reverseString(String s) {
Stack<Character> stack = new Stack<>();
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < s.length(); i++) {
stack.push(s.charAt(i));
}
while (!stack.empty()) {
stringBuilder.append(stack.pop());
}
return stringBuilder.toString();
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
It is an android app.
Text is stored in an array. It should change serial wise
Here is what I had done before.
String name = "";
String names[] = {"A", "B", "C", "D"};
int counter = 0;
name = names[counter];
counter++;
if(counter >= 3)
{
counter = 0;
}
return name;
I was doing something like that before. I know it totally incorrect . But something like this I wanted to do.
This may help solve your problem.
import java.util.Scanner;
public class Main {
static int currentIndex = 0;
static String[] words = {"word1", "word2", "word3"};
public static void main(String[] args) {
while(true) {
Scanner keyboard = new Scanner(System.in);
String input = keyboard.nextLine();
if(input != null){
System.out.println(words[currentIndex++]);
if(currentIndex == words.length){
currentIndex = 0;
}
}
}
}
}
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 7 years ago.
Improve this question
I have a class like below
public class EXOR{
public static void conv(){
String [] Parray={"243f6a88","85a308d3","13198a2e","03707344","a4093822","299f31d0","082efa98",
"ec4e6c89","452821e6", "38d01377", "be5466cf","34e90c6c","c0ac29b7","c97c50dd","3f84d5b5","b5470917","9216d5d9","8979fb1b"};
String binAddr[]=new String[18];
for (int i=0;i<18;i++)
{
int x[]=new int[18];
binAddr[i]= Integer.toBinaryString(Integer.parseInt(Parray[i],16));
System.out.println("binernya : " +binAddr[i]);
}
}
public static void main(String[] args){
new EXOR().conv();
}
}
and I want to convert that array to binary array format.
I want to get output like below
for example
00100100001111110110101010001000
10000111101000110000100011010011
................................
How to fix this problem?
I suppose while executing your code you must've got a number point exception. This occurs when the Hexadecimal string is out of the range of Integer.
You can use:
binAddr[i]= (new BigInteger(Parray[i],16)).toString(2);
instead of
binAddr[i]= Integer.toBinaryString(Integer.parseInt(Parray[i],16));
This will solve your problem for quick reference
Big Integer Documentation
Code:
public class EXOR {
public static void conv(){
String [] Parray={"243f6a88","85a308d3","13198a2e","03707344","a4093822","299f31d0","082efa98",
"ec4e6c89","452821e6", "38d01377", "be5466cf","34e90c6c","c0ac29b7","c97c50dd","3f84d5b5","b5470917","9216d5d9","8979fb1b"};
String [] binAddr = new String[Parray.length];
for (int i = 0; i < binAddr.length; i++)
{
int strLen = Parray[i].length();
binAddr[i] = "";
for(int j = 0; j < strLen; j++) {
String temp = Integer.toBinaryString(
Integer.parseInt(String.valueOf(
Parray[i].charAt(j)), 16));
// Pad with leading zeroes
for(int k = 0; k < (4 - temp.length()); k++) {
binAddr[i] += "0";
}
binAddr[i] += temp;
}
System.out.println("Original: " + Parray[i]);
System.out.println("Binary: " + binAddr[i]);
}
}
public static void main(String[] args){
conv();
}
}
First few lines of Output:
Original: 243f6a88
Binary: 00100100001111110110101010001000
Original: 85a308d3
Binary: 10000101101000110000100011010011
We have Integer.MAX_VALUE = 2147483647
But, the 2nd item "85A308D3" = 2242054355. It exceed the capability of an Integer.
So, you use Integer.parseInt(85A308D3) will cause java.lang.NumberFormatException.
To fix it, change your code to use Long instead of Integer
binAddr[i] = Long.toBinaryString(Long.parseLong(Parray[i], 16));
Hope this help!
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 8 years ago.
Improve this question
//This program determines if the input string is a palindrome
import java.util.*;//importing all the methods from java.util class
import static java.lang.System.out;
public class Pallindrome {
public static void main(String[] args) {
#SuppressWarnings("resource")
Scanner input= new Scanner(System.in);
String pallindrome;
out.println("Enter a string: ");
pallindrome= input.nextLine();
ArrayList<String> pall= new ArrayList<String>();
buildAL(pall, pallindrome);
display(pall);
if(isPalendrome(pall))
out.println(pallindrome + " is a pallindrome");
else
out.println(pallindrome + " is not a pallindrome");
}
static void display(ArrayList<String> arr1){ //this method is for displaying the array list
for(int i=0; i<arr1.size();i++)
out.print(arr1.get(i));
out.println();
}
static void buildAL(ArrayList<String> arr2, String word){ //this is for building the array with the entered word
for(int i=0;i<arr2.size();i++)
arr2.add(word.charAt(i)+ "");
}
static Boolean isPalendrome(ArrayList<String> arr3){ //it will test if the word is pallindrome
ArrayList<String> rarr3= new ArrayList<String>();
rarr3.addAll(arr3);
Collections.reverse(rarr3);
for(int i=0;i<rarr3.size();i++)
if(!(rarr3.get(i).equals(arr3.get(i))))
return false;
return true;
}
}
When I run this code it shows the same output. please point out the error.
It's unclear what the problem is but your for loop doesnt over the letters in word as the termination condition is based on the empty List size passed to the buildAL method. Replace
for (int i = 0; i < arr2.size(); i++)
with
for (int i = 0; i < word.length(); i++) {
Below
static void buildAL(ArrayList<String> arr2, String word){
for(int i=0;i<arr2.size();i++)
arr2.add(word.charAt(i)+ "");
}
arr2.size() is 0 as you don't have any element in the list. Either add the word to the list or do word.length() in for loop.
Also, if I have to do the same thing I would do something like -
After reading the string from the scanner, Simply do
StringBuilder sb = new StringBuilder("your String");
if ("yourString".equals(sb.reverse().toString())) {
//or you can use equalsIgnoreCase also if that fits your requirement
//its a palindrome
} //Otherwise, not.