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.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I'm trying to create a program as described in the image linked below. I'm finding trouble with how to print the characters on the same line so that I can get 20 across and how to go to a new line after the first 20 characters have printed. Can you help me figure out a way to print the randomly selected characters into a 20 by 7 grid? Thank you for the help! Below is what I have so far but it's printing every new character on its own line and for gridArray[3] the forward-slash has to have two quotes otherwise it says that it's not a valid string. Does anyone know how I could solve these problems?
Link to Problem Directions
package edu.skidmore.cs106.lab09.problem14;
import java.util.Random;
public class GridGenorator {
public static void main(String[] args) {
// TODO Auto-generated method stub
String[] gridArray = new String[6];
gridArray[0] = "+";
gridArray[1] = "-";
gridArray[2] = "/";
gridArray[3] = "\"";
gridArray[4] = "|";
gridArray[5] = "_";
for (int elementx = 0; elementx < 7; elementx++) {
for(int elementy = 0; elementy<20; elementy++) {
Random rand = new Random();
int randomNum = rand.nextInt(6);
System.out.println(gridArray[randomNum]);
}
}
}
}
System.out.println prints a new line which you do not want to do every time, only after a group of 20, so try
for (int elementx = 0; elementx < 7; elementx++) {
for(int elementy = 0; elementy<20; elementy++) {
Random rand = new Random();
int randomNum = rand.nextInt(6);
System.out.print(gridArray[randomNum]);
}
System.out.println(); // now you want to print a newline
}
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 does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 years ago.
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.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
so I have to write a program for an assignment, and for that i have to accept a string, make sure it's got the right number of sentences and print the frequency of each word. I've got this almost completely right, but at the end, when I print the words (which I've stored in an array), each word is preceeded by Ljava.lang.String; #xyznumber. I have no idea why this is happening and I can't find a solution on the net. Here's my code:
import java.util.Arrays;
import java.io.*;
class frequency
{
public static void main(String args[])throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the number of sentences");
int cS = Integer.parseInt(br.readLine());
System.out.println("Enter sentences");
String s = br.readLine();
int cS1 = 0;
int cW = 0;
for(int i = 0; i < s.length(); i++)
{
char ch = s.charAt(i);
if (ch=='.'||ch=='?')
{
cW++;
cS1++;
}
if (ch==' ')
{
cW++;
}
}
if (cS1!=cS)
{
System.out.println("You have entered the wrong number of sentences. Please try again.");
}
else
{
int c = 0;
int d = 0;
String a[] = new String[cW];
System.out.println("Total Number of words: "+cW);
for (int i= 0;i<s.length();i++)
{
char ch=s.charAt(i);
if (ch==' '||ch=='?'||ch=='.')
{
a[c++]=a+s.substring(d,i);
d = i+1;
}
}
int length=0;
firstFor: for(int i=0;i<a.length;i++)
{
for(int j=0;j<i;j++)
{
if (a[j].equalsIgnoreCase(a[i]))
{
continue firstFor;
}
else
{
length++;
}
}
}
String words[] = new String[length];
int counts[] = new int[length];
int k=0;
secondFor: for (int i =0;i<a.length;i++)
{
for(int j = 0; j<i;j++)
{
if (a[j].equalsIgnoreCase(a[i]))
{
continue secondFor;
}
}
words[k]=a[i];
int counter = 0;
for (int j =0;j<a.length;j++)
{
if(a[j].equalsIgnoreCase(a[i]))
{
counter++;
}
}
counts[k]=counter;
k++;
}
for (int i=0;i<words.length;i++)
{
System.out.println(words[i]+"\n"+(counts[i]));
}
}
}
}
The problem stems from this line here:
a[c++]=a+s.substring(d,i);
Since a is a String array, what this does is assigns one of the elements in a to be equal to the String representation of the entire array, plus a substring of s. Arrays don't have a very useful String representation though, which is where the Ljava.lang.String;#xyznumber you see is coming from.
Depending on what you want the first part of a[c] to be, either use an index into the array, or convert the array to a String
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
I'm currently doing a challenge on a website to do with checking if a number is a Happy Number or not. The program is to read in a file with a list of numbers, one on each line and determine if it is a happy number. I'm having some trouble with implementing the check to see if the number is happy or not.
The happy part is fairly straight forward, if it gets to 1 it is a happy number. The problem occurs for me if the number is not happy it will never get to 1 and will stay in an infinite loop so I'm not sure on how to track that successfully
Here is the code:
package com.jconnolly.codeeval;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
public class HappyNumbers {
public static void main(String[] args) {
HappyNumbers hn = new HappyNumbers();
File file = new File(args[0]);
BufferedReader br;
String[] numbers;
try {
// Read in file
br = new BufferedReader(new FileReader(file));
String line;
// Store each line as a string in an array
while((line = br.readLine()) != null) {
numbers = line.split("\n");
for(int i = 0; i < numbers.length; i++) {
if(hn.isHappy(numbers[i])) {
System.out.println(1);
} else {
System.out.println(0);
}
}
}
System.exit(0);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.exit(-1);
}
}
// Separates digits, squares them and adds them together
public boolean isHappy(String str) {
int sum = 0;
// Holds numbers after they are squared and added together
ArrayList<Integer> happy = new ArrayList<Integer>();
// Separates the digits to be squared
while((sum != 1) && !happy.contains(sum)) {
for(int i = 0; i < str.length(); i++) {
Character c = new Character(str.charAt(i));
String character = c.toString();
int digit = Integer.parseInt(character);
sum += (digit * digit);
}
happy.add(sum);
}
happy.clear();
return sum == 1;
}
Any advice on a better implementation or correction would be greatly appreciated. It does give results but they are incorrect. Thanks
The only way I can think of to test for "never reaches 1" is to recognize when you're retesting values you've already tested, which would show that you're caught in a loop where all the numbers in that loop are unhappy. Build up a dictionary from there and use it to recognize other unhappy numbers more quickly. I believe that for this formula that's actually practical; for some, it might not be.
Try the following isHappy() method; I think recursion is the most elegant solution, but a list is needed to prevent entering a loop:
ArrayList<Integer> checked = new ArrayList<Integer>(); // used to tell if a number has already been checked
public boolean isHappy(int i) {
if(i == 1)
return true;
for(int j : checked)
if(i == j)
return false
int nextNum = ...; // generate the next number (sum the squared digits of i)
checked.add(nextNum);
return isHappy(nextNum);
}
Hope this helps!