How to print a new line after each printing call - java

I want to get as many stars i give in the function with a line break. But I am not able to get them with line break.
public class prac11 {
public static void main(String[] args) {
//printStars(1);
printStars(2);
printStars(3);
}
public static void printStars(int x) {
int i=1;
while(i<=x) {
System.out.print("*");
i++;
}
}
}

You have to add a println statement to put the linebreak after your loop:
public class prac11 {
public static void main(String[] args) {
printStars(5);
printStars(3);
printStars(9);
}
public static void printStars(int x) {
int i=1;
while(i<=x) {
System.out.print("*");
i++;
}
System.out.println(); // this will produce a linebreak
}
}
Output:
*****
***
*********

Add System.out.println() after the while loop, to get the linebreak.

Related

count the "IfStmt" statement in java parser

I am using javaparser to parse a java file , when I count the "If" statement it display the output as an incremental number such that Eg: if there are 3 "if" statements then it displays as
[
1
2
3
]
I want to get only the total number of "IF" statements. eg:[ 3].
I don't want to get all the incrementing count
This is my source code.
package org.javaparser.examples.chapter2;
public class VoidVisitorComplete {
private static final String FILE_PATH = "src/main/java/org/javaparser/samples/prime.java";
public static void main(String[] args) throws Exception {
CompilationUnit cu = StaticJavaParser.parse(new FileInputStream(FILE_PATH));
VoidVisitor<Void> methodNameVisitor = new IfStmtVisitor();
methodNameVisitor.visit(cu, null);
}
private static class IfStmtVisitor extends VoidVisitorAdapter<Void> {
int i=0 ;
#Override
public void visit(IfStmt n, Void arg) {
//visit a if statement, add 1
i++;
System.out.println( getNumber() );
}
public int getNumber() {
return i;
}
}
}
You have to put the print statement after all the if statements have been counted.
package org.javaparser.examples.chapter2;
public class VoidVisitorComplete {
private static final String FILE_PATH = "src/main/java/org/javaparser/samples/prime.java";
public static void main(String[] args) throws Exception {
CompilationUnit cu = StaticJavaParser.parse(new FileInputStream(FILE_PATH));
VoidVisitor<Void> methodNameVisitor = new IfStmtVisitor();
methodNameVisitor.visit(cu, null);
System.out.println(methodNameVisitor.getNumber());
}
}
private static class IfStmtVisitor extends VoidVisitorAdapter<Void> {
int i = 0;
#Override
public void visit(IfStmt n, Void arg) {
//visit a if statement, add 1
i++;
}
public int getNumber() {
return i;
}
}

i have problem understanding difference between two solutions

why should i use two swap functions in second and i only one swap function in first
//first one am getting correct answer with only one swap
import java.util.*;
public class stringper
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
String s=sc.next();
perm(s,0,s.length());
}
static void perm(String s,int left,int right)
{
if(left==right)
{
System.out.print(s+" ");
}
for(int i=left;i<right;i++)
{
s=swap(s,i,left,1);
perm(s,left+1,right);
}
}
static String swap(String s,int i,int j)
{
char ch[]=s.toCharArray();
char temp=ch[i];
ch[i]=ch[j];
ch[j]=temp;
s=new String(ch);
return s;
}
}
//in the second code am getting correct answer with two swap functions
import java.util.*;
public class stringper
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
String s=sc.next();
int nums[]={1,2,3};
perm(nums,0,nums.length);
}
static void perm(int[] nums,int left,int right)
{
for(int k=0;k<nums.length;k++)
{
System.out.print(nums[k]);
}
System.out.print(" ");
if(left==right)
{
ArrayList<Integer> l1=new ArrayList<Integer>();
for(int i=0;i<nums.length;i++)
{
//l1.add(nums[i]);
}
//l.add(l1);
}
for(int i=left;i<right;i++)
{
nums=swap(nums,i,left);
perm(nums,left+1,right);
nums=swap(nums,i,left);
}
}
static int[] swap(int[] nums,int i,int j)
{
int temp=nums[i];
nums[i]=nums[j];
nums[j]=temp;
for(int k=0;k<nums.length;k++)
{
nums[k]=nums[k];
}
return nums;
}
}

Sorting text from text File

