Where am I doing wrong in getting the outputs? - java

import java.util.Scanner;
public class Qa2QeArray2 {
public static void main(String[] args) {
int numOfAircrafts;
Scanner sc = new Scanner(System.in);
System.out.print("Enter num Of Aircrafts : ");
numOfAircrafts = sc.nextInt();
//To Determine the length of the Array
String[] stringArray = new String[numOfAircrafts];
System.out.println("Please enter the names of the Aircrafts : ");
for(int i = 0; i < numOfAircrafts; i++)
{
stringArray[i] = sc.next();
}
//To insert the string values to the Array
double[] doubleArray = new double[numOfAircrafts];
System.out.println("Please enter the shipment rate through each aircraft carrier : ");
for(int i = 0; i < numOfAircrafts; i++)
{
doubleArray[i] = sc.nextDouble();
}
//To insert the double values to the Array
String[] dubArr = new String [stringArray.length * doubleArray.length];
for (int x = 0; x < stringArray.length ; x++)
for (int y = 0; y < doubleArray.length ; y++) {
System.out.println(stringArray[x] + " \t " + doubleArray[y]);
}
}
}
I am looking for o/p as below
Emirates 20.89
Indigo 10.34
But I am getting o/p as
Emirates 20.89
Emirates 10.34
Indigo 20.89
Indigo 10.34

You have two nested loops that are running.
The x loop and y loop.
When x=0 prints the values and again when x=1
it prints values.
That is why you are getting output two times.
Remove one loop and you are good to go.

Related

Inserting a string element in the beginning of an array in Java with loops without making the last element disappear

Create an algorithm using JAVA language that will insert an array element to the BEGINNING of the array given.
the first array elements are as follows:
John, Cecille, Mark, Judas, Theresa
Example:
Insert Element to insert: Martha
New Updated List: Martha, John, Cecille, Mark, Judas, Theresa
This is my attempt, unfortunately "Theresa" at the end keeps disappearing:
import java.util.Scanner;
public class ArraySample1 {
public static void main(String[] args) {
String[] names = new String[] {"John", "Cecille", "Mark", "Judas", "Theresa"};
int x = 1, n = 5;
int i = 0, y = n;
System.out.println("The original array elements are: ");
for(i = 0; i < n; i++){
System.out.println("NAME[" + i + "] = " + names[i] + " ");
}
Scanner sc = new Scanner(System.in);
System.out.print("Enter element to insert: ");
String item = sc.next();
for(i = 0; i < x; i++){
for(y = n-1; y > i; y--){
names[y]=names[y-1];
}
names[i] = item;
}
System.out.println("The array elements after insertion :");
for(i = 0; i < n; i++) {
System.out.println("NAME[" + i + "] = " + names[i] + " ");
}
}
}
Output of the above code
As you can see from the pic, Theresa disappears. I have tried to insert an n=n+1 anywhere in the code in hopes of increasing the number of elements, but when I run the program, it returns an "out of bounds"

Getting an input on the next line of an output

Basically I am trying to get my code to output the given variables (provided by a user input) to display on a single line below my output.
For example, my code does this:
Enter 3 variables for array X:
2.1
3.5
5.4
I want it to do this:
Enter 3 variables for array Y:
2.1 3.5 5.4
Here is my entire code:
import java.util.Scanner;
public class Project4JamesVincent {
public static void main(String []args){
Scanner input = new Scanner(System.in);
System.out.print("Enter the size of the arrays: ");
int size = input.nextInt();
System.out.println();
System.out.println("Enter " + size + " items for array X: ");
double[] arrayX = createArray(size, input);
System.out.println();
System.out.println("Enter " + size + " items for array Y: ");
double[] arrayY = createArray(size, input);
System.out.print("The distance between x and y is: ");
double totalDistance = 0;
for (int i = 0; i<size; i++){
totalDistance = totalDistance + Math.abs((arrayX[i]-arrayY[i]));
}
double averageDistance = 0;
averageDistance = (totalDistance/size);
System.out.printf("%3.2f", averageDistance);
}
public static double[] createArray (int n, Scanner enter){
double[] tempArray = new double[n];
for (int i=0; i<tempArray.length; i++){
tempArray[i] = enter.nextDouble();
}
return tempArray;
}
}
Any help is appreciated.
If you change your createArray function to this, you can enter numbers either one on each line or all on one line:
public static double[] createArray (int n, Scanner enter){
double[] tempArray = new double[n];
int pos=0;
while (enter.hasNext()) {
tempArray[pos++] = enter.nextDouble();
if (pos>=n)
break;
}
return tempArray;
}

