I like to print a pattern in java which gives the output:
1
23
456
78910 ....
But i am not getting how to do this, I wrote a program to print pattern of
1
12
123
1234 ...
as
import java.util.*;
public class Test {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int r,c;
for(r=1;r<=5;r++) {
for(c=1;c<=r;c++){
System.out.print(c+" ");
}
System.out.println();
}}}
Getting that last number on each row and start from that number on the second row is what I am having trouble with. I tried adding an another for loop in between the other but it didn't help.Can anyone get me what I am missing and how to do this!
You are printing the column counter c, which is reset at every row: you'll want a separate variable to hold the number you want to print.
Also, you should probably start getting accustomed to start counting at zero :)
public static void main(String[] args) {
int n = 1;
for (int r = 0; r < 4; r++) {
for (int c = 0; c < r+1; c++) {
System.out.print(n++);
if (c != r) System.out.print(" ");
}
System.out.println();
}
}
import java.util.Scanner;
public class Pattern14 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int flag = 1;
for(int i=1;i<=n;i++) {//number of rows
for(int j=1;j<=i;j++) { // decides the number of digits in row
System.out.print(flag+++" ");
}
System.out.println();
}
}
}
public static void main(String args[]) {
int x=1;
Scanner sc = new Scanner(System.in);
int n=sc.nextInt();
for (int i=1;i<=n;i++) {
for (int j=1;j<=i;j++) {
System.out.print(x+++" ");
}
System.out.println();
}
}
Related
I have learnt selection sort, and i try to code it with java. But it has an error, i think it a runtime error. I don't know what to fix in my code.
This is the code:
import java.util.Scanner;
public class Main {
public static void main(String args[])
{
int temp;
Scanner sc=new Scanner(System.in);
int number;
int input=sc.nextInt();
int [] carriage;
carriage=new int[input];
for(int i=0;i<input;i++)
{
number=sc.nextInt();
carriage[i]=number;
}
int n=carriage.length;
for(int i=0;i<n-1;i++)
{
for(int j=i+1;i<n;j++)
{
if(carriage[j]<carriage[i])
{
temp=carriage[i];
carriage[i]=carriage[j];
carriage[j]=temp;
}
}
System.out.println(carriage[i]+ " ");
}
sc.close();
}
}
I think you want to sort the number of integer provided by user. Your code is having 2 errors. One in the for loop starting with i, the condition should be i
public class Main {
public static void main(String args[]) {
int temp;
Scanner sc = new Scanner(System.in);
int number;
System.out.println("Enter the number of integers to be sorted - ");
int input = sc.nextInt();
int[] carriage;
carriage = new int[input];
for (int i = 0; i < input; i++) {
System.out.println("Enter the "+ i+1 +"number - ");
number = sc.nextInt();
carriage[i] = number;
}
int n = carriage.length;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (carriage[j] < carriage[i]) {
temp = carriage[i];
carriage[i] = carriage[j];
carriage[j] = temp;
}
}
System.out.println(carriage[i] + " ");
}
sc.close();
}
}
I've been working on this program and am currently stuck. The HW prompt is to prompt a user to input numbers, save it as an array, find the number of odd numbers & the percentages then display those values back to the user.
Currently I am trying to write to part of the code that finds the percentage of the odd numbers in the array but the return isn't displaying and i just cant figure it out. Any ideas? Thank you!
import java.util.*; // import java course for Scanner class
public class Integers {
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
System.out.println("Please input a series of numbers");
int inputs = Integer.parseInt(console.next());
int[] arraysize = new int[inputs];
Oddvalues(arraysize);
}
public static int Oddvalues (int[] size) {
int countOdd = 0;
for (int i = 1; i < size.length; i++) {
if(size[i] % 2 != 0) {
i++;
}
}
return countOdd;
}
}
Consider the following code, which appears to be working in IntelliJ locally. My approach is to read in a single line from the scanner as a string, and then to split that input by whitespace into component numbers. This avoids the issue you were facing of trying to directly create an array of integers from the console.
Then, just iterate over each numerical string, using Integer.parseInt(), checking to see if it be odd.
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
System.out.println("Please input a series of numbers");
String nextLine = console.nextLine();
String[] nums = nextLine.split(" ");
int oddCount = 0;
for (String num : nums) {
if (Integer.parseInt(num) % 2 == 1) {
++oddCount;
}
}
double oddPercent = 100.0*oddCount / nums.length;
System.out.println("Total count of numbers: " + nums.length + ", percentage odd: " + oddPercent);
}
In the function Oddvalues you promote i instead of promoting countOdd. And the loop should start from 0 not 1.
Try this
import java.util.*;
import java.lang.*;
import java.io.*;
public class OddVals{
public static void main(String[] args) throws java.lang.Exception {
Scanner sc = new Scanner(System.in);
int[] array = new int[sc.nextInt()]; // Get the value of each element in the array
System.out.println("Please input a series of numbers");
for(int i = 0; i < array.length; i++)
array[i] = sc.nextInt();
System.out.println("Number of Odds:" +Oddvalues(array));
printOdd(array);
}
public static int Oddvalues (int[] size) {
int countOdd = 0;
for (int i=0; i < size.length; i++){
if(size[i]%2 != 0)
++countOdd;
}
return countOdd;
}
public static void printOdd(int[] arr)
{
for(int i=0;i<arr.length;++i)
{
if(arr[i]%2==1)
System.out.print(arr[i]+" ");
}
}
import java.util.*; // import java course for Scanner class
public class Integers {
public static void main(String[] args) {
List<Integer> intList = new ArrayList<Integer>();
Scanner console = new Scanner(System.in);
System.out.println("Please input a series of numbers");
while (console.hasNext())
{
String str = console.next();
try
{
if(str.equals("quit")){
break;
}
int inputs = Integer.parseInt(str);
System.out.println("the integer values are" +inputs);
intList.add(inputs);
}
catch (java.util.InputMismatchException|NumberFormatException e)
{
console.nextLine();
}
}
console.close();
double d = Oddvalues(intList);
System.out.println("the percent is" +d);
}
public static double Oddvalues (List<Integer> list) {
int count = 0;
for( Integer i : list)
{
if(!(i%2==0))
{
count++;
}
}
double percentage = (Double.valueOf(String.valueOf(count))/ Double.valueOf(String.valueOf(list.size())))*100;
return percentage;
}
}
If this helps
Hi I need help with my code. Im trying to make a user base input multiplication table using a Scanner and a method, but i dont know how to get the user base input from the main method to the method i created, here is what i have so far:
public static void multiplicationTable(int i){
for (int i=1;i<=size;i++){
for (int j=1;j<=size;j++)
System.out.print("\t"+i*j);
System.out.println(); }
}
public static void main (String[] args) {
System.out.println("This program displays a multiplication table.");
Scanner size = new Scanner(System.in);
System.out.println("Enter a positive integer: ");
int n = size.nextInt ();
}
You can pass like :
System.out.println("This program displays a multiplication table.");
Scanner size = new Scanner(System.in);
System.out.println("Enter a positive integer: ");
int n = size.nextInt ();
multiplicationTable(n); \\ pass here
Actually your codes are pretty closed. Please see below (I have tested the codes):
public static void multiplicationTable(int size) {
for (int i = 1; i <= size; i++) {
for (int j = 1; j <= size; j++) {
System.out.print("\t" + i * j);
}
System.out.println();
}
}
public static void main(String[] args) throws Exception {
System.out.println("This program displays a multiplication table.");
Scanner size = new Scanner(System.in);
System.out.println("Enter a positive integer: ");
int n = size.nextInt();
multiplicationTable(n);
}
I know this is simple. How would I take input from my console and store the input into a Set that can later be used to be returned on a Method. This is what I have so far.
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
public class InputConsole {
public static void main(String[] args) {
Set<Integer> s = new HashSet<Integer>(6);
int[] numbers = new int[6];
Scanner input = new Scanner(System.in);
for (int i = 0; i < numbers.length; i++) {
System.out.print("Please enter number ");
numbers[i] = input.nextInt();
{
}
}
}
}
I am using and Array just to test with. The Array is set to 6 so if I type 6 numbers in the console it will stop. I have instantiated the HashSet but I don't know how to go about storing the numbers from the console into it.
Use method Set::add()
for (int i = 0; i < numbers.length; i++)
{
System.out.print("Please enter number ");
s.add(input.nextInt());
}
You don't need int[] array
EDIT:
Whole main()
public static void main(final String ... args)
{
final int inputs = 6;
final Set<Integer> s = new HashSet<Integer>(6);
final Scanner input = new Scanner(System.in);
for (int i = 0; i < inputs; i++)
{
System.out.print("Please enter number #" + (i + 1) + ":");
s.add(input.nextInt());
}
System.out.println("Well done!");
System.out.println(s);
}
import java.util.*;
class Hashsetdemo
{
public static void main(String args[])
{
HashSet h=new HashSet(6);
int [] no = new int[6];
Scanner s=new Scanner(System.in);
for (int i=0;i<no.length;i++)
{
System.out.println("please enter number");
h.add(s.nextInt());
}
System.out.println(h);
}
}
I am using java and want to use a Scanner to get user input and then output arrays.
I've created a file that works for one and a separate file for the other, but I want to ask the user questions and get custom array properties back (variable number of arrays, variable rows, variable columns, variable addition, subtraction, and multiplication of the array)
import java.util.Scanner;
import java.io.IOException;
public class ScannerDemoforNetbeans
{
public static void main (String [] args)throws IOException
{
//`enter code here`
System.out.println ("Okay, you got it! How many rows do you want");
Scanner rc= new Scanner (System.in);
public static rows;
rows= rc.nextInt();
System.out.println ("How many columns do you want?");
Scanner c = new Scanner (System.in);
public static columns;
columns = c.nextInt();
System.out.println ("Rows" + rows);
System.out.println ("Columns:" + columns);
}
// }
// public static void createarray (String [] args)
// {
int matWidth = ScannerDemoforNetbeans.columns;
int matHeight = ScannerDemoforNetbeans.rows;
int maxCellValue = 70;
int [][] mat1 = new int [matHeight][matWidth];
int [][] mat2 = new int [matHeight][matWidth];
int [][] sum = new int [matHeight][matWidth];
for (int i=0; i<matHeight; ++i)
{
for (int j=0; j<matWidth; ++j)
{
java.util.Random r = new java.util.Random();
mat1 [i][j]= r.nextInt (maxCellValue);
mat2 [i][j] = r.nextInt (maxCellValue);
}
}
print2Darray (mat1);
System.out.println ();
print2Darray (mat2);
for (int i=0; i<matHeight; ++i)
{
for (int j=0; j<matWidth; ++j)
{
sum [i][j]= mat1 [i][j] + mat2 [i][j];
}
}
System.out.println ();
print2Darray (sum);
}
public static void print2Darray (int[][]arr)
{
for (int [] i : arr)
{
for (int j : i)
{
System.out.print (j + " ");
}
System.out.println();
}
System.out.println ();
}
}
Any help is appreciated.
package araay;
import java.util.Scanner;
public class Araay {
public static void main(String[] args)
{
Scanner a=new Scanner(System.in);
int i=0;
int j=0;
int arr[][]=new int [3][3];
System.out.println ("Enter the numbers");
for (i=0;i<=3;i++)
for(j=0;j<=3;j++)
arr[i][j]=a.nextInt();>>>Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
at araay.Araay.main(Araay.java:18)//this error is occur in this code plz solve it
for (i=0;i<=3;i++)
for(j=0;j<=3;j++)
System.out.println("The matrix of array is "+arr[i][j]+"\t");
System.out.println();
}
}