Why am I getting a '.class' expected error? Simple Array script - java

My program is creating an array and allowing the user to input 10 double precision numbers. Then the program will sort them in order from lowest to highest. I have the following but receive .class expected error upon compiling. Any ideas on why this is happening? Note * I have not been able to compile this yet so I don't even know if this will work. *
import java.io.*;
public class ArrayDemo
{
public static void main(String[] args) throws IOException
{
int i = 0;
int j = 0;
int temp = 0;
double[] intValue = new double[10];
String[] numbers = {"first", "second", "third", "fourth", "fifth", "sixth", "seventh", "eighth", "ninth", "tenth"};
int len = intValue.length[];
BufferedReader dataIn = new BufferedReader (new InputStreamReader(System.in));
for (i = 0; i < len; ++i)
System.out.println("Enter the " + numbers[i] + " number");
intValue[i] = Double.valueOf(dataIn.readLine());
{
for (j = 0; j < (len - 1) -i; j++)
if (intValue[j] > intValue[j+1])
{
temp = intValue[j];
intValue[j] = intValue[j+1];
intValue[j+1] = temp;
}
for (i = 0; i < 10; i++);
{
System.out.println("Array after sorting in ascending order");
System.out.println();
System.out.println(intValue[i]);
}
}
}
}
Thank you for any input. :)

int temp = 0; should be double temp = 0;
and
int len = intValue.length[]; should be int len = intValue.length;
and
for (i = 0; i < 10; i++); should be for (i = 0; i < 10; i++)
Sample
EDIT
import java.io.*;
public class Main
{
public static void main(String[] args) throws IOException
{
int i = 0;
int j = 0;
int k = 0;
double temp = 0;
double[] intValue = new double[10];
String[] numbers = {"first", "second", "third", "fourth", "fifth", "sixth", "seventh", "eighth", "ninth", "tenth"};
int len = intValue.length;
BufferedReader dataIn = new BufferedReader (new InputStreamReader(System.in));
for (i = 0; i < len; ++i) {
System.out.println("Enter the " + numbers[i] + " number");
intValue[i] = Double.valueOf(dataIn.readLine());
}
for (j = 0; j < len; j++)
{
for(k = 0; k < len; k++) {
if (intValue[j] > intValue[k])
{
temp = intValue[j];
intValue[j] = intValue[k];
intValue[k] = temp;
}
}
}
System.out.println("Array after sorting in ascending order");
for (i = 0; i < 10; i++)
{
System.out.print(intValue[i] + ", ");
}
}
}

int len = intValue.length[];
You don't need [] after length and you also tried to assign int temp to a value in a double array
temp = intValue[j];
Also using an IDE like Eclipse/NetBeans/IntelliJ would definitely help!

int len = intValue.length[];
should instead be:
int len = intValue.length;
Also, some of your bracketing appears to be incorrect. I believe, for example, that you want the following snippet:
for (i = 0; i < len; ++i)
System.out.println("Enter the " + numbers[i] + " number");
intValue[i] = Double.valueOf(dataIn.readLine());
Changed to:
for (i = 0; i < len; ++i)
{
System.out.println("Enter the " + numbers[i] + " number");
intValue[i] = Double.valueOf(dataIn.readLine());
}
You have a number of other logical errors in your code as well. Let me know, after you work with the code for a while based on the current answers, if you have any specific questions and I will help you further.

Related

Having trouble searching array

So I have everything working fine up until the point in which I need to search. I'm really new to this so my code is probably awful I'm sorry in advance. Anyway, its a user input array, and the user should be able to search for a number in an array. Im getting the error for a duplicate variable on line 50 (int i, get 1).
import java.util.Scanner;
class SearchingSorting {
public static void main (String[] args) {
Scanner input = new Scanner(System.in);
System.out.println ("How many numbers would you like to input?");
int num = input.nextInt();
double[] array = new double[num];
for (int i = 0; i < num; i++) {
System.out.println ("Input number " + (1 + i) + ":");
array[i] = input.nextDouble();
}
for (double temp1 : array){
System.out.print (temp1 + "\t");
}
input.close();
int pass;
int i;
int hold;
for(pass = 1; pass < array.length; pass++)
{
for(i = 0; i < array.length - 1; i++)
{
if(array[i] > array[i+1])
{
hold = (int) array[i];
array[i] = array[i+1];
array[i+1] = hold;
}
}
System.out.println("\nSorted number is: ");
for(i = 0; i < array.length; i++)
System.out.print(" " + array[i]);
}
int i, get1;
Scanner keyboard = new Scanner(System.in);
int[] numbers = new int[10];
for(i = 0; i < numbers.length; i++)
{
numbers[i] = i * 10;
}
System.out.print("Enter search number: ");
get1 = keyboard.nextInt();
SearchMethod(numbers, get1);
}
public static void SearchMethod(int[] num, int get2)
{
int i ;
boolean j = false;
for(i = 0; i < num.length; i++)
{
if(num[i] == get2)
{
j = true;
break;
}
}
if(j == true)
System.out.println(get2 + " is found at num[" + i + "]");
else
System.out.println(get2 + " is not found in an array");
}
}
You are trying to declare a new variable with the same name ("i") in the same scope.
Rename your variable i on line 50.