Adding multiple java arrays once

I am trying to create a simple program that asks you 10 integers and the program will automatically add them all. I always get an error from Java which is this
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 55
at Sum2.main(Sum2.java:29)
How can I add those multiple array values once?
I tried using
integerArray[0]+[1].....
But it is still not working, please help.
import java.util.Scanner;
public class Sum2 {
private static Scanner sc;
public static void main(String[] args) {
int totalsum;
int[] integerArray = new int[11];
sc = new Scanner(System.in);
System.out.println("Please enter your 10 integers : ");
integerArray[0] = sc.nextInt();
integerArray[1] = sc.nextInt();
integerArray[2] = sc.nextInt();
integerArray[3] = sc.nextInt();
integerArray[4] = sc.nextInt();
integerArray[5] = sc.nextInt();
integerArray[6] = sc.nextInt();
integerArray[7] = sc.nextInt();
integerArray[8] = sc.nextInt();
integerArray[9] = sc.nextInt();
integerArray[10] = sc.nextInt();
totalsum = integerArray[0+1+2+3+4+5+6+7+8+9+10];
System.out.println("The sum of the first 10 integers is: " +totalsum);
}
}
> totalsum = integerArray[0]
+ integerArray[1]
+ integerArray[2]
+ integerArray[3]
+ integerArray[4]
+ integerArray[5]
+ integerArray[6]
+ integerArray[7]
+ integerArray[8]
+ integerArray[9];
Or
totalsum = 0;
for(int i = 0; i < 10; i++) {
totalsum += integerArray[i];
}
By the way, your array contains 11 Integers, not 10.
EDIT: (reply on your comment)
About making code cleaner, this is a lot better than 10 times the same line:
System.out.println("Please enter your 10 integers : ");
for(int i = 0; i < 10; i++) {
integerArray[i] = sc.nextInt();
}

Arrays showing vertically instead of horizontally

