Getting an integer to go to a different line [closed] - java

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 4 years ago.
Improve this question
This is my code for reversing the digits of an integer,
but the result gives each digit reversed but in a new line,
like if you give 341 to it , it gives you:
1
4
3
But I want to make it all come to the same line , is there a way to do it without changing the main code?
public static void main(String[] args)
{
Scanner scan=new Scanner(System.in);
System.out.println("Enter your number to reverse it ...");
int a=scan.nextInt();
while(a>0) {
i=a%10;
a/=10;
l++;
System.out.println(i);
}
System.out.println("Number of digits: "+l);
}

You could use print instead of println:
System.out.print(i);

Answer :
public static void main(String[] args)
{
Scanner scan=new Scanner(System.in);
System.out.println("Enter your number to reverse it ...");
int a=scan.nextInt();
while(a>0) {
i=a%10;
a/=10;
l++;
System.out.print(i);
}
System.out.println("Number of digits: "+l);
}

I think your code has few errors. It should be
int i = 0;
while(a>0) {
i=a%10;
a/=10;
System.out.print(i+" ");
i++;
}

In your code you are just picking digits and printing it in reversed order instead of really reversing it.
public static void main(String[] args)
{
int i,l=0,num=0;
Scanner scan=new Scanner(System.in);
System.out.println("Enter your number to reverse it ...");
int a=scan.nextInt();
while(a>0)
{
i=a%10;
a/=10;
num=(num*10)+i;
l++;
}
System.out.println("Number of digits: "+l);
System.out.println("Reversed Number: "+num);
}
Try this code instead.....

Related

Trying to print out some sequence of numbers [closed]

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 1 year ago.
Improve this question
I want to print out a sequence of numbers using this formula
Enter a number
if number is even divide number by 2.
But if number is odd multiply number by 3 and add 1
continue doing this until number becomes 1
sample input=3
Sample output=10 5 16 8 4 2
this is what I tried but still not getting it
package victor;
import java.util.Scanner;
public class proj {
public static void main(String[] args) {
Scanner put=new Scanner(System.in);
int temp=0;
boolean notOne=true;
System.out.println("input::: ");
int num=put.nextInt();
while(temp!=1){
if (num%2==0){
temp=num;
System.out.println(temp/2);
break ;
}
else {
temp=num;
System.out.println(temp*3+1);
break;
}
}
if(temp!=1){
notOne=false;
}
}
}
It's not working because you keep re-assigining the variable temp to the initially scanned num.
You keep checking if the initially scanned num is odd or even, when you should check if temp is odd or even.
You also break out of the loop for no reason.
And finally, you're not saving the result of the operations, you're only printing out the result.
Try to understand the points I mentioned above by noticing the differences between your code and the following:
while(temp!=1){
if (temp%2==0){
temp = temp/2;
}
else {
temp = temp*3+1;
}
System.out.println(temp);
}
You are not updating the value of temp. You are just printing it. Take the following statement
if (num%2==0){
temp=num;
System.out.println(temp/2);
break ;
}
Here you are setting temp to num and just printing temp/2 and never setting a value.
I wrote my version of it which is a bit more simpler. I hope this will help you. You can create a string to get a better output of course.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter a number:");
int number = scan.nextInt();
while (number != 1) {
number = number % 2 == 0 ? number / 2 : ((number * 3) + 1);
System.out.println("Number became " + number);
}
}
}
Try this:
public class Main
{
public static void main(String[] args) throws Exception
{
System.out.println("Starting...");
//Lets start the program, first we need
//the Scanner class to access to the input
Scanner stdin = new Scanner(System.in);
System.out.print("Type a num: ");
//I dont use: nextInt() because when asking for another input, will scan only
//the rest of the line (Maybe just \n - line break )
int num = Integer.parseInt(stdin.nextLine());
//Optional
int loops = 0;
while(num!=1){
//Pair, so num/2
if ( num %2 == 0){
num/=2;
}
else{
//num*3 +1
num=num*3 +1;
//Note that:
//1 + num*3
//Doesnt alter the result
}
System.out.println("num: "+num);
loops++;
}
System.out.println("total loops: "+loops);
}
}

Is the function wrong? [closed]

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 1 year ago.
Improve this question
Scanner sc= new Scanner(System.in);
int n = sc.nextInt();
while(n>0) {
int count=0;
n= n/10;
count++;
}
System.out.println(count);
In your code, you have declared the variable count in the while loop block and so it is a local variable and the scope of the variable is limited till while loop block.
If you want the program to execute successfully, you should declare the variable inside the main method block but outside the while loop block.
Like this
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int count = 0;
while(n > 0){
n = n/10;
count++;
}
System.out.println("Count = "+count);
}
Scope of a variable is the part of the program where the variable is accessible.
Refer this article for more details Scope in Java
Also to correct the code,
Scanner sc= new Scanner(System.in);
int n = sc.nextInt();
int count = 0;
while(n>0) {
count=0;
n= n/10;
count++;
}
System.out.println(count);

