I'm working on a java program using arrays and loops to create a table, however when the values print they are followed by "java.io.PrintStream#1909752" repeating over and over a number of times
The chunk of code causing the error is as follows, more specifically the "row +=" sections. Any help for how to get rid of the repeated part at the end would be appreciated.
for ( int i = starting; i <= ending; i+= 1){
row += System.out.format("%6d" + ": ", i);
for ( int j = 0; j <= 11; j+=1){
double answer = i*octaveArray[j];
row += System.out.format("%.0f ", answer );
}
System.out.printf(row);
System.out.println("");
}
From the documentation of PrintStream#format():
Writes a formatted string to this output stream using the specified format string and arguments.
That means that PrintStream#format() will write the values to the output stream but you then append its toString representation which looks like java.io.PrintStream#1909752 to the row variable which you then print out to the same output stream.
You should use String.format() instead if you wish to append the formatted result to a String variable.
Related
I wanted to have a List with Unicode Strings, but I wondered if I could use a for loop instead of adding 9 variables by hand. I tried the following code, but it didn't work.
List<String> reactions = new ArrayList<>();
for (int i = 1; i < 10; i++) {
reactions.add("\u003" + i + "\u20E3");
}
My IDEA gives me an 'illegal unicode escape' error.
Is there an other way to accomplish this?
The easiest way to convert a number to a character within a string is probably using a Formatter, via String.format:
List<String> reactions = new ArrayList<>();
for (int i = 1; i < 10; i++) {
reactions.add(String.format("%c\u20e3", 0x0030 + i));
}
Assuming you want to display the character \u003i, with ifrom 1 to 9, and \u20E3, remember a character is like a number and can be used in mathematical operation.
get the character \u0030: '\u0030'
add i : '\u0030' + i
concatenate the new character with the other one (as a string)
Then print the result:
System.out.println((char)('\u0030' + i) + "\u20E3");
I have a program where it asks the user for how many numbers to be sorted then randomly generates the amount of numbers the user asks and sorts it. This is my first attempt at using swing on java so im not sure how to go about one of my features. When the user press's sort there are 2 text fields. One is the array of the unsorted numbers and the other field will be where the sorted numbers go. However when I enter the amount of numbers then press sort I should expect the numbers with a comma. Maybe im using the wrong command for the text field but I cant figure it out. I think only the last number is coming up and is the only one that appears.
//get data
String data = txtInput.getText();
//parse for numerical value
int numGenerate = Integer.parseInt(data);
int Numbers[]=new int[numGenerate];
for (int x=0;x<=Numbers.length-1;x++)
{
Numbers[x]=(int)(Math.random()*1000)+1;
txtUnsorted.setText(String.valueOf(Numbers[x])+",");
}
Try this one.
txtUnsorted.setText(txtUnsorted.getText() + String.valueOf(Numbers[x])+ "," );
First of all don't name variable with a capital letter. Only classes should name with a capital letter.
Second thing, you don't have to parse int to String to print it.
In method setText you put only a number and a comma. You should also put existing value of this field:
txtUNsorted.setText(txtUnsorted.getText() + ", " + numbers[x]);
setText() method replaces the current text.
So get first txtUnsorted.getText() then assign to variable. Use setText() afterwards.
var unsorted = txtUnsorted.getText();
txtUnsorted.setText(unsorted + String.valueOf(Numbers[x])+",");
As czachodym said don't begin a variable with an Uppercase letter. Go through this Java Document to see the rules and conventions for naming your variables.
And also I don't think you need this statement like this because Arrays are zero-based,
x <= Numbers.length - 1
Just change above to the following because following is the correct way to iterate through an array,
x < Numbers.length
So your entire code should be like this,
// get data
String data = txtInput.getText();
// parse for numerical value
int numGenerate = Integer.parseInt(data);
int Numbers[] = new int[numGenerate];
for (int x = 0; x < Numbers.length; x++)
{
Numbers[x] = (int)(Math.random() * 1000) + 1;
txtUnsorted.setText(Numbers[x] + "," + txtUnsorted.getText()); // or
txtUnsorted.setText(txtUnsorted.getText() + Numbers[x] + ",");
}
I have a task which involves me creating a program that reads text from a text file, and from that produces a word count, and lists the occurrence of each word used in the file. I managed to remove punctuation from the word count but I'm really stumped on this:
I want java to see this string "hello-funny-world" as 3 separate strings and store them in my array list, this is what I have so far , with this section of code I having issues , I just get "hello funny world" seen as one string:
while (reader.hasNext()){
String nextword2 = reader.next();
String nextWord3 = nextword2.replaceAll("[^a-zA-Z0-9'-]", "");
String nextWord = nextWord3.replace("-", " ");
int apcount = 0;
for (int i = 0; i < nextWord.length(); i++){
if (nextWord.charAt(i)== 39){
apcount++;
}
}
int i = nextWord.length() - apcount;
if (wordlist.contains(nextWord)){
int index = wordlist.indexOf(nextWord);
count.set(index, count.get(index) + 1);
}
else{
wordlist.add(nextWord);
count.add(1);
if (i / 2 * 2 == i){
wordlisteven.add(nextWord);
}
else{
wordlistodd.add(nextWord);
}
}
This can work for you ....
List<String> items = Arrays.asList("hello-funny-world".split("-"));
By considering that you are using the separator as '-'
I would suggest you to use simple split() of java
String name="this-is-string";
String arr[]=name.split("-");
System.out.println("Here " +arr.length);
Also you will be able to iterate through this array using for() loop
Hope this helps.
I am not sure how to interpret the format method down below of the String class,ugh so confused, could someone help me with me recognize it so i can use it.
StringBuilder sb = new StringBuilder();
String[] s = new String[] { "quit", "add", "delete", "find", "change" };
for (int i = 0; i < s.length; i++) {
sb.append(String.format("| %d:%s |", i, s[i]));
}
for (int i = 0; i < s.length; i++) {
sb.append(String.format("| %d:%s |", i, s[i]));
}
%d is a placeholder for numbers you just place it where you want to use your number.
%s is a placeholder for a string.
String.format("| %d:%s |", i, s[i])
first run string looks like this
"| 0:quit|"
each time you loop you just go trought array of strings s. And decimal is used for showing its location in array.
Formating string can be very usefull and it make your strings rather more elegant and easyer to read :)
Basically, %d represents a placeholder of numbers ("Formats the argument as a decimal integer") and %s is a placeholder for String
The command will supplement for the value of i where %d is and s[i] for where %s is...
Check out Formatter for more details
You could also check out How to format String in Java – String format Example which goes into more detail
for (int j =0; j < marks.size(); j++) {
analyzeTextArea.setText(j + marks.get(j));
}
The above code gives me the following error:
required: java.lang.String found: int
I guess marks.get(j) give you an Integer. So when you do j + marks.get(j) you add the value of marks.get(j) to the value of j.
So you end with an Integer as result of j + marks.get(j). But setText expect a String.
You have several possibilities now depending on you needs.
analyzeTextArea.setText(Integer.toString(j + marks.get(j)));
This case still make the addition then convert it to String in order to respect setText parameter type.
With this :
analyzeTextArea.setText("" + (j + marks.get(j)));
"" tells that the parameter will be a String and then you will concatenate j and marks.get(j). So, for example, for the first loop you will have something that start with 0
Now using setText in a loop don't really make sense because only the last value set in the loop will be used you probably should use JTextArea#append(String).
You need to do something like this:
analyzeTextArea.setText("" + (j + marks.get(j)));
analyzeTextArea.setText(Integer.toString(j + marks.get(j)));
Try this,
for (int j =0; j < marks.size(); j++) {
analyzeTextArea.setText(j + marks.get(j)+"");
}
That should work but instead of .setText(), you should use .append(). because .setText() deletes the previous contents and writes it. but .append() just adds on information