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();
}
}
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 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();
}
}
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
package array;
import java.util.Scanner;
public class Array {
public static void main(String[] args) {
int n;
Scanner input = new Scanner(System.in);
n = input.nextInt();
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = input.nextInt();
}
System.out.println(a);
}
}
You cannot print an array directly, you have to iterate through it's values.
for (int j=0;j<a.length;j++){
System.out.println(a[j]);
}
I'm trying to print out the frequency of each integer in an array
import java.util.*;
public class NumFrequency {
public static void main(String [] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter the amount of numbers your going to input, up to 50");
int num = input.nextInt();
int array[] = new int[num];
System.out.println("Enter the "+ num + " numbers now.");
for (int i=0 ; i<array.length; i++) {
array[i] = input.nextInt();
}
System.out.println("array created");
printArray(array);
}
public static void printArray(int arr[]){
int n = arr.length;
for (int i=0; i<n; i++) {
System.out.print(arr[i]+" ");
}
}
private static int[] intFreqArray = new int[51];
public static void FreqOfInt(int[] array, int num) {
for (int eachInt : array) {
intFreqArray[eachInt]++;
}
for (int m = 0; m<intFreqArray.length; m++) {
if (intFreqArray[m] > 1) {
System.out.println(m+ " occurs " + intFreqArray[m] + " times.");
}
}
}
}
It'll print out the array created by the user but nothing after that I'm lost as to why it wont print out the last part.
You need to call FreqOfInt before you print.
Note that we normally use lower case letters for the names of Java methods.
In main, the last method call is to printArray, but you never call FreqOfInt. That's why that output doesn't show up.
Call it after calling printArray.