i try to write on label an array with methods. When i try to put values of array it writes system int 32 on label, here is the code and how can put values on label
p.s. sorry about bad English
enter code here void arr_5( int[] mas5)
{
for (int i=0, j=5; i<10; i++, j+=5)
{
mas5[i] = j;
}
}
private void button1_Click(object sender, EventArgs e)
{
int[] a = new int[10];
arr_5 (a);
label1.Text += a.ToString() + " ";
}
That's because an array contains more than one value and in order to show it you could do something like:
int[] arr_5( int[] mas5)
{
for (int i=0, j=5; i<10; i++, j+=5)
{
mas5[i] = j;
}
return mas5;
}
private void button1_Click(object sender, EventArgs e)
{
int[] a = new int[10];
arr_5 (a);
String label="";
for(int i=0; i<a.length; a++)
{
label= label + a +" ";
}
label1.Text =label;
}
Please be aware that i've changed your arr_5 to return the newly created array.
Related
I have an array with 5 integer values.
I need to check each value in it against input values from a text box.
A label displays a Value found message if the input value exists in the array.
If not, theValue not found message is displayed.
How do I display the Value not found message correctly?
Here's my code,
private void button1_Click(object sender, EventArgs e)
{
try
{
int[] id = { 1, 2, 3, 4, 5 };
int row;
int x = Convert.ToInt32(txtid.Text);
for (row = 0; row < id.Length; row++)
{
if (id[row] == x)
{
txtdesc.Text = "Value found!";
}
}
txtdesc.Text = "Value not found!";
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Without going into great detail, using a flag is the simplest way of tracking a Boolean state when you are first starting out.
private void button1_Click(object sender, EventArgs e)
{
try
{
int[] id = { 1, 2, 3, 4, 5 };
int row;
int x = Convert.ToInt32(txtid.Text);
bool found = false;
for (row = 0; row < id.Length; row++)
{
if (id[row] == x)
{
found = true;
}
}
txtdesc.Text = found ? "Value found!" : "Value not found!";
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
With LINQ, these things get a lot easier:
// At the top of the file
using System.Linq;
private void button1_Click(object sender, EventArgs e)
{
try
{
int[] id = { 1, 2, 3, 4, 5 };
int x = Convert.ToInt32(txtid.Text);
txtdesc.Text = id.Contains(x) ? "Value found!" : "Value not found!";
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
private void button1_Click(object sender, EventArgs e)
{
try
{
int[] id = { 1, 2, 3, 4, 5 };
int row;
int x = Convert.ToInt32(txtid.Text);
for (row = 0; row < id.Length; row++)
{
if (id[row] == x)
{
txtdesc.Text = "Value found!";
return; //only add this line
}
}
txtdesc.Text = "Value not found!";
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
I need help writing this code to get the permutation of numbers.
I need to store all the permutations in a 2D array.
After output of the permutation, I then need to process 30 percent of the permutations in one method an the the rest in another method.
My code:
public class Permutation {
* #param args the command line arguments
*/
void printArray(int []a) {
for (int i = 0; i< a.length; i++) {
System.out.print(a[i]+" ");
}
System.out.println("");
}
void permute(int []a,int k ) {
if(k==a.length)
printArray(a);
else
for (int i = k; i< a.length; i++) {
int temp=a[k];
a[k]=a[i];
a[i]=temp;
permute(a,k+1);
temp=a[k];
a[k]=a[i];
a[i]=temp;
}
}
public static void main(String[] args) {
Permutation p=new Permutation();
int a[]={1,2,3,4,5,6};
p.permute(a, 2);
}
}
This is my solution instead of a 2d array use an ArrayList
import java.util.ArrayList;
import java.util.Arrays;
/**
*
* #author David
*/
public class Permutations {
public static int[] a;
public final int SIZE = 6;
public final int NUMPERM;
public final ArrayList<int[]> newlist;
public Permutations()
{
a = new int[SIZE];
for(int x = 0; x < SIZE; x++)
a[x] = x+1;
NUMPERM = Factorial(a.length);
newlist = new ArrayList<>(NUMPERM);
}
public void permute()
{
permutation(a,0,a.length);
}
private void permutation(int array[],int start, int end)
{
newlist.add(saveArray(array));
if (start<end)
{
int i,j;
for(i=end-2; i>=start; i--)
{
for(j=i+1; j<end; j++)
{
Swap(array,i,j);
permutation(array,i+1,end);
}
Rotate_Left(array,i,end);
}
}
}
private int[] saveArray(int[] array)
{
int[] newarray = new int[array.length];
System.arraycopy(array, 0, newarray, 0, array.length);
return newarray;
}
public void Print()
{ //just to prove the list works
System.out.println("the current size of newlist is : " + newlist.size());
int[] array = new int[a.length];
for(int x = 0; x < newlist.size(); x++)
{
array = newlist.get(x);
System.out.println(Arrays.toString(array));
}
}
private void Swap(int array[],int i,int j)
{
int t;
t = array[i];
array[i] = array[j];
array[j] = t;
}
private void Rotate_Left(int array[],int start,int end)
{
int tmp = array[start];
for (int i=start; i < end-1; i++)
{
array[i] = array[i+1];
}
array[end-1] = tmp;
}
private int Factorial(int a)
{
int fact = 1;
for(int x = a; x > 0; x++)
fact *= a;
return fact;
}
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Permutations newperm = new Permutations();
newperm.permute();
newperm.Print();
}
}
then all you have to do is send the list to the other functions and only use what you need from it.
I have the code. Some of names are in Polish, but I think it's not a problem.
In this code I create randomly filled array, write it to the .txt file and then create another array using data read from this file. Unfortunately in static method hasNextDouble() method return false and my array is filled with 0.0's. Why is that so? If I open this .txt file via notepad it contains doubles that can be read. Where is a problem?
package algorytmyz1;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.util.Random;
import java.util.Scanner;
class Tablica {
private int w;
private int k;
private double[][] tab;
//konstruktor
public Tablica(int w,int k) {
this.w = w;
this.k = k;
tab = new double[w][k];
Wypelnij();
Zapisz();
}
//wypelnia macierz liczbami od 0 do 9
private void Wypelnij(){
Random rand = new Random();
for(int i=0; i<w; i++)
for(int j=0; j<k; j++)
//generuje pseudolosowe liczby rzeczywiste między 0 a 10
tab[i][j]=rand.nextDouble() * 10;
}
private void Zapisz() {
try (PrintWriter fout = new PrintWriter("macierz1.txt"))
{
fout.println("Macierz:");
fout.println(w);
fout.println(k);
for (int i=0; i<w; i++)
{
for (int j=0; j<k; j++)
{
fout.print(tab[i][j] + " ");
}
fout.println();
}
}
catch (IOException e) {
System.out.println("Błąd wejścia/wyjścia.");
}
}
}
public class AlgorytmyZ1 {
public static void main(String[] args) throws FileNotFoundException {
int w,k;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//wprowadzenie liczby wierszy i kolumn macierzy
try {
System.out.println("Podaj liczbę wierszy:");
w = Integer.parseInt(br.readLine());
System.out.println("Podaj liczbę kolumn:");
k = Integer.parseInt(br.readLine());
Tablica macierz = new Tablica(w,k);
}
catch (IOException e) {
System.out.println(e);
}
WczytajWyswietl();
}
public static void WczytajWyswietl() throws FileNotFoundException {
int w, k;
String naglowek;
double[][] tablica;
File file = new File("macierz1.txt");
Scanner fin = new Scanner(file);
naglowek = fin.nextLine();
w = Integer.parseInt(fin.nextLine());
k = Integer.parseInt(fin.nextLine());
tablica = new double[w][k];
while (fin.hasNextDouble())
{
for (int i=0; i<w; i++)
{
for (int j=0; j<k; j++)
tablica[i][j] = fin.nextDouble();
}
}
System.out.println(naglowek);
System.out.println("Liczba wierszy: " + w);
System.out.println("Liczba kolumn: " + k);
for (int i = 0; i<w; i++)
{
for (int j=0; j<k; j++)
{
System.out.print(tablica[i][j] + " ");
}
System.out.println();
}
}
}
The correct answer was that I had to use useLocale(Locale.English) method on my scanner object. I don't know which Locale was set before, but with english everything runs smooth.
I post this answer to my own post because maybe it will be helpful for someone in the future.
Can anyone teach me the coding on how to to get the answer of this coding to appear onto another class?
public class BubbleSort4
{
public static void main(String[] args) {
int intArray[] = new int[]{5,90,35,45,150,3};
System.out.println("Array Before Bubble Sort");
for(int i=0; i < intArray.length; i++)
{
System.out.print(intArray[i] + " ");
}
bubbleSort(intArray);
System.out.println("");
System.out.println("Array After Bubble Sort");
for(int i=0; i < intArray.length; i++)
{
System.out.print(intArray[i] + " ");
}
}
public static void bubbleSort(int[] intArray)
{
int n = intArray.length;
int temp = 0;
for(int i=0; i < n; i++)
{
for(int j=1; j < (n-i); j++)
{
if(intArray[j-1] > intArray[j])
{
temp = intArray[j-1];
intArray[j-1] = intArray[j];
intArray[j] = temp;
}
}
}
}
}
Coding answer:
Array after Bubble Sort
5 90 35 45 150 3
Array After Bubble Sort
3 5 35 45 90 150
I think you have to change the structure of you class. Make the intArray a class field:
public class BubbleSort4 {
static int intArray[] = new int[]{5,90,35,45,150,3};
public static void main(String[] args) {
...
Have a method that returns the sorted array:
public static int[] getSortedArray() {
bubbleSort(intArray);
return intArray;
}
And now you can call BubbleSort4.getSortedArray() from any class, and the sorted array will be returned. Hope this helps.
i want to insert double dimensional array into ms-access dynamically in java ..
here is my code..
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String url = "jdbc:odbc:Driver={Microsoft Access Driver " +
"(*.mdb, *.accdb)};DBQ=C:\\Documents and Settings\\ANIL KUMAR\\Desktop\\hyperdata.mdb";
con = DriverManager.getConnection(url);
System.out.println("Connected!");
}
catch (SQLException e) {
System.out.println("SQL Exception: "+ e.toString());
}
catch (Exception e) {
e.printStackTrace();
}
if i have a string array with two columns:
String[][] a = new String[10][2];
PreparedStatement pst = con.prepareStatement("INSERT INTO sap_details VALUES (?,?)");
for (int i = 0; i < 10; i++) {
pst.setString(1, a[i][0]);
pst.setString(2, a[i][1]);
pst.addBatch();
}
pst.executeBatch();
what if have a have string array with n columns and n rows?
how to insert string array a[n][n]?
Have an inner for loop
for (int i=0; i<a.length; i++) {
for (int j=0; j<a[i].length; j++) {
System.out.print(a[i][j]);
}
}
You can also use enhanced loop as below
for (String[] array : a) {
for (String s : array) {
System.out.println(s);
}
}