I can't seem to get rid of previously printed lines. If I do just
AnsiConsole.out.println(ansi);
It repeats the same output every successive output. E.g.
A
AB
ABC
Even if the output is only supposed to be
A
B
C
My code is:
AnsiConsole.out.println(a);
a.eraseLine(Erase.ALL);
AnsiConsole.out.flush;
If I don't use a.eraseLine(Erase.ALL), it looks like this:
Also, if I don't use Jansi and use plain System.out, it looks like this so I know it's not a matter of code elsewhere.
Solution:
Thanks to Betlista's answer. I found out what was wrong. The thing was that my ansi variable was actually called using the Ansi constructor and not the Ansi.ansi() method. That was what was different in my code. Thanks. :)
It seems you are doing something wrong, but there is a lot of info missing in your post, that it is hard to follow...
...so I'll write how it works for me:
I'm using maven with jansi in version 1.11 tested on Windows 7.
I simply followed the info on https://github.com/fusesource/jansi and my code
package jansi;
import java.io.IOException;
import org.fusesource.jansi.Ansi;
import org.fusesource.jansi.Ansi.Color;
import org.fusesource.jansi.AnsiConsole;
public class Main {
public static void main(String[] args) throws IOException {
AnsiConsole.systemInstall();
String[] sa = {"A", "B", "C"};
Color[] ca = {Color.RED, Color.GREEN, Color.BLUE};
for (int i = 0; i < sa.length; ++i ) {
System.out.println(Ansi.ansi().eraseLine().fg(ca[i]).a(sa[i]).reset());
}
}
}
is doing exactly what I expected and this is the result:
Related
First let me clear out i am new to programming and hope that i am using the right terminology.
I using the System.out.print(""); Method to print to the Windows Console:
System.out.print("Backspace b"); //Output: Backspace b
So the cursor is now "behind" the b, if i type in System.out.print("\b"); the curser moves one to the left, deleting the "b". -->
System.out.print("Backspace b"); //Output: Backspace b
System.out.print("\bh"); //Output: Backspace h
Now if i Type in System.out.print("\n\bh"); the output isn't Backspace bh but:
"Backspace b
h"
How can i manage that the cursor goes back one line "up" and to it's far right. Something line a "minus \n" or "not \n", so that it reads Backspace bh?
Is there something like that in Java?
It's impossible without third party libraries. I've done extensive research on this as I've been working on console games for the past week and from what I can tell you can either require JNI, JNA, Jansi, or JLine which will require Jansi or JNA. Personally I recommend JLine as all the others will require you to write C or C++.
If you say want to go up so many lines you could do something like this with JLine.
import org.jline.terminal.Terminal;
import org.jline.terminal.TerminalBuilder;
import org.jline.utils.InfoCmp;
import java.io.IOException;
public class CursorStuff
{
static Terminal terminal;
public static void main(String[] args) throws IOException
{
terminal = TerminalBuilder.terminal();
System.out.println("1");
System.out.println("2");
System.out.println("3");
System.out.println("This line will get overwritten by the terminal path");
goBackLines(3);
System.out.println("back 3 overwriting 2");
}
public static void goBackLines(int remove)
{
for(int i = 0; i < remove; i++)
{
terminal.puts(InfoCmp.Capability.cursor_up);
}
}
}
I got this error from running the code without the break; at the end and by searching for it that was a solution i came up with on my own but i was wondering is there a better way to handle it.
import java.util.Scanner;
public class SummanNeliojuuri {
public static void main(String[] args) {
Scanner lukija = new Scanner(System.in);
while(true) {
int luku = Integer.valueOf(lukija.nextLine());
int luku2 = Integer.valueOf(lukija.nextLine());
int summa = luku + luku2;
int neliojuuri = (int) Math.sqrt(summa);
System.out.println(neliojuuri);
break; }
}
}
Just that the code runs without the error. Not sure though that if the error comes up only because im running it through a mooc course "inspector" in netbeans.
Aaaah okay that cleared it up. Took the while loop away from it. I assumed the inspection thing in netbeans needed the loop for it for some reason. Also i didnt think that the break was needed here. Thanks a lot all of you! :)
also the error i got was: java.util.NoSuchElementException: No line found
edit: Really thankful for all the help! Was a bit afraid to post something since i knew it was a really "noobie" question. Got rid of the while loop and the break and it works great now!
I am new to programming and learning Java.
I was looking at an old code and they used something called a console. When I copy-pasted the code, Java did not read anything called a console. Is a console basically a scanner? I searched online and they said it's for input, so should I just remove all consoles and write scanner there instead?
Scanner as in writing Scanner scan = new Scanner(System.in);
I am using the latest version of Java.
(Yes, this for a game of crazy eights)
This is just a part of the old code :
// The "CrazyEights" class.
import java.awt.*;
import hsa.Console;
public class CrazyEights
{
static Console c; // The output console
static int[] deck, player, computer; // card arrays
static int pile, suit; // discard pile, current suit (0-3)
static boolean deckEmpty;
public static void main(final String[] args)
{
// setup console
c = new Console(30, 100, "Crazy Eights");
intro(); // splash page, instructions
char playAgain;
do
{
c.setFont(new Font("Arial", java.awt.Font.PLAIN, 14));
game();
c.setCursor(30, 32);
playAgain = c.getChar();
} while ((playAgain == 'y') || (playAgain == 'Y'));
c.close();
}
}
The specific Console you are asking for is in your imports.
There is an import "import hsa.Console" on the top of your code. If you are not familiar with imports, as you say you are new to programming/Java, it signifies that when you write "Console" inside this file ("CrazyEights.java") you are referencing the "Console" defined in "hsa.Console".
Now, since this class is specific to your project, we can't know what it is and does. You will have to open it and see for yourself.
However, with a quick search, you are probably using "ReadyToProgram" IDE for Java by Holtsoft and Associates and that's what the prefix hsa stands for in "hsa.Console".
If you are not using that and just copy-pasted the code expecting it to work, it won't. That import is not in the standard Java library. You will need to find it, download it and add it to your project dependencies. If you did that just to start somewhere with Java and it is not essential to you, I would suggest leaving this block of code and moving somewhere else.
I am trying to generate weibull random numbers using WeibullGen from umontreal.iro.lecuyer.randvar package this is my code
RandomStream stream = new MRG32k3a();
for(int i=0;i<5;i++) {
int result = WeibullGen.nextDouble(stream, alp, lam,bet);
System.out.println(result);
}
But I am a little confused about the parameters I mean does lam means the scale or the rate parameters, I think alp means the shape parameter. finally, I am confused about the last parameter bet what it present. I tried to look online but it wasn't clear to me.
Thanks in advance
I apologize in advance for the similarity between this question and many others, but after checking through and trying solutions from other questions, I've yet to solve my issue. The following is test code provided for testing the SAP class from the princeton assignment wordnet(found here http://www.cs.princeton.edu/courses/archive/fall14/cos226/assignments/wordnet.html )
public static void main(String[] args) {
In in = new In(args[0]);
Digraph G = new Digraph(in);
SAP sap = new SAP(G);
while (!StdIn.isEmpty()) {
int v = StdIn.readInt();
int w = StdIn.readInt();
int length = sap.length(v, w);
int ancestor = sap.ancestor(v, w);
StdOut.printf("length = %d, ancestor = %d\n", length, ancestor);
}
}
For some reason, using this test code gives me an infinite loop when I try to pass text files (an example txt file can be found here: ftp://ftp.cs.princeton.edu/pub/cs226/wordnet/digraph1.txt).
Since this code was given to me by the assignment page, I assume that its right. This leads me to question, could something else in my code be causing an infinite loop? I'm unsure of what to check besides the while loop of this test code.
In addition, am I some how passing the text file wrong? The steps that I took to pass digraph1.txt were
Run -> Run configurations
for the SAP class
and then for arguments I put in ${file_prompt} and chose the digraph1.txt each time.
When I manually enter in the digraph1.txt all of my methods work perfectly fine.
Thanks in advance!