how to call httpconeection setRequestProperty android in loop to setHeader? - java

HTTPConnection conn method
setRequestProperty
conn.setRequestProperty("","");
I have different header for different URL. So, it's not fix in my project.
I need to use a array to fill the setRequestproperty data.
Individual call for setRequestProperty it's working 1stPart .
I tried to call same into Array it's not work "part 2".
1) conn.setRequestProperty("Authorization","12345678");
conn.setRequestProperty("ReToken", "erjeorjeorjeoureorjr");
2)
String[] array1 = new String[]{"Authorization","12345678","RefreshToken","erjeorjeorjeoureorjr"};`
if (array1 != null) {
int size = array1.length;
for (int i = 0; i < size; i = i + 2) {
conn.setRequestProperty('"' + array1[i] + '"',
'"' + array1[i + 1] + '"');
Log.d(TAG,"Value Print:: " + array1[i] + " ," +
array1[i+1] );
}
}

You are trying to convert something that is already a string into a string. Just remove the double quotes around the array.
Try this:
for (int i = 0; i < size; i = i + 2) {
conn.setRequestProperty(array1[i] ,array1[i + 1]);
Log.d(TAG,"Value Print:: " + array1[i] + " ," + array1[i+1] );
}

Related

Error on the for loop - trying to use loop to count the repetition of letter

On the for loop I have the Java applet is showing me that I have an error. I am trying to use the for loop to count the repetition of letter.
String countString = "";
for (int i = 0; i < 26; i++){
// at the line below, my java applet says I have an error, and that the
//"letterCounts" should be a int and not a string, but I need it to be a string
String n = letterCounts[i];
if (n.equals("0")) {
countString = countString + " ";
} else if (n.length() == 1) {
countString = countString + " " + n + " ";
} else {
countString = countString + n + " ";
}
}
this.countLabel.setText(countString);
You donot show the definition of letterCounts, but I bet it is int[] letterCounts.
So since letterCounts is an array of int, you cannot just assign it to a String.
Just change String n to int n and your comparison to n == 0 and it should work. See below:
String countString = "";
for (int i = 0; i < 26; i++)
{
int n = letterCounts[i];
if (n == 0) {
countString = countString + " ";
} else if (n < 10) {
countString = countString + " " + n + " ";
} else {
countString = countString + n + " ";
}
}
this.countLabel.setText(countString);

returning ArrayIndexOutOfBoundsException error for unknown reason

I am trying to create a basic bubble sort program but at some point the array tries to reference the 11th position despite the array being 10 long and i am not sure when it occurs
int Last, i = 0, Temp;
int[] Numbers = new int[10];
String[] NumbersString = new String[10];
String initialString = TextBox.getText();
NumbersString = initialString.split(" ");
while(i<10){
Numbers[i] = Integer.parseInt(NumbersString[i]);
i = i + 1;
}
Last = 9;
Boolean Swapped = true;
while(Swapped = true) {
Swapped = false;
i = 0;
while(i < Last) {
if(Numbers[i] > Numbers[i+1]){
Temp = Numbers[i];
Numbers[i] = Numbers[i+1];
Numbers[i+1] = Temp;
Swapped = true;
}
i = i + 1;
}
Last = Last-1;
}
String Result = Numbers[0] + " " + Numbers[1] + " " + Numbers[2] + " " + Numbers[3] + " " + Numbers[4] + " " + Numbers[5] + " " + Numbers[6] + " " + Numbers[7] + " " + Numbers[8] + " " + Numbers[9];
ResultText.setText(Result);
change
while(Swapped = true) {
to
while(Swapped == true) {
What is happening is that Last is firstly wrapping to Negative and then when it gets to the minimum negative number it wraps to Integer.MAX_VALUE and then i will exceed 9
just try to put the length of the Array in here.
int Last, i = 0, Temp;
int[] Numbers = new int[10];
String[] NumbersString = new String[10];
String initialString = "1";
NumbersString = initialString.split(" ");
while (i < NumbersString.length) { // <-- I change it to NumbersString.length
Numbers[i] = Integer.parseInt(NumbersString[i]);
i = i + 1;
}
Last = Numbers.length-1; // <-- I change it to Numbers.length-1
Boolean Swapped = true;
while (Swapped) { // <-- I change it to Swapped only because your Swapped is a Boolean type, no need to equals it into true.
Swapped = false;
i = 0;
while (i < Last) {
if (Numbers[i] > Numbers[i + 1]) {
Temp = Numbers[i];
Numbers[i] = Numbers[i + 1];
Numbers[i + 1] = Temp;
Swapped = true;
}
i = i + 1;
}
Last = Last - 1;
}
String Result = Numbers[0] + " " + Numbers[1] + " " + Numbers[2] + " " + Numbers[3] + " " + Numbers[4] + " "
+ Numbers[5] + " " + Numbers[6] + " " + Numbers[7] + " " + Numbers[8] + " " + Numbers[9];
System.out.println(Result);
}

java StringIndexOutOfBoundsException: String index out of range: 0

Its a very simple function. I don't know why it is giving an StringIndexOutOfBoundsException for the function source.charAt(i). I have checked the values of 'i' are not exceeding the length of the string which will be always 9.
public static String getClockResetString(String source, String target, Hashtable order)
{
String temp = "",name;
for(int i = 0; i < source.length(); i++){
name = (String)order.get(""+i);
if(source.charAt(i) != target.charAt(i))
{
if((int)source.charAt(i) < (int)target.charAt(i)){
temp = "h" + name + "=" + "dp" + name + "0" + " do " + "{h" + name + "'=0, k'=k+1} ";
}
else{
temp = "h" + name + "=" + "dn" + name + "1" + " do " + "{h" + name + "'=0, k'=k+1} ";
}
}
}
return temp;
}
I bet that you are exceeding the bounds on target, not source
if(source.charAt(i) != target.charAt(i))
You need to check that
i < target.length()

Incorrect logics

So the point of this method is to get an array of temperatures above 100. What is wrong with this? When I return this in my toString it says blazing[] doesnt exist.
public int[] above100Degrees()
{
int[] blazing = new int[temps.length];
for( int i = 0; i < temps.length; i++ )
{
if( temps[i] > 100 )
{
blazing[i] = temps[i];
}
}
return blazing;
}
toString method:
public String toString()
{
String returnString = "The temperature forecast of week " + WEEK + " is logged in as: ";
for( int i = 0; i < temps.length; i++ )
{
returnString += "\t" + temps[i] + "\t";
}
returnString += "\n" + "The number of temperatures below freezing is " + getUnderFreeze() + "." + "\n" +
"The largest difference this week was a net change of " + NetChange() + ".";
for( int i = 0; i < blazing.length; i++ )
{
returnString += "The temperature above 100 degrees is " + above100Degrees() + "." + "\n" + "\t" + blazing[i] + "\t";
}
return returnString;
}
Output
Forecast.java:122: error: cannot find symbol
for( int i = 0; i < blazing.length; i++ )
^
symbol: variable blazing
location: class Forecast
Forecast.java:124: error: cannot find symbol
returnString += "The temperature above 100 degrees is " + above100Degrees() + "." + "\n" + "\t" + blazing[i] + "\t";
^
symbol: variable blazing
location: class Forecast
2 errors
The array is returned by the above100Degrees method. It does not establish the variable name blazing in the scope in which it's called. In fact, you can assign the returned array to a variable named differently.
Try
int[] reallyHot = above100Degrees();
// Then check reallyHot...
for( int i = 0; i < reallyHot.length; i++ )
{
returnString += "The temperature above 100 degrees is " + reallyHot[i] + "." + "\n";
}
Make sure to access the specific element with array access syntax.
Change :
for( int i = 0; i < blazing.length; i++ )
{
returnString += "The temperature above 100 degrees is " + above100Degrees() + "." + "\n" + "\t" + blazing[i] + "\t";
}
to :
int[] blazing = above100Degrees();
for( int i = 0; i < blazing.length; i++ )
{
returnString += "The temperature above 100 degrees is " + blazing[i] + "."; // personalise format
}
Your above100Degrees() function returns the array blazing but you don't invoke that function in your toString() method. As far as toString() is concerned blazing doesn't exist.
Call above100Degrees() from within toString() and save the resulting array to a variable. Then you'll be able to iterate over the (newVariable).length instead of blazing.length.

NullPointer Exception Jcreator, Java

I am having an incredibly difficult time trying to figure out why I am getting this error.
When I use a driver file to test the program it fails horribly.
Here's my code:
import java.util.Scanner;
import java.lang.Math.*;
public class Histogram
{
private int[] arrayData;
private int[] arrayRange;
private final int LOW = 1;
private final int HIGH = 100;
public Histogram()
{
int[] arrayData = new int[11];
}
public void getInput()
{
int[] arrayRange = new int[11];
for(int count = 1; count < arrayRange.length; count++)
{
arrayRange[count] = count * 10;
}
Scanner input = new Scanner(System.in);
System.out.println("Enter numbers from 1 to 100, Type -999 to quit.");
int nextNumb = input.nextInt();
while(nextNumb != -999)
{
if(nextNumb >= LOW && nextNumb <= HIGH)
{
for(int i = 0; i <= arrayRange.length; i++)
{
if(nextNumb > arrayRange[i] && nextNumb <= arrayRange[i+1])
arrayData[i]++;
}
nextNumb = input.nextInt();
}
else arrayData[10]++;
nextNumb = input.nextInt();
}
}
public String starPrint(double count)
{
String star = "";
count = (Math.round(count) / 5);
for(int i = 1; i <= count; i++)
{
star = star + "*";
}
return star;
}
public String toString()
{
String results = " Range | Histogram" + "\n";
results = results + "1 - 10 | " + starPrint(arrayData[0]) + "\n";
results = results + "11 - 20 | " + starPrint(arrayData[1]) + "\n";
results = results + "21 - 30 | " + starPrint(arrayData[2]) + "\n";
results = results + "31 - 40 | " + starPrint(arrayData[3]) + "\n";
results = results + "41 - 50 | " + starPrint(arrayData[4]) + "\n";
results = results + "51 - 60 | " + starPrint(arrayData[5]) + "\n";
results = results + "61 - 70 | " + starPrint(arrayData[6]) + "\n";
results = results + "71 - 80 | " + starPrint(arrayData[7]) + "\n";
results = results + "81 - 90 | " + starPrint(arrayData[8]) + "\n";
results = results + "91 - 100 | " + starPrint(arrayData[9]) + "\n";
results = results + "Outliers: " + starPrint(arrayData[10]) + "\n";
return results;
}
}
I believe that the problem is in my getInput method
right here to be precise:
if(nextNumb > arrayRange[i] && nextNumb <= arrayRange[i+1])
arrayData[i]++;
I have no idea what's wrong with it though I am a beginner programmer and couldn't find a solution to this particular problem.
Thanks for any help you're able to give!
public Histogram()
{
int[] arrayData = new int[11];
}
You're shadowing your arrayData field in the constructor. This is creating a local variable with the same name as your class's arrayData field, initializing it, then immediately discarding it. When you try to use the field later in your code, it's null. Get rid of the int[] part.
Note that your next exception will be an ArrayIndexOutOfBoundsException ... you should look at your loop ;)
this: for(int i = 0; i <= arrayRange.length; i++)
wont work since you are trying to access arrayRange[i] and arrayRange[i+1]
which dont exists for i = arrayRange.length-1 and further
so change it to:
for(int i = 0; i < arrayRange.length-1; i++)

Categories