I am sort of new to programming and I am working on a school assignments on arrays
I am suppose to write a program that stores statistics using arrays.
import java.io.*;
public class HockeyLeague {
static final int Rows = 7;
static final int Cols = 8;
static double HockeyChart [][] = new double[Rows][Cols];
static HockeyLeague HL = new HockeyLeague();
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public static void main(String args[])throws IOException {
while(true){
System.out.println("Welcome to the NHL statistic program");
System.out.println("Would you like to proceed to the program? <y for yes, n for no>");
final String menuDecision = br.readLine();
if (menuDecision.equals("n")) {
System.out.println("Goodbye");
System.exit(0);
}
if (menuDecision.equals("y")) {
while(true) {
System.out.println("The 8 teams are Toronto Maple Leafs, Montreal Canadiens, Ottawa Senators, Detroit Red Wings, Boston Bruins,"+
" Chicago Blackhawks, New York Islanders, and Pitsburg Penguins");
System.out.println("To input statistics for Toronto, enter '0' ");
System.out.println("To input statistics for Montreal, enter '1' ");
System.out.println("To input statistics for Ottawa, enter '2' ");
System.out.println("To input statistics for Detroit, enter '3' ");
System.out.println("To input statistics for Boston, enter '4' ");
System.out.println("To input statistics for Chicago, enter '5' ");
System.out.println("To input statistics for New York, enter '6' ");
System.out.println("To input statistics for Pitsburg, enter '7' ");
int numString = Integer.parseInt(br.readLine());
Info (numString);
}
}
}
}
public static double[][] Info(int teamInput)throws IOException{
System.out.println("Enter the amount of games played");
int games = Integer.parseInt(br.readLine());
HockeyChart [0+teamInput][1] = games;
System.out.println("Enter the amount of wins");
int wins = Integer.parseInt(br.readLine());
HockeyChart [0+teamInput][2] = wins;
System.out.println("Enter the amount of ties");
int ties = Integer.parseInt(br.readLine());
HockeyChart [0+teamInput][3] = ties;
System.out.println("Enter the amount of losses");
int losses = Integer.parseInt(br.readLine());
HockeyChart [0+teamInput][4] = losses;
System.out.println("Enter the amount of goals scored");
int goals = Integer.parseInt(br.readLine());
HockeyChart [0+teamInput][5] = goals;
for (int i = 0; i < Rows; i ++) {
for (int j = 0; j < Cols;j ++) {
System.out.println(HockeyChart[i][j] + " ");
}
System.out.println(" ");
}
return HockeyChart;
}
}
This is the program I came up with. I dont understand why I get an output that is a long vertical row of numbers instead of row of numbers side by side.
Any help would be appreciated! thanks
In your Info method, while iterating over arrays, you are using System.out.println(), instead of that you will need to use System.out.print() method.
for (int i = 0; i < Rows; i ++) {
for (int j = 0; j < Cols;j ++) {
System.out.print(HockeyChart[i][j] + " ");
}
System.out.println();
}
Second println statement will help you to move to next line after one row is printed.
Take a look at your loop:
for (int j = 0; j < Cols;j ++) {
System.out.println(HockeyChart[i][j] + " ");
}
println is short for Print Line - it prints the given string, and then moves on to the next line. If you want to print all the contents of the array in a single line, you could use System.out.print instead:
for (int j = 0; j < Cols;j ++) {
System.out.print(HockeyChart[i][j] + " ");
}
You are using System.out.println which means print in new line.
You can use System.out.print for same line i.e horizontal row.
Code will be like this
for (int i = 0; i < Rows; i ++)
{
for (int j = 0; j < Cols;j ++) {
System.out.print(HockeyChart[i][j] + " ");
}
System.out.println(" ");
}
return HockeyChart;

How do I add user input to an Array in java?

public static void main(String[] args) {
Scanner user_input = new Scanner(System.in);
int i;
int n;
String a;
System.out.println("Enter the Class:");
a = user_input.next();
System.out.println("Enter the number of Students:");
n = user_input.nextInt();
for (i= 1; i <= n; i++) {
String g = a + i;
System.out.println(g);
}
}
This is my program. It gets user input for the Class and prints the Roll Number for the students.
For example: If the class is 10A and the number of students is 10, it prints a series like 10A1 , 10A2, 10A3 ... 10A10
How do I get the program to store these as elements in an array?
For example:
array[0] = 10A1;
array[1] = 10A2;
array[2] = 10A3;
etc.
Your code should look like this:
public static void main (String args[])
{
Scanner user_input = new Scanner(System.in);
int i;
int n;
String a;
System.out.println("Enter the Class:");
a = user_input.next();
System.out.println("Enter the number of Students:");
n = user_input.nextInt();
String []strings = new String[n]; // Creating an are of string with the given number
for(i= 0; i < n ;){
strings[i] = a + ++i; // Storing strings on to the array !
System.out.println(strings[i-1]);
}
}
You can just edit each index in your current for loop:
String[] arr;
for(i=0; i < n ; i++){
int j = i+1;
String g = a + j;
System.out.println(g);
arr[i] = g;
}
So all your printed g's will be part of the array arr.
First, declare a String array of the appropriate size.
Second, in your for loop, assign the strings you are currently printing, to positions in the array.
String[] things = new String[n];
for (i=1; i <= n; i++) {
String g = a + i;
System.out.println(g);
things[i-1] = g;
}
The strings are now in an array.
Following code is modified, for storing values in array.
public static void main(String[] args) {
// TODO code application logic here
Scanner user_input = new Scanner(System.in);
int i;
int n;
String a;
System.out.println("Enter the Class:");
a = user_input.next();
System.out.println("Enter the number of Students:");
n = user_input.nextInt();
String[] arr = new String[n]; // create string array of size n.
for(i= 1; i <= n ; i++){
String g = a + i;
System.out.println(g);
arr[i-1]=g; // assign your g veriable vale to array index
}
for(String s : arr){
System.out.println(s); // print your array
}
}

Categories

Resources