package individual;
import java.io.*;
import java.util.*;
public class Individual{
public static void getContent(Scanner inp,String[] contents){
int i;
for(i=0;i<contents.length;i++)
contents[i]=inp.next();
public static void Sort(String[] contents){
int i,j;
int min;
String temp;
//for selection sort
for(i=0;i<contents.length-1;i++)
{
min=i;
for(j=i+1;j<contents.length;j++)
if(contents[j].compareTo(contents[min])<0)
min=j;
temp=contents[i];
contents[i]=contents[min];
contents[min]=temp;
}
}
public static void main(String[] args) throws FileNotFoundException{
String[] newAyat=new String[50];
//read From file that i save in drive
Scanner inFile=new Scanner(new FileReader("D:\\newFile.txt"));
getContent(inFile,newAyat);
Sort(newAyat);
}
}
}
so when i run the program i got an error. The output is not what i want. Is there any problems with my codes. And how to print out the sorting results?
This is the question that I try to solve:
Based on the list relates , you must program can display a list of words beginning with ' a ' , ' b ' , ' c ' and so on .
package textconcordance;
import java.util.*;
import java.io.*;
public class TextConcordance {
final static int SEN_LONG=8;
public static void getAyat(Scanner inp,String[] Sentence){
int i;
while(inp.hasNext()){
for(i=0;i<Sentence.length;i++)
Sentence[i]=inp.next();
}
}
//sort sentence
public static void sortSen(String[] Sentence){
int i,j;
int min;
String temp;
for(i=0;i<Sentence.length-1;i++)
{
min=i;
for(j=i+1;j<Sentence.length;j++)
if(Sentence[j].compareTo(Sentence[min])<0)
min=j;
temp=Sentence[i];
Sentence[i]=Sentence[min];
Sentence[min]=temp;
}
}
//for print
public static void print(String[] Sentence){
for(int i=0;i<Sentence.length;i++)
System.out.print(Sentence[i]+",");
}
public static void main(String[] args) throws FileNotFoundException{
String[] Sentence=new String[SEN_LONG];
Scanner inFile=new Scanner(new FileReader("D://Ghost.txt"));
getAyat(inFile,Sentence);
sortSen(Sentence);
inFile=null;
inFile=new Scanner(new FileReader("D://Story.txt"));
print(Sentence);
}
}

Program won't run when using java.util.Arrays -- what might be going wrong?

I get a bunch of errors when the code within the "clearPets" method is not commented out. As long as I delete that code, the program will run otherwise.
How can the problems be fixed? I've only recently learned about creating and calling methods, and this is my first time using java.util.Arrays.
The errors in the console are:
Exception in thread "main" java.lang.ArrayStoreException: java.lang.Boolean
at java.util.Arrays.fill(Unknown Source)
at rf.uhh.clearPets(uhh.java:34)
at rf.uhh.optionOne(uhh.java:39)
at rf.uhh.main(uhh.java:20)
Here is the code I have:
public class uhh {
public static void main(String[] args){
System.out.println("Select a number");
System.out.println("1");
System.out.println("2");
System.out.print("Choice: ");
Scanner scnr = new Scanner(System.in);
String numberChoice = scnr.nextLine();
if( "1".equals(numberChoice) ) {
System.out.println("You chose 1");
optionOne(new boolean[][] { {false}, {true} });
}
scnr.close();
}
public static boolean[][] adoptPets( int cats, int dogs) {
boolean[][] pets = new boolean[cats][dogs];
return pets ;
}
public static void clearPets( boolean[][]pets) {
Arrays.fill(pets, false);
}
public static void optionOne(boolean[][] center) {
clearPets(center);
boolean[][] dogFaceMan = adoptPets(10, 10);
dogFaceMan[1][1] = true;
}
}
You are passing a 2D array into a method (Arrays.fill) that expects a 1D array.
Try this:
public static void clearPets( boolean[][]pets) {
for(int i = 0; i < pets.length; i++) {
Arrays.fill(pets[i], false);
}
}

how this while(true) is working

package interf;
public class NumberPrinter {
public interface Printer {
public void print (int idx);
}
public static void print (Printer p) {
for (int i = 0; i < 4; i++) {
p.print(i);
}
}
public static void main(String[] args) {
while(true){
System.out.println("hi-1");
print(new Printer() {
#Override
public void print(int idx) {
while(true){
System.out.println(idx);
}
}
});
}
}
}
why it only printing 0 0 0
why it is not printing System.out.println("hi-1");
The code (when fixed to make system System) prints "hi-1" then lots of 0's (forever), because your inner print method has a while(true) loop in it.
The outer while(true) loop is never executed more than once because your code gets "stuck" in this inner loop, so you never see "hi-1" more than once.
Comment out the second while loop and capitalize the first letter of system.out.println and you'll get an infinite loop of:
hi-1
0
1
2
3
hi-1
0
1
2
3
...
package interf;
public class NumberPrinter {
public interface Printer {
public void print (int idx);
}
public static void print (Printer p) {
for (int i = 0; i < 4; i++) {
p.print(i);
}
}
public static void main(String[] args) {
while(true){
System.out.println("hi-1");
print(new Printer() {
#Override
public void print(int idx) {
//while(true){
System.out.println(idx);
//}
}
});
}
}
}

Categories

Resources