Creating a matrix from a text file - Java

I have this text file:
2
2
12
13
23
24
49
59
69
79
the first two numbers should be the rows and columns of the matrix, which is 2x2 in this case. My issue that I'm trying to get around is finding a way to include a second 2D array that holds the second matrix.
my code:
Scanner fileInput = new Scanner(new File("input1.txt"));
int n1 = fileInput.nextInt();
int n2 = fileInput.nextInt();
System.out.print("matrix is " + n1 + "x" + n2 +"\n");
int [][] firstMatrix = new int [n1][n2];
int [][] secondMatrix = new int [n1][n2];
for(int i = 0; i < n1; ++i)
{
for(int j = 0; j < n2; ++j)
{
if(fileInput.hasNextInt())
{
firstMatrix[i][j] = fileInput.nextInt();
}
}
}
System.out.println("Matrices: ");
for(int i = 0; i < n1; i++)
{
for(int j = 0; j < n2; j++)
{
System.out.println(firstMatrix[i][j]);
}
}
it only prints the following:
12
13
23
24
How do I make it read the next four lines of integers from the file? It would also be helpful to understand how I can make it look something like this:
12 13
23 24
EDIT: This approach seemed to help with the last question:
for(int i=0; i<n1; i++)
{
for(int j=0; j<n2; j++)
{
System.out.print(firstMatrix[i][j] + " " );
//System.out.print(secondMatrix[i][j] + " ");
}
System.out.println();
}
The only problem I'm facing now is being able to include the four other integers and turn them into a matrix.
You can group them to 1 array:
int matrixNumb = 2; // number of matrix
int [][][] matrix = new int [matrixNumb][n1][n2];
for (int h = 0; h < matrixNumb; h++)
{
for(int i = 0; i < n1; ++i)
{
for(int j = 0; j < n2; ++j)
{
if(fileInput.hasNextInt())
{
matrix [h][i][j] = fileInput.nextInt(); // read from file
}
}
}
}
System.out.println("Matrices: ");
for (int h = 0; h < matrixNumb; h++)
{
for(int i = 0; i < n1; i++)
{
for(int j = 0; j < n2; j++)
{
System.out.print(matrix[h][i][j]);
System.out.print("\t"); //How do you want to separate columns?
}
System.out.print("\r\n"); //How do you want to separate rows?
}
System.out.println(); //How do u want to print next matrix?
}
I have not compiled or run it, but hope it help.
You can just read data for the two arrays from file sequentially, like this:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class App {
private static void readMatrix(final Scanner scanner, final int[][] matrix) {
for (int i = 0; i < matrix.length; ++i) {
for (int j = 0; j < matrix[i].length; ++j) {
if (scanner.hasNextInt()) {
matrix[i][j] = scanner.nextInt();
}
}
}
}
private static void displayMatrix(final int[][] matrix) {
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[i].length; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
}
public static void main(final String... args) throws FileNotFoundException {
final Scanner scanner = new Scanner(new File("input.txt"));
final int n1 = scanner.nextInt();
final int n2 = scanner.nextInt();
System.out.print(String.format("The matrix is %d x %d \n", n1, n2));
final int[][] firstMatrix = new int[n1][n2];
final int[][] secondMatrix = new int[n1][n2];
System.out.println("Reading data to first matrix");
readMatrix(scanner, firstMatrix);
System.out.println("Reading data to second matrix");
readMatrix(scanner, secondMatrix);
System.out.println("First Matrix");
displayMatrix(firstMatrix);
System.out.println("Second Matrix");
displayMatrix(secondMatrix);
}
}
for(int i = 0; i < n1*n1; i++)
{
for(int j = 0; j < n2*n2; j++)
{
if(fileInput.hasNextInt())
{
if(i < n1){
firstMatrix[i][j] = fileInput.nextInt();
}
else
{
secondMatrix[i][j] = fileInput.nextInt();
}
}
}
}
You have two matrix of size n*n so you can do like this:

Convert two 1D arrays into single 2D array