While statement isn't excuting inside if statement? [closed]

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 1 year ago.
Improve this question
I am trying to make it print out a range of specified numbers. So if they select option 1 and put in 1 and 15, I want it to print out 1 to 15. Once it gets to the while statement though it just prints nothing.
import java.util.Scanner;
public class Lab4 {
public static void main(String[] args) {
Scanner in = new Scanner (System.in);
System.out.print("Please choose your choice from the following menu");
System.out.print("\n1) Print through all integer numbers between two given integers");
System.out.print("\n2) Display a right triangular pattern of stars");
System.out.println("\n3) Quit");
int userInput = in.nextInt();
if (userInput == 1) {
System.out.print("Enter the start number: ");
int firstInteger = in.nextInt();
System.out.print("Enter the second number: ");
int secondInteger = in.nextInt();
while (firstInteger < secondInteger);
System.out.print(firstInteger);
firstInteger++;
} else if (userInput == 2) {
System.out.print("Enter the height: ");
int triangleHeight = in.nextInt();
} else if (userInput == 3);{
System.exit(userInput);
}
in.close();
}
}
You should change :
while (firstInteger < secondInteger);
System.out.print(firstInteger);
firstInteger++;
to
while (firstInteger < secondInteger) {
System.out.print(firstInteger);
firstInteger++;
}
while (firstInteger < secondInteger);
This will be treated as two executable lines of instructions,
which is similar like
while (firstInteger < secondInteger)
;
Try to remove the ; after while statement

MergeSort-My first DS algo in java [closed]

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 9 months ago.
Improve this question
package allSortings;
import java.util.Scanner;
public class MergeSort {
public static int A[],B[];
void mergeArray(int A[],int first,int mid,int last){
int i=first,j;
j=mid;
while(i<=mid && j<=last)
{
if(A[i]<A[j])
B[first++]=A[i++];
else B[first++]=A[j++];
}
while(i<=mid)
B[first++]=A[i++];
while(j<=last)
B[first++]=A[j++];
}
void copyArray(int A[],int last,int B[])
{
int i=0;
while(i<last)
{
A[i]=B[i];i++;
}
}
void splitArray(int A[],int first,int last)
{
if(first<last)
{
int mid=first+last/2;
System.out.println("first:"+first);
splitArray(A,first,mid);
splitArray(A,mid+1,last);
//mergeArray(A,first,mid,last);
//copyArray(A,last,B);
}
}
public static void main(String args[])
{
int n;
A=new int[100];
B=new int[100];
System.out.println("Enter the no. of elements in the Array:"+"\n");
Scanner input;
input=new Scanner(System.in);
n=input.nextInt();
MergeSort m1=new MergeSort();
for(int i=0;i<n;i++)
A[i]=input.nextInt();
System.out.println("\nThe Original array is:");
for(int i=0;i<n;i++)
System.out.format("%d"+" ",A[i]);
m1.splitArray(A,0,n-1);
System.out.println("\nThe Sorted array is:");
for(int i=0;i<n;i++)
System.out.format("%d"+" ",A[i]);
}
}
I keep getting at allSortings.MergeSort.splitArray(MergeSort.java:34).Guys any clue (I am new to Java, so I dont know to use a debugger)?
The value of "first" variable always gets 2 and then does not change.
You're only dividing one summand by 2, but you should divide the sum by 2 instead. Replace
int mid=first+last/2;
with
int mid=(first+last)/2;

I am getting illegal start of expression [closed]

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
import java.util.Scanner;
//trying to print pyramid shape
class try
{
public static void main(String[] args)
{
Scanner in=new Scanner(System.in);
System.out.println("Enter the number of lines:");
int n=in.nextInt();
for(int i=n;i>0;i--)
{
for(int j=1;j<=i;j++)
{
System.out.print(" ");
}
j=n-(i-1);
for(int k=0;k<j;k++)
{
System.out.print(j+" ");
}
System.out.println();
}
}
}
But iam not able to print solve.
try is a reserved keyword in Java. You cannot use it as a class name. Simply rename your class - i.e. to MyTry
Reference: Java Language Keywords
try is a key word you have to change your class name first. (Use MyClass)
How you can do this, You have to define and initialize the variable before use them.
for(int j=1;j<=i;j++){
System.out.print(" "); // j visible inside for loop only
}
j=n-(i-1); // access j from outside for-loop is not possible
You can make this mistake correct by your own. Don't you use an IDE for coding?
Here is the solution of your problem.
import java.util.Scanner;
//trying to print pyramid shape
class Try
{
public static void main(String[] args)
{
Scanner in=new Scanner(System.in);
System.out.println("Enter the number of lines:");
int n=in.nextInt();
int i,j;
for(i=n;i>0;i--)
{
for( j=1;j<=i;j++)
{
System.out.print(" ");
}
j=n-(i-1);
for(int k=0;k<j;k++)
{
System.out.print(j+" ");
}
System.out.println();
}
}
}

Categories