Need help converting a picture in byte in java [duplicate] - java

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
how to convert image to byte array in java?
Hi,
Can anybody help me convert a picture to byte array in java?
thank you

Point a File object to your picture location and then use a Scanner to read every byte. Something like:
int count=0;
File f = File("path");
Scanner sc = new Scanner(f);
while(sc.hasNextByte())
{
your_array[count] = sc.nextByte();
count++;
}
I didnt test this so dont trust me on everything

Related

Save multiple array in a String [duplicate]

This question already has answers here:
How to convert a char array back to a string?
(14 answers)
Closed 3 years ago.
My problem is I have 4 arrays, a[1]=1, a[2]=3, a[3]=4, a[4]=5, and want to save as new string/ char, so the output will be s[ ]={1345}
I try to define like this, but it doesn't works
char s[]= new char [5];
s={'a[1]','a[2]','a[3]','a[4]'};
Instead of initialising the char array s[] and then setting the value in the next line, you can directly initialize the array like: char s[] = {a[0], a[1], a[2], a[3]};
In Java the concept of String is fairly simple. You dont have to define it as a character array. Just take a String variable and concat the array values to it. The below is how you can do it.
public static void main(String[] args) {
int[] a = {1,2,3,4};
String output = a[0]+a[1]+a[2]+a[3];
System.out.println(output);
}
Hope it shall work for you.

How do I turn an input String's characters to lowercase? [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 3 years ago.
I am trying to create the following program in which I need to turn the input String's characters into lowercase to ease my work.
I tried using toLowerCase(); first but it didn't work. Then I tried using toLowerCase(locale); and yet I have not succeeded.
public static void Mensuration() {
Locale locale = Locale.ENGLISH;
Scanner inputz = new Scanner(System.in);
System.out.println("Which kind of shape's formula would you like to find out.2D or 3D?");
char dimension = inputz.nextLine().charAt(0);
if(dimension == '2') {System.out.println("Okay so which shape?");
String dimensiond = inputz.nextLine().toLowerCase(locale);
if(dimensiond == "rectangle") {System.out.println("Area = length x breadth\nPerimeter = 2(length + breadth)");}
}
}
I expected the program to give the accurate output but the thing that happens is that there is no output actually!!
use equals to compare strings. I think this causing the error
"rectangle".equals(dimensiond)

Copying Files into Arrays (Java) [duplicate]

This question already has answers here:
What's the simplest way to print a Java array?
(37 answers)
Closed 6 years ago.
I am trying to read into a file, make it into an array and print it.
I'm not sure what I'm doing wrong with this code. I've been trying to get a solution but all the solutions I can find are more advanced than what we should be using...I wrote a previous program using the same method but as a Double array. I can't seem to make it work for a String?
I keep getting [Ljava.lang.String;#3d4eac6 when I run it.
I just want it to print boysNames. boyNames.txt is just a list of 200 names in a text file.
Someone please help me, or let me know if this is possible?
this is my code so far
public static void main(String[] args) throws FileNotFoundException {
String[] boyNames = loadArray("boyNames.txt");
System.out.println(boyNames);
}
public static String[] loadArray(String filename) throws FileNotFoundException{
//create new array
Scanner inputFile = new Scanner(new File (filename));
int count = 0;
//read from input file (open file)
while (inputFile.hasNextLine()) {
inputFile.nextLine();
count++;
}
//close the file
inputFile.close();
String[] array = new String[count];
//reopen the file for input
inputFile = new Scanner(new File (filename));
for (int i = 0; i < count; i++){
array[i] = inputFile.nextLine();
}
//close the file again
inputFile.close();
return array;
}
`
Java arrays don't override Object.toString(), so you get a generic [Ljava.lang.String;#3d4eac6 (or similar) when you try to print it out using System.out.println(). Instead loop over the array and print each value.
for (String boyName : boyNames) {
System.out.println(boyName);
}
Alternatively, you could use Arrays.toString():
System.out.println(Arrays.toString(boyNames));
Another alternative would be to use String.join() (new for Java 8)
System.out.println(String.join("\n", boyNames));

How to convert JOptionPane String Input into an Int? [duplicate]

This question already has answers here:
How do I convert a String to an int in Java?
(47 answers)
Closed 6 years ago.
I am beginning to learn Java and I am stuck on how to receive the user's input as a String and converting it to an int afterwards so I can use If Statements. Thank you for reading guys, good programming for all.
Try this:
JOptionPane pane // your control
int result = Integer.parseInt(pane.getInputValue().toString());
System.out.println("result = " + result);

How to convert component of String[] or Object[] to other type [duplicate]

This question already has answers here:
How do I convert a String to an int in Java?
(47 answers)
Java - Convert integer to string [duplicate]
(6 answers)
Closed 8 years ago.
I have knowledge of wrapper class methods, but I want to know how to convert the component of String[] or Object[] to other type like int,float or Date format. for example
BufferedReader br = new BufferedReader(new FileReader(csvfile));
String curr;
while((scurr=br.readLine())!null) {
String[] input = scurr.split(",");
}
Now I want assign the component of String to primitive type(assume that my input string contains integer value)
but when I am trying to do
int i = input[0]
I am getting following suggestions:
1. change the type of i to String or
2. change the type of input to int
Is there any way to tackle the above scenario
edit:
I am really sorry guys, I really don't want ask duplicate questions, but after going through your answers and analyzing my scenario, I understood my mistake. So I would like to delete this post. How to do that without impacting the community please guild me
You can use Integer.parseInt() without an issue.
int i = input[0] // i is an int while input[0] is a String
Now
int i=Integer.parseInt(input[0])
will convert String to int
Integer.parseInt()

Categories

Resources