Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
This is my code. I have 1 error. Please help me.
Try this input:
8
1 2 3 4 5 6 7 8
..........................................................................................
import java.io.*;
public class permutation
{
public static void main (String []args) throws IOException
{
BufferedReader dataIn = new BufferedReader(new InputStreamReader( System.in) );
String n="";
int parsen=0, parsenum=0;
System.out.println(n);
n = dataIn.readLine();
parsen=Integer.parseInt(n);
String num[]= new String [1000];
int visited[]=new int [1000];
int vindex[]=new int [2000];
int a=0;
for(int i=1;i<=parsen;i++){
num[i-1]=dataIn.readLine();
parsenum=Integer.parseInt(num[i-1]);
}
int t_visited=0, cycles=0, start=0, index=0;
while(t_visited<parsen)
{
for(int i=1; i<=parsen;i++)
{
if(visited[i]==0)
{
vindex[start]=i;
visited[i]=1;
t_visited++;
index=start;
break;
}
}
while(true)
{
index++;
vindex[index]=parsenum[vindex[index-1]];
if(vindex[index]==vindex[start])
break;
visited[vindex[index]]=1;
t_visited++;
vindex[++index]=0;
start=index+1;
cycles++;
}
System.out.println(cycles+vindex[0]);
for(int i=0;i<(parsen+2*cycles);i++)
{
if(vindex[i]==0)
{
System.out.println();
}else{
System.out.println(vindex[i]);
}
}
}
}
}
Thank you!
Another code:
working but wrong output. help pls...i declare num as an integer.
import java.io.*;
public class permutation
{
public static void main (String []args) throws IOException
{
BufferedReader dataIn = new BufferedReader(new InputStreamReader( System.in) );
String n="";
int parsen=0;
System.out.println(n);
n = dataIn.readLine();
parsen=Integer.parseInt(n);
int num[]= new int [1000];
int visited[]=new int [1000];
int vindex[]=new int [2000];
int a=0;
for(int i=1;i<=parsen;i++){
num[i-1] = Integer.parseInt(System.console().readLine());
}
int t_visited=0, cycles=0, start=0, index=0;
while(t_visited<parsen)
{
for(int b=1; b<=parsen;b++)
{
if(visited[b]==0)
{
vindex[start]=b;
visited[b]=1;
t_visited++;
index=start;
break;
}
}
while(true)
{
index++;
vindex[index]=num[vindex[index-1]];
if(vindex[index]==vindex[start])
break;
visited[vindex[index]]=1;
t_visited++;
vindex[++index]=0;
start=index+1;
cycles++;
}
System.out.println(cycles+vindex[0]);
for(int c=0;c<(parsen+2*cycles);c++)
{
if(vindex[c]==0)
{
System.out.println();
}else{
System.out.print(vindex[c]);
}
}
}
}
}
the output must be
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
parsenum is an int - but you use it as if it were an array: parsenum[vindex[index-1]]. That can't work.
Instead of declaring int parsenum=0; you will have to declare int parsenum[10]; where 10 will be the size of your array.
Related
This question already has answers here:
What is a StackOverflowError?
(16 answers)
Closed 2 years ago.
During execution the problem is giving stack overflow problem, but what is this!
Runtime Error (NZEC)
Exception in thread main java.lang.StackOverflowError at
sun.nio.cs.US_ASCII$Encoder.encodeArrayLoop(US_ASCII.java:198) at
sun.nio.cs.US_ASCII$Encoder.encodeLoop(US_ASCII.java:231) at
java.nio.charset.CharsetEncoder.encode(CharsetEncoder.java:579) at
sun.nio.cs.StreamEncoder.implWrite(StreamEncoder.java:271) at
sun.nio.cs.StreamEncoder.write(StreamEncoder.java:125) at
java.io.OutputStreamWriter.write(OutputStreamWriter.java:207) at
java.io.BufferedWriter.flushBuffer(BufferedWriter.java:129) at
java.io.PrintStream.write(PrintStream.java:526) at
java.io.PrintStream.print(PrintStream.java:669) at
Solution.solve(Solution.java:22) at Solution.solve(Solution.java:23)
at Solution.solve(Solution.java:23) at
Solution.solve(Solution.java:23) at Solution.solve(Solution.java:23)
at Solution.solve(Solution.java:23) at
Solution.solve(Solution.java:27) at Solution.solve(Solution.java:23)
at Solution.solve(Solution.java:27) at
Solution.solve(Solution.java:23)
import java.util.*;
public class Solution {
public static void printIncreasingNumber(int n) {
/* Your class should be named Solution.
* Don't write main() function.
* Don't read input, it is passed as function argument.
* Print output as specified in the question
*/
int k = 10;
int N = (int)Math.pow(10,n);//limit
solve((int)Math.pow(10,n-1),N);
}
static void solve(int j,int n){
if(j==n){
return;
}
if(check(j)){
System.out.print(j+" ");
solve(j+1,n);
}
else{
j = increase(j,n);
solve(j,n);
}
}
static boolean check(int k){
ArrayList<Integer> arr = new ArrayList<>();
int temp = k;
while(temp>0){
arr.add(temp%10);
temp = temp/10;
}
boolean ans = true;
for(int i=0;i<arr.size()-1;i++){
if(arr.get(i)<=arr.get(i+1)){
ans = false;
return ans;
}
}
return ans;
}
static int increase(int j,int n){
int ans = 0;
for(int i = j;i<n;i++){
ArrayList<Integer> arr1 = new ArrayList<>();
int temp = i;
while(temp>0){
arr1.add(temp%10);
temp = temp/10;
}
int count = 0;
for(int i1=0;i1<arr1.size()-1;i1++){
if(arr1.get(i1)<=arr1.get(i1+1)){
count++;
}
}
if(count==0){
ans = i;
break;
}
}
return ans;
}
}
My main function is,
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int a;
Scanner s = new Scanner(System.in);
a = s.nextInt();
Solution.printIncreasingNumber(a);
}
}
You have several problems in your code:
Instead of if(j==n){ you should use if(j>=n){
It look like function increase has bug, due to this function returns same number or 0 (it is a quite strange for increase function, isn't?). For example, if you call printIncreasingNumber(2), when j = 90 this function returns 0 and you have infinitive loop (you call solve function again and again from 0 till 90),
P.S. I recommend you use any Java debbuger for checking, then you can see all numbers step by step.
This question already has answers here:
Syntax for creating a two-dimensional array in Java
(13 answers)
2D array Null Pointer Exception error
(2 answers)
Closed 4 years ago.
Matrix.java
import java.io.*;
class Matrix {
private int q[][];
public Matrix() {
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
q[i][j] = Integer.parseInt(System.console().readLine());
}
public Matrix( int a , int b ) {
int mat[][] = new int [a][b];
for(int i=0; i<mat.length; i++) {
for(int j=0;j<mat[i].length;j++)
q[i][j] = Integer.parseInt(System.console().readLine());
}
}
public void show() {
for(int i=0; i<q.length; i++) {
for(int j=0;j<q[i].length;j++)
System.out.println(q[i][j]+" ");
}
}
}
UseMatrix.java
class UseMatrix {
public static void main(String args[]) {
Matrix m1 = new Matrix();
System.out.println("First Matrtix ");
m1.show();
Matrix m2 = new Matrix(5,4);
System.out.println("Second Matrtix ");
m2.show();
}
}
This programs shows NullPointerException error at runtime
Confused why this isn't working could use a little help, I want to the create a 2D Array of Size 3*3 through Default Constructors.
Then I want to create a Array of size 5*4 using parameterized constructors.
There are a number of problems:
First: private int q[][]; // this holds a null value, never assigned on the parameterless constructor. A possible solution to this would be to add in your constructor: q[][] = new int[a][b]; and q[i] = new int[b];// each time you enter the loop. See the code below for clarity.
Second in public Matrix() { you try to access the array contents without checking its length (for(int i=0;i<3;i++) goes from 0 to 3, regarles of the actual array size). The array is empty at the beggining so this causes another NullPointerException here q[i][j] = Integer.parseInt... (I will solve this reusing the other constructor because they want to achieve the same result)
Third there is problems reading from console(), so I changed that too. (And added a line where you ask the user for a number)
And the last change I made was a small tweak to the show method.
Result:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class App {
public static void main(String[] args) {
Matrix m1 = new Matrix();
System.out.println("First Matrtix ");
m1.show();
Matrix m2 = new Matrix(5,4);
System.out.println("Second Matrtix ");
m2.show();
}
}
class Matrix {
private int q[][];
private BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public Matrix() {
this(3, 3);
}
public Matrix(int a, int b ) {
int value;
System.out.println("Input a number to fill the new 3x3 matrix");
try {
value = Integer.parseInt(br.readLine());
} catch (IOException e) {
throw new RuntimeException("There was a problem reading the number from console", e);
}
q = new int[a][b];
for(int i=0;i<a;i++) {
q[i] = new int[b];
for(int j=0;j<b;j++) {
q[i][j] = value;
}
}
}
public void show() {
for(int i=0; i<q.length; i++) {
for(int j=0;j<q[i].length;j++)
System.out.print(q[i][j]+" ");
System.out.print("\n");
}
}
}
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 7 years ago.
Improve this question
I'm trying to create an array of numbers, starting from a big number, which is a string, and then splited to create an array.
This array has numbers from 1 to 6.
I want to create a new array of 29 items, checking the last 3 items. For instance, if the current item to add is number 4, it will check for item 3, 2, and 1. I want it to check if any of the other 3 items are the same as the current, and if is the same, it will skip to next number.
The idea, is not to have repeated numbers in a "range" of 4 items.
I also want to do another thing, and is that if the current item is number 1, it would check the last 6 items, instead of just the last 3. Only when the item is number 1. I just haven't found a way to do it.
I hope someone could help me.
Here's the current code:
import java.io.*;
import java.util.*;
public class Randomizer {
public static String numbers = "1632544362511653244213652164535316243164251654235231465312645164233462153465124361522316545326412354165412633214654531626513246124351423565431623162453625412564325134634562136145256142342365161423542563151426325146335241613645225431656324164351215263453642123416561345264215321536436451243156263542125631415624314536235124662145152436352164436512645312264315432651216345624315421563516423465213614352621543342516352614";
public static void main(String[] args) {
show("\n");
String[] nums = numbers.split("(?!^)"), finalNums = new String[29];
for (int i = 0; i < finalNums.length; i++) {
finalNums[i] = newnumber(nums, i);
}
for (String num : finalNums) {
show(num + "-");
}
}
public static String newnumber(String[] array, int start) {
String num = "x";
String act = array[start];
for (int i = start; i < array.length; i++) {
int a = start - 1, b = start - 2, c = start - 3, d = start - 4;
if (a >= 0 && b >= 0 && c >= 0 && d >= 0) {
String na = array[a], nb = array[b], nc = array[c], nd = array[d];
if (act != na && act != nb && act != nc && act != nd) {
num = act;
} else {
}
} else {
num = act;
}
}
return num;
}
public static void show(String s) {
System.out.print(s);
}
public static void show(int s) {
System.out.print(s);
}
public static void show(float s) {
System.out.print(s);
}
public static void show(double s) {
System.out.print(s);
}
}
The output is:
1-6-3-2-5-4-4-3-6-2-5-1-1-6-5-3-2-4-4-2-1-3-6-5-2-1-6-4-5
What I expect to get is something like:
1-6-3-2-5-4-6-1-2-5-3-4-6-5-1-2-5-4-6-3-2-1-2-4-5-6-3-2-1
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 8 years ago.
Improve this question
I am trying to generate non-repeating random numbers. Please help me identify the problem in my code and how to fix it.
package number;
public class name {
public static void main(String[] args)
{
int counter=0;
boolean flag=true;
int number=0;
int a[] = new int[16];
try
{
while(counter<16)
{
while(flag)
{
number = (int)(Math.random()*16);
for(int i = 0; i < 16; i++)
if(a[i]==number)
{
continue;
}
else
{
System.out.println(""+i+ "===="+ number);
a[counter]=number;
flag= false;
}
}
for(int i1=0;i1<16;i1++)
{
for(int j=0;j<16;j++)
{
if(a[i1]==a[j])
{
}
else
System.out.print(" \t "+a[i1]);
}
}
}
counter --;
}
catch(Exception e)
{
e.getStackTrace();
}
}
}
You're changing the counter variable outside the while(counter<16) loop and you should increment counter counter++ instead of decrementing it.
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 6 years ago.
Improve this question
I am trying to write a program in java which takes decimal number as an int parameter and prints to the screen the binary equivalent of that decimal number ...
In short decimal to binary conversion using only int parameters ...
Solution will be much appreciated ... i have tried some steps but i am not successful ..
Thanks
Integer.toString(input, 2);
OR
Integer.toBinaryString(input);
You might also want checkout:
Integer.toHexString
Integer.toOctalString
INPUT : 10 (decimal)
RESULT: 1010 (binary)
Hope this helps :)
Or you can write program like this (modify it):
import java.util.Scanner;
public class DecToBinary {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int broj;
System.out.print("Enter number that you want to conver to binary: ");
broj = input.nextInt();
convert(broj);
}
public static void convert(int a) {
String obrnuti = "";
while (a > 0) {
int x = a % 2;
obrnuti += x;
a /= 2;
}
System.out.println(reverse(obrnuti));
}
public static String reverse(String a) {
String novi = "";
for (int i = a.length() - 1; i >= 0; i--) {
char c = a.charAt(i);
novi += c;
}
return novi;
}
}
conversion from decimal to binary :
Integer.toBinaryString(Int_variable)
conversion from binary to decimal:
Integer.parseInt(string_binary_variable,2)
public class DecimalToBinary
{
// 5 = 101
public static void main(String[] args)
{
int d2b = dec2Bin(5);
System.out.println(d2b);
}
public static int dec2Bin(int num){
int a = num;
int binary=0;
int i=1;
while(a != 0){
binary = binary+i*(a%2);
a = a/2;
i=i*10;
}
return binary;
}
}
public class BinaryToDecimal {
public static void main(String[] args) {
String binary = "110";
System.out.println(binToDec(binary));
}
public static int binToDec(String binary){
int decimal=0;
for(int i=0;i<binary.length();i++){
decimal = 2*decimal + Integer.parseInt("" + binary.charAt(i));
}
return decimal;
}
}