Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Suppose I have a txt file called "filename". The data inside is as following,
N
12 39
34 23
12 22
5 7
7 10
11 8
.
.
.
left column contains the x value of each point. Right column contains y value of each point. N is the number of lines Point data that follow. I need to extract all the Point data and store it in a data structure(such as List). Is there any way I can do that?
File file = new File(filepath);
BufferedReader br = new BufferedReader(file.getInputStream);
int n = Integer.parseInt(br.readLine());
for (int i = 0; i < n-1; i++) // reads n-1 points, if you have n points to read, use n instead of "n-1"
{
line = br.readLine();
StringTokenizer t = new StringTokenizer(line, " ");
int x = Integer.parseInt(t.nextToken());
int y = Integer.parseInt(t.nextToken());
// do whatever with the points
}
This would work for something like this as an input file,
3 // line 1
1 2 // line 2
3 4 // line 3
My solution using Scanner instead of BufferedReader/StringTokenizer:
Scanner scanner = new Scanner(new File("filename"));
int n = scanner.nextInt();
for (int i = 0; i < n; i++) {
int x = scanner.nextInt();
int y = scanner.nextInt();
// do something with the point or store it
}
It's probably not as fast, but it's much easier to read and write.
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I have a problem with arrays and assigning them to values like float. Here is my code
System.out.print("Please enter the number of iteration: ");
Scanner scn = new Scanner(System.in);
int inumber = scn.nextInt(); // i get the iteration number
Random rnd = new Random();
float x = -10 + rnd.nextFloat() * 20; // i created random number between 10 and -10
System.out.println("My x = " +x);
float[] xarray = new float[4]; // created my test array
Random arrayrnd = new Random();
for (int i=0;i<inumber;i++) { // created a for loop with that number
for (int j = 0; j<xarray.length;j++) {
xarray[j] = arrayrnd.nextFloat(); // also created random for array and assigned them
}
Arrays.sort(xarray); // i sorted the array
xarray[0] = x; // i tried to assign the smallest number to x but didn't work
System.out.println("t="+i+" My new x = " +x);
Also here is my output :
Please enter the number of iteration: 2
My x = -6.2841988
t=0 My new x = -6.2841988
t=1 My new x = -6.2841988
I just don't understand why my x hasn't changed even though I tried to assign the x with the new value. I want my x to change in every turn of the loop and got the the smallest number of the array. But it seems like x never wants to move. I'm sorry if my code is complicated or if I have any mistake. Happy coding!
If you want assign TO x, you should do:
x = xarray[0];
Instead of:
xarray[0] = x;
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 6 years ago.
Improve this question
I wanted to see better code/logic of the below questions from you guys.
Que: Say, there are two arrays - Input and Output array. Taking 4 elements in input array. O/p array will also have length of same as Input array. Output should have - first index value should product of all values from the rest of indexes except first. Similarly, second index value should have product of all other values from the rest of indexes except second. Likewise, the product should go till output array get the same length as that of input's. I have written code but I wanted to see different answers from different viewers. More customized and use of library functions is helpful. Customizing my code also is welcome.
System.out.println("Enter the size of the array..");
scan1 = new Scanner(System.in);
int n = scan1.nextInt();
int[] arr = new int[n];
System.out.println("Enter the array elements..");
for (int i = 0; i<n; i++)
arr[i] = scan1.nextInt();
System.out.println("Array is.." +Arrays.toString(arr));
int[] arr1 = new int[arr.length];
for (int i = 0;i<arr.length;i++){
int sum = 1, j = 0;
while (j<arr.length){
if(i==j)
System.out.println("I and J are equal");
else
sum = sum * arr[j];
j++;
}arr1[i] = sum;
}
System.out.println(Arrays.toString(arr1));`
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
Say I have an already existing CSV file on my filesystem
0 red
1 orange
2 yellow
3 green
4 blue
I would like to update these results to that the first column remains, but the second column shifts up by one point like so:
0 orange
1 yellow
2 green
3 blue
4 (some randomly generated color)
Is this possible to do with Java? I don't want to write to another file.. just update the one I am reading from. Any help is appreciated. Thank you.
Edit to show current output
02 orange
03 yellow
04 green
05 blue
0 random
(Note: 0 and random are in the same column)
Scanner scan = new Scanner(new File("csv.txt")); //or whatever the file name is
int[] numbers = new int[5];
String[] colors = new String[5];
int i = 0;
while (scan.hasNextLine()) {
String line = scan.nextLine();
Scanner s = new Scanner(line);
if (s.hasNextInt()) {
numbers[i] = s.nextInt();
if (s.hasNext()) colors[i] = s.next();
}
s.close();
i++;
}
scan.close();
PrintWriter output = new PrintWriter("csv.txt");
for (i = 0; i < numbers.length; i++) {
output.print(numbers[i] + " ");
if (i + 1 < numbers.length) output.println(colors[i + 1]);
else output.println(/* random color */);
}
output.close();
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
The code can be found here: https://sites.google.com/site/indy256/algo/kuhn_matching2
More specifically, I'm not sure I understand what the int n2 is for and how it is being used.
Here, for bipartite graphs, n1 denotes number of vertices of the first set (partition) and n2 denotes number of vertices of the second set.
E.g. you would have a set of workers and a set of tasks they would perform. In the example above, there are 2 workers (say John=0 and Bob=1) and three tasks (say, coding=0, QA=1, support=2).
John can do coding and support. Bob can do only support. None of them can do QA (there is no g[i] == 1)
Then the algorithm follows Kuhn's proposal to find a maximum matching (do not confuse with maximal matching). In our example case, maximum matching has two edges (e.g. John->coding and Bob->support).
The algorithm above would not work for weighted bipartite graphs.
Update
To accommodate the input rules from the question, following needs to be done. Simpy ensure that in g[x]=y : 0 <= x < n1 and 0 <= y < n2
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n1 = sc.nextInt();
int n2 = sc.nextInt();
int m = sc.nextInt();
LinkedList<Integer>[] g = new LinkedList[n1];
for (int j = 0; j < n1; j++) {
g[j] = new LinkedList<Integer>();
}
int i = 0;
while(i != m){
int v = sc.nextInt();
int v2 = sc.nextInt();
if(v>=1 && v<=n1) {
//v belongs in first set
g[v-1].add(v2-n1-1);
}else if(v>=n1+1 && v<=n1+n2) {
//v belongs in the second set, v2 into the first
g[v2-1].add(v-n1-1);
}
i++;
}
System.out.println(maxMatching(g, n2));
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I need to write a section of code that outputs the binary value up to a user defined number (0-7).
We can't use .toBinaryString it has to be using loops (for loops preferably).
The output should be three columns with filler zeros.
Ex) User enters 7
001
010
100
101
110
111
It seems like it should be so simple but I cant seem to get it right.
for (int i = 1; i <= input; i++) {
String line = "";
for (int k = 2; k >= 0; k--) {
line += ((i >> k) & 1) == 1 ? "1" : "0";
}
System.out.println(line);
}
That uses two for loops.
I would create your own toBinary() function:
int toBinary(int x){
StringBuilder sb = new StringBuilder("");
while(x >= 1){
sb.append(x%2);
x /= 2;
}
return Integer.parseInt(sb.reverse().toString());
}
Then just use that function to print:
for(int i=1; i<=7; i++)
System.out.println( String.format("%03d", toBinary(i)) );