My code's output is not the array I input [duplicate] - java

This question already has answers here:
What's the simplest way to print a Java array?
(37 answers)
Closed 1 year ago.
I am trying to run the following code in order to input an array of any size I desire:
import java.util.Scanner;
import java.util.Arrays;
public class testing2 {
public static void main(String[] args) {
double[] inputs = new double[5];
int currentSize = 0;
System.out.println("Please enter values, Q to quit:");
Scanner in = new Scanner(System.in);
while (in.hasNextDouble())
{
if (currentSize >= inputs.length)
{
inputs = Arrays.copyOf(inputs, 2 * inputs.length);
}
inputs[currentSize] = in.nextDouble();
currentSize++;
}
inputs = Arrays.copyOf(inputs, currentSize);
System.out.print(inputs);
}
}
But when I input any array, my output is just a combination of symbols such as: [D#13fee20c, and this is not the output I am hoping for. Could anyone help me?

You're not printing correctly:
for (int i = 0; i < input.length; i++)
System.out.print(input[i] + " ");
}

Related

Output contents of array in reverse in Java [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
My task is to make an array of size 20. Ask the user how many numbers he/she wants to enter. Put all those numbers in an array, then output that array in reverse. I've gotten it completed up to the "output that array in reverse" part.
import java.util.Scanner;
public class Activity7 {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System. in );
System.out.println("How many numbers?");
int quantityOfNumbers = keyboard.nextInt();
int[] numbers = new int[20]; //making an array the size of 20
//0 - 19 (Valid values of the array)
for (int subscript = 0; subscript < quantityOfNumbers; subscript++) {
System.out.println("Enter number " + subscript);
numbers[subscript] = keyboard.nextInt();
}
System.out.println("Array Contents");
for (int subscript = 19; subscript >= 0; subscript--) {
}
}
}
If you want to print the array with out the empty elements, you can use something like this. As 0 is the default int value, print it as long as it is not 0.
public static void reverse(int[] array)
{
for(int i=array.length-1;i>=0;i--)
{
if(array[i]!=0)
{
System.out.println(array[i]);
}
}
}
I'm not completely sure what you are looking for. Could you specify a bit more? I'm completing your code to match the provided output anyway:
import java.util.Scanner;
public class Activity7
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
System.out.println("How many numbers?");
int quantityOfNumbers = keyboard.nextInt();
int[] numbers = new int[20]; //making an array the size of 20
//0 - 19 (Valid values of the array)
for (int subscript = 0; subscript < quantityOfNumbers; subscript++)
{
System.out.println("Enter number " + subscript);
numbers[subscript] = keyboard.nextInt();
}
System.out.println("Array Contents");
for (int subscript = 19; subscript >= 0; subscript--)
{
if (subscript >= quantityOfNumbers) System.out.println("Subscript " + subscript + "is empty");
else System.out.println("Subscript " + subscript + "contains " + numbers[subscript]);
}
}
}

for loop jumping an input in java [duplicate]

This question already has answers here:
Scanner is skipping nextLine() after using next() or nextFoo()?
(24 answers)
Closed 4 years ago.
this is a program that given in input 5 names and 5 telephone numbers gives in output the names with their numbers in alphabetical order.
the problem is that when I give in input the first name and the first number, then the program jumps to the second "telephone number input" without making me insert the second name.
I hope this makes sense.
also I wouldn't mind any suggestion to make the sorting easier.
this is the code:
import java.util.Arrays;
import java.util.Scanner;
public class RubricaTelefonica {
public static void main(String[] args) {
String names[] = new String[5];
long phone_num[] = new long[5];
Scanner sc = new Scanner(System.in);
for(int i = 0; i < 5; i++) {
System.out.println("Inserisci il nome:");
names[i] = sc.nextLine();
System.out.println("Inserisci il numero di telefono:");
phone_num[i] = sc.nextLong();
}
sc.close();
String names_unsorted[] = names;
Arrays.sort(names);
long sorted_num[] = new long[5];
for(int a = 0; a < 5; a++) {
//sorted cicle
for(int b = 0; b < 5; b++) {
//unsorted cicle
if(names[a] == names_unsorted[b]) {
sorted_num[a] = phone_num[b];
}
}
}
}
}
You misinterpret nextLong(); it simply reads the long value and then the upcoming nextLine() is triggered by the new line entry from the console.
Put an sc.nextLine() after the nextLong() call, or even nicer is to read the phone number as String with nextLine() and then parse a long from it.