I have 2 1d arrays and i am trying to populate them into a single 2d array in JAVA.
For instance:
a[] = {2,7}
b[] = {9,1}
The results should then be:
result[][] = {{2,9}, {7,1}}
This is my code
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter Test Cases:\t");
int t = sc.nextInt();
int[] a;
int[] b;
int i, j, x;
for (int k = 0; k < t; k++) {
System.out.println("Enter 1st Array Limit:\t");
int len = sc.nextInt();
System.out.println("Enter 2nd Array Limit:\t");
int len1 = sc.nextInt();
a = new int[len];
b = new int[len1];
System.out.println("Enter Sum Value");
x = sc.nextInt();
System.out.println("Enter " + len + " elements:\t");
for (i = 0; i < len; i++) {
a[i] = sc.nextInt();
}
System.out.println("Enter " + len1 + " elements:\t");
for (j = 0; j < len1; j++) {
b[j] = sc.nextInt();
}
int [][] c = new int[len][2];
for (i = 0; i < len; i++) {
for (j = 0; j < len1; j++) {
if (a[i] + b[j] == x) {
for(int l = 0; i < a.length; i++){
c[l][0] = a[i];
c[l][1] = b[j];
}
}
}
}
System.out.println(Arrays.deepToString(c));
}
}
}
This still produces wrong output
i want to find Find all pairs with a given sum
int[] a = {2,7};
int[] b = {9,1};
int[][] c = new int[a.length][2];
for(int i = 0; i < a.length; i++){
c[i][0] = a[i];
c[i][1] = b[i];
}
should do the trick

Appending Arrays in loop

I have a java code where X is a 2D array and a loop to add data to it. The code is as follows:
public static void main(String [] args0
{
int[] len = new int[3];
double[][] X = null;
double[][] vec = null;
for (int i = 0; i < 3; i++)
{
System.out.println("Enter the len" +(i+1)+":");
len[i] = in.nextInt();
if(i == 0)
{
vec = new double [1][len[i] + 1];
X = new double[1][vec[0].length];
for(int k = 0; k < vec[0].length - 1; k++)
{
Xi[0][k] = 0;
}
Xi[0][ve1[0].length-1] = 1;
}
else
{
vec = new double [1][len[i] + 1];
X = new double[1][vec[0].length];
for(int k = 0; k < vec[0].length - 1; k++)
{
X[0][k] = 0;
}
X[0][vec[0].length-1] = 1;
}
}
}
When I print X, I need it have appended the values added when i=0 and when i>0. But it prints only the value of what is supposedly the final iteration. How do I make it print the data of all iterations appended to the end of data added during each iteration? I understand that since I am creating a new double[][] in each iteration, the value of gets overwritten. But how do i fix it?
You have correctly identified that with X = new double[1][vec[0].length]; you are always overriding the result from previous iterations. In order to fix it, you need to move the initialization out of the for loop. Here is an example of how you can do that:
public static void main(String [] args) {
int[] len = new int[3];
double[][] X = new double[3][];
Scanner in = new Scanner(System.in);
for (int i = 0; i < 3; i++) {
System.out.println("Enter the len " +(i+1)+":");
len[i] = in.nextInt();
X[i] = new double[len[i]];
for (int j = 0; j < len[i] - 1; j++) {
X[i][j] = 0;
}
X[i][len[i] - 1] = 1;
}
in.close();
}
As you can see, the array X is initialized in the beginning with size 3, since it will always hold 3 one-dimensional arrays in your case. However, you can initialize the size dynamically as well. You don't need the vec variable, because you can always access the length that was just read and stored in len. In fact, you don't need the len array as well, since it stores information that is anyways contained in X:
public static void main(String [] args) {
double[][] X = new double[3][];
Scanner in = new Scanner(System.in);
for (int i = 0; i < 3; i++) {
System.out.println("Enter the len " + (i+1) + ":");
X[i] = new double[in.nextInt()];
for (int j = 0; j < X[i].length - 1; j++) {
X[i][j] = 0;
}
X[i][X[i].length - 1] = 1;
}
in.close();
}

How do i output random numbers for 2 different arrays?

This is what i have so far but the output for both arrays is the same randomly generated numbers instead of different ones for each array.
import java.util.Scanner;
public class Prog2_b {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
System.out.println("Program #2b, Breanna Bergado, mascID 1885");
System.out.println("Please enter number of elements for a 1D array:");
int userVal = scnr.nextInt();
//System.out.println(((Object)userVal).getClass().getName();
double[] array = new double[userVal];
double [] array2 = new double[userVal];
double dotProd = 0;
for(int i = 0; i < userVal; i++) {
array[i] = Math.random();
array2[i] = Math.random();
}
for (int j = 0; j < 2; j++){
System.out.println("a"+(j+1)+"[]");
System.out.println("----------------");
for (int i = 0; i < userVal; i++) {
System.out.println(array[i] + " ");
}
System.out.println();
}
for(int i = 0; i < userVal; i++) {
dotProd = dotProd + array[i]* array2[i];
}
System.out.println("Dot Product is " + dotProd);
}
}
You should update your printing logic to:
for (int i = 0; i < userVal; i++) {
System.out.println(array[i] + " ");
}
System.out.println();
for (int j = 0; j < userVal; j++) {
System.out.println(array2[j] + " ");
}
Your current logic just prints the first array twice. You could also use nested arrays as one of the comments suggested, but the above code will work with what you have so far.

Categories

Resources