input from the keyboard and print out asterisks - java

Hi there I'm trying to input 3 integers from the keyboard and print rows of asterisks equal to the integers input from keyboard. I really appreciate if someone could help on this, thanks in advance.
public class Histogram1 {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Please input the first integer for histogram1: ");
int a1 = in.nextInt();
System.out.print("Please input the second integer for histogram1: ");
int b1 = in.nextInt();
System.out.print("Please input the third integer for histogram1: ");
int c1 = in.nextInt();
histogram1();
}
public static void histogram1(){
int n =0;
for (int i = 0; i <= n; i++)
{
for( int j = 1; j <=n; j++)
{
System.out.println("*");
}
System.out.println();
}
}
}

is it what you want ?!
your n variable was always =0 : I think you wanted an argument instead
you had 2 loops imbricated : you were printing out (n*(n-1)) *, one per line (but as n=0 none appeared)
for that purpose, do you really need a scanner ?.. System.in.read() can do the job, and you don't have to manage your Scanner.
as you asked for not having an argument, you can use a static variable in your class. I also changed the names to more meaningful variable names, as it is always a good idea to correctly choose good names for variables.
public class Histogram1 {
static int nb_stars=0;
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Please input the first integer for histogram1: ");
nb_stars = in.nextInt();
show_histogram();
System.out.print("Please input the second integer for histogram1: ");
nb_stars = in.nextInt();
show_histogram();
System.out.print("Please input the third integer for histogram1: ");
nb_stars = in.nextInt();
show_histogram();
}
public static void show_histogram(){
for(int j=1; j<=nb_stars; ++j)
{
System.out.print("*");
}
System.out.println();
}
}
}

Related

Can I search for values in two different type of arrays with just one type of variable?

I'm quite new to Java and I've been asked to create a program in which the user is able to input two values and store them in separate arrays. The two values I'm asking the user are name and cell number, then I must allow the user to search by typing either a name or a cell number and return the corresponding name or cell number. I made it possible to input the values and search within them by number but when I try searching by name I get this error :
Exception in thread "main" java.lang.NumberFormatException: For input string: "B"
at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.base/java.lang.Integer.parseInt(Integer.java:652)
at java.base/java.lang.Integer.parseInt(Integer.java:770)
This is my code:
import java.util.Scanner;
public class HW {
static Scanner sc = new Scanner(System.in);
private static int i, x = 2;
static String names[] = new String[x];
static int numbers[] = new int[x];
public static void main(String[] args) {
Input();
Compare();
}
public static void Input() {
System.out.println("Enter a name followed by the persons number");
while (i < x) {
System.out.println("NAME: ");
names[i] = sc.next();
System.out.println("NUMBER: ");
numbers[i] = sc.nextInt();
i++;
}
}
public static void Compare() {
System.out.println("=======SEARCH=======\nSEARCH CRITERIA: ");
var temp = sc.next();
System.out.println("NAME\tNUMBER");
for (i = 0; i < numbers.length; i++)
if ((names[i].equals(temp)) || (numbers[i] == Integer.parseInt(temp.trim()))) {
System.out.println(names[i] + "\t" + numbers[i]);
}
}
}
Thanks! :)
Looking at your problem statement it doesn't seem like you need to do any additional processing on numbers. Hence, even if you store the number as a string it should be fine in this case.
Hence after getting a user search criteria, you could do a simple string search within both arrays.
Hope this helps :)
First of all, the highest number that can be represented as an int in Java is 2147483647 (214-748-3647). This clearly will not be able to hold a high enough number to accommodate any phone number. To address this issue and also fix your main error, I would suggest storing the numbers as a string instead. Here's my solution:
import java.util.Scanner;
public class HW {
static Scanner sc = new Scanner(System.in);
private static int x = 2;
static String names[] = new String[x];
static String numbers[] = new String[x];
public static void main(String[] args) {
input();
compare();
}
public static void input() {
System.out.println("Enter a name followed by the persons number");
for (int i = 0; i < x; i++) {
System.out.println("NAME: ");
names[i] = sc.next();
System.out.println("NUMBER: ");
numbers[i] = sc.next();
i++;
}
}
public static void compare() {
System.out.println("=======SEARCH=======\nSEARCH CRITERIA: ");
String temp = sc.next();
System.out.println("NAME\tNUMBER");
for (int i = 0; i < numbers.length; i++) {
if ((names[i].equals(temp)) || numbers[i].equals(temp)) {
System.out.println(names[i] + "\t" + numbers[i]);
}
}
System.out.println("===END OF SEARCH====")
}
}
Please also note that I un-defined your variable i. As far as I can see there's no reason for you to be defining it. Hope this helps, good luck!

How to take n input in java according to the value of test cases?

How can we take n input in java according to value of T
Like if i take t=2,then we take two n input
If value of t=3,then we take three n input
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner s= new Scanner(System.in);
int t=s.nextInt();
int n=s.nextInt();
int n=s.nextInt();
int evensum=0;
for (int i=1; i<=n; i++) {
if(i%2==0) {
evensum=evensum+i;
}
System.out.print(evensum);
}
}
As in this code i take t=2 and I take two n inputs.
If I take t=3, then what how could I take that third input?
You can't have multiple variable with the same name, for your purpose use a loop :
Scanner s = new Scanner(System.in);
System.out.println("How many values ?");
int nbInput = s.nextInt();
for (int i = 1; i <= nbInput; i++) {
int input = s.nextInt();
System.out.print(input);
}

Java User Input array is only capturing 3 integers instead of five

This code is supposed to capture 5 user integers, print them out, then print them in reverse. It is capturing the first int only, and printing it 3 times, then printing the first integer again 5 more times without reversing. Test ends with "Process finished with exit code 0" which I think is says the program finished without errors -- which of course is not correct. I assume the issue is in how the user input array is stored. I have it assigning as userNum[i] with a limited array of 5, and int i =0 to begin array storage at userNum[0], so I'm not clear on why all the inputs are not captured up to userNum[4].
Thank you for any insight you can provide. I am very new to java and this is prework for my java class.
import java.util.Scanner;
public class ArrayReverse {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in); // scanner for input
final int NUM_VALS = 5; // number on int user able to enter
int[] userNum = new int[NUM_VALS]; // user integers storage
int j = 0;
int i = 0;
System.out.println("Enter integer values: ");
userNum[i] = scnr.nextInt(); // capture user input int
for (j = 0; j < NUM_VALS; j++) {
System.out.print("You entered: ");
System.out.println(userNum[i]);
++j;
}
System.out.print("\nNumbers in reverse: "); // statement to Print reversed array
for (j = NUM_VALS - 1; j >= 0; j--) {
System.out.print(userNum[i] + " ");
}
}
}
You need to work more about on for loops and study how to iterate values in for loop, the problem in your i,j variables.
Here I fix your code.
import java.util.Scanner;
public class ArrayReverse {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in); // scanner for input
final int NUM_VALS = 5; // number on int user able to enter
int[] userNum = new int[NUM_VALS]; // user integers storage
int j = 0;
int i = 0;
//for 5 inputs you need loop
for(;i<NUM_VALS;i++){
System.out.println("Enter integer values: ");
userNum[i] = scnr.nextInt(); // capture user input int
}
for (j = 0; j < NUM_VALS; j++) {
System.out.print("You entered: ");
System.out.println(userNum[j]);
//++j; //no need to increment as you already did in for loop
}
System.out.print("\nNumbers in reverse: "); // statement to Print reversed array
for (j = NUM_VALS - 1; j >= 0; j--) {
System.out.print(userNum[j] + " ");// userNum[0] = your last value which you reverse
}
}
}
Here is a solution using the collections framework:
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;
public class ArrayReverse {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in); // scanner for input
final List<Integer> numbers = new ArrayList<>();
System.out.println("Enter any number of integers. (whitespace delimited. enter a non-integer to quit.): ");
while (scnr.hasNextBigInteger()) {
final int n = scnr.nextInt();
System.out.println("Parsed: " + n);
numbers.add(n);
}
System.out.println("Done reading user input.");
System.out.println("Your input: " + numbers);
Collections.reverse(numbers);
System.out.println("Your input reversed: " + numbers);
}
}
I have provided you with a solution. This is a clean way of doing it.
nextInt() reads the next integer that the user inputs. Notice that this will throw a InputMismatchExceptionif the user does not input a integer as value.
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
List<Integer> input = new ArrayList<Integer>();
//Simple loop that will read 5 user inputs
//and add them to the input list
for(int i = 0; i < 5; i++){
input.add(scanner.nextInt());
}
//print the 5 values
for(Integer val : input){
System.out.println(val);
}
//reverse the 5 values
Collections.reverse(input);
//print the 5 values again, but they are now reversed
for(Integer val : input){
System.out.println(val);
}
}

User based Multiplication Table

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);
}

How can I make a program which ask the user to enter number of limit odd number?

For example, if I enter 5, then the first 5 odd numbers will be shown, like 1,3,5,7,9.
import java.util.Scanner;
/**
*
* #author Hameed_Khan
*/
public class JavaApplication20 {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
Scanner obj=new Scanner(System.in);
int i;
System.out.println("Enter limit of ODD num");
i=obj.nextInt();
for(int n=0;n<10;n++){
if(n%2!=0){
int count=n;
while(count!=i)
System.out.print("\t"+n);
n++;
}
}
}
}
If I understand your question, I'd suggest a simple for loop.
int limit = (what ever user input you use);
int oddNums = 1;
for(int ii = 0; ii < limit; ii++ )
{
System.out.println(oddNums);
oddNums += 2;
}
Here you go.
import java.util.Scanner;
public class JavaApplication20 {
public static void main(String args[]) {
System.out.println("Enter an integer");
Scanner in = new Scanner(System.in);
int count = in.nextInt();
for(int i =1, j=1 ; i <= count ; j+=2,i++){
System.out.print(j);
if(i < count) System.out.print(",");
}
}
}
Hope you understand whats happening with the loop and the 2 variables.
Here i control the no. of iterations i.e. equal to the integer entered by the user and j is used to store the value of the odd number and is always incremented by 2 to get the next odd number.
Try its as:
public class JavaApplication20 {
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
{
Scanner obj=new Scanner(System.in);
int i,count=0;
System.out.println("Enter limit of ODD num");
i=obj.nextInt();
for(int n=0;;n++)
{
if(n%2!=0 && count!=i)
{
System.out.print("\t"+n);
count++;
}
}
}
}

Categories