for loop is printing twice in java and data conversion [duplicate]

This question already has answers here:
Scanner is skipping nextLine() after using next() or nextFoo()?
(24 answers)
Closed 6 years ago.
import java.util.Scanner;
import java.text.SimpleDateFormat;
import java.util.Date;
public class salary
{
public static void main(String[] args)
{
int n,sum=0,average=0,count1=0,count2=0;
System.out.println("How many people salary you want to enter" );
Scanner in = new Scanner(System.in);
n = in.nextInt();
String[] salary=new String[n];
int[] sal=new int[n];
for (int i=0;i<n;i++)
{
System.out.println("please enter salary with $ sign as prefix" );
salary[i]=in.nextLine();
}
for (int j=0;j<n;j++)
{
salary[j] = salary[j].replaceAll("[$]+", "0");
System.out.println(salary[j]);
sal[j]= Integer.parseInt(salary[j]);
//salary[j] = Integer.parseInt(salary[j].replaceAll("[$]+", " "));
sum=sum+sal[j];
}
average=sum/n;
for(int q=0;q<n;q++)
{
if (sal[q]>average)
{count1=count1+1;
}
else if (sal[q]<average)
{
count2=count2+1;
}}
System.out.println("The average is "+average);
System.out.println("The salary greater than average "+count1);
System.out.println("The salary less than average "+count2);
}
}
getting error and also for loop is not working properly
Exception in thread "main" java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:592)
at java.lang.Integer.parseInt(Integer.java:615)
at salary.main(salary.java:34)
read a list of salary amounts that start with a dollar sign “$” and followed by a nonnegative
number, save the valid salary inputs into an array and sort the salary in ascending
order, calculate the average of the salary inputs, and count the number of inputs less than
/greater than the average.
Change Line 16 to in.next:
for (int i = 0; i < n; i++) {
System.out.println("please enter salary with $ sign as prefix");
salary[i] = in.next();
}

How to concatenate string in a for loop using scanner? [duplicate]

This question already has answers here:
How do I concatenate two strings in Java?
(23 answers)
Closed 6 years ago.
package exercises;
import java.util.Scanner;
public class SentenceBuilder {
public static void main(String[] args) {
final int MAX_WORDS = 5;
Scanner scan = new Scanner(System.in);
String word ="";
for (int i = 0; i < MAX_WORDS; i++){
System.out.println("Please enter word "+(i+1)+" of "+MAX_WORDS);
word = scan.nextLine();
}
System.out.println(word);// im stuck on how to concatenate the result
}
}
Inside the for:
word = word + scan.nextLine();

Sorting 5 generated numbers from lowest to highest [duplicate]

This question already has answers here:
int[] array (sort lowest to highest)
(11 answers)
Closed 6 years ago.
I have a project in school to make a simplified Yatzy program and now I've hit a road bump. I have generated 5 random dice throws and printed them out in one window, but I need to sort them from lowest to highest and I don't know how.
I've looked in forums like this one but I don't understand, feel free to ask questions if you don't understand. BTW I'm Swedish so the text in the "" lines are in Swedish if you don't understand.
This is my code:
import javax.swing.*;
public class Projekt1 {
public static void main(String[] args) {
JOptionPane.showMessageDialog(null, "Välkommen till Johans Yatzy");
JOptionPane.showConfirmDialog(null, "Vill du starta spelet?");
int[] tar = new int[5];
String output = "";
for(int i=0; i<5; i++){
tar[i] = (int)(Math.random()*6+1);
output = output + tar[i] + "\t";
}
JOptionPane.showMessageDialog(null, "Dina tärningskast blev följande: " + output);
}
}
Use
java.util.Arrays.sort(tar);
to sort the int array:
for(int i = 0; i < tar.length; i++){
tar[i] = (int)(Math.random()*6+1);
}
// sort tar array
java.util.Arrays.sort(tar);
for (int i = 0; i < tar.length; i++) {
output = output + tar[i] + "\t";
}

Categories