Why does Scanner say that there is no double to be returned? - file

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.

Related

Need help on solving this Arrays problem using java

Make an array of integer (score) with 10 members. Randomize the
content with value between 0-100. For each of the member of array,
visualize the value using “-” for each ten. For example: score[0] = 55 will be visualized as “-----" (Using Java).
public class w9lab1 {
public static void main(String args[]) {
double[] temperature = new double[7];
for (int i = 0; i < 7; i++) {
temperature[i] = Math.random()*100;
}
for (int i = 0; i < 7; i++) {
System.out.println(temperature[i]);
}
double totalTemperature = 0;
for (int i = 0; i < 7 ; i++) {
totalTemperature += temperature[i];
}
double maxTemperature = temperature[0];
for (int i = 1; i < 7; i++){
if (temperature[i] > maxTemperature){
maxTemperature = temperature[i];
}
}
System.out.println("Temperatur maximum adalah " + maxTemperature);
}
}
import java.util.Random;
import java.util.Arrays;
public class w9lab1 {
public static void main(String[] args) {
Random random = new Random();
int[] score = new int[10];
for (int i = 0; i < score.length; i++) {
score[i] = random.nextInt(101);
for (int n = 1; n <= score[i] / 10; n++)
System.out.print('-');
System.out.println();
}
System.out.println(Arrays.toString(score));
}
}

How to call runningSum function and print it?

package Package;
import java.util.Arrays;
class Cars {
public int[] runningSum(int[] nums) {
int sum = 0;
int[] res = new int[nums.length];
for (int i = 0; i < nums.length; i++) {
sum = sum+nums[0];
res[i]=sum;
}
return res;
}
public static void main(String[] args) {
int[] a = {1,2,3,4};
Cars arr = new Cars();
System.out.println(arr.runningSum(a));
}
}
//output i'm getting is [I#e580929]
//output i want is [1,3,6,10]
Am not sure what you are trying to do by sum = sum+nums[0]; after the loop. But the solution below works!
public int[] runningSum(int[] nums) {
int sum = 0;
int[] res = new int[nums.length];
for (int i = 0; i < nums.length; i++) {
sum = sum + nums[i];
res[i] = sum;
}
return res;
}
}

How can I store the results of permutation in a 2D array?

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.

Loop, iterate with unexpected results, Propertie

package javaapplication43;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Properties;
import java.io.FileInputStream;
public class JavaApplication43 {
int totalResults = 45; //
int itemsperPage = 10;
int i = 0;
int j = 0;
int count = 0;
FileOutputStream output = null;
Properties prop = new Properties();
FileInputStream input=null;
public JavaApplication43() throws FileNotFoundException, IOException {
output = new FileOutputStream("config.properties");
// set the properties value
prop.setProperty("totalResults", "45");
prop.setProperty("itemsperPage", "10");
prop.setProperty("?", "?");
// save properties to project root folder
prop.store(output, null);
input = new FileInputStream("config.properties");
// load a properties file
prop.load(input);
// get the property value and print it out
System.out.println(prop.getProperty("totalResults"));
System.out.println(prop.getProperty("itemsperPage"));
System.out.println(prop.getProperty("?"));
}
public void makeLoop() {
for (i = 1; i <= (totalResults / itemsperPage) + 1; i++) {
System.out.println("nextPage " + i);
for (; j < i * itemsperPage; j++) {
if (j > totalResults) {
break;
}
System.out.println("Filenumber " + (j + 1));
}
}
}
public static void main(String[] args) throws IOException {
JavaApplication43 myTest = new JavaApplication43();
myTest.makeLoop();
}
}
*This Code gives the Result:
nextPage1: Filnumber1, Filnumber2...Filenumber10
nextPage2: Filenumber11, Filenumber12.., Filenumber20
nextPage5: Filenumber41, Filenumber42.., Filenumber46
And so on. I expect the result so, if i start the next time with a sheduller it should start
with the nextpage2 and print the files from 11-20,
if i start again the programm it should start with the nextpage 3 and print the files from 21-30 and so on depends on the value wich i have for totalResults.
The Solution is may to save the value in the Property to make it Persistent, so that
if i run the Programm again, it will read the Property config.properties to start on the right index, but i dont know how to iterate, through the loop. ?
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Properties;
import java.io.FileInputStream;
public class JavaApplication43_with_Main_3 {
public static void main(String[] args) throws IOException {
int totalResults = 45; //
int itemsperPage = 10;
int i = 0;
int j = 0;
FileOutputStream output = null;
Properties prop = new Properties();
FileInputStream input = null;
input = new FileInputStream("config.properties");
// load a properties file
prop.load(input);
// get the property value and print it out
System.out.println("nextPage Prop " + prop.getProperty("nextPage"));
String nextPage = prop.getProperty("nextPage");
int intNextPage = Integer.parseInt(nextPage);
System.out.println("intNextPage " + intNextPage);
for (i = intNextPage; i <= (totalResults / itemsperPage) + 1; i++) {
int jNextPage=intNextPage-1;
System.out.println("nextPage here " + i);
for (j=jNextPage*itemsperPage; j < i * itemsperPage; j++) {
// System.out.println("j ist "+j);
if (j > totalResults) {
break;
}
System.out.println("Filenumber " + (j + 1));
}
String strI = "" + (i + 1);
System.out.println("hello " + strI);
output = new FileOutputStream("config.properties");
prop.setProperty("nextPage", strI);
prop.store(output, null);
break;
}
}
}
This is the does make loop and printing out
nextPage 1, Filenumber1,Filenumber2,..,Filenumber10
then it saves the nextPage value into the Property File.
If you start again, it does printing out
nextPage 2, Filenumber11,Filenumber12,...,Filenumber20
You should have e Propertiy File with the Name, config.properties
and but the key nextPage and the value 1, nextPage=1;--->config.properties

How to get the answer of the calculation from one class to appear onto another class?

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.

Resources