Print output on the same line - java

I'm making a translator in Java to translate a fake language that I came up with for fun. I input an English word and it returns it's equivalent word in the other language. It's successfully translating everything, but each new word is on a separate line and I just want the output on one line. I'm still new to Java but here is my code:
import java.io.*;
import java.util.*;
public class Translator {
private static Scanner scan;
public static void main(String[] args) {
HashMap <String, String> XanthiumLang = new HashMap <String, String>();
XanthiumLang.put("hello", "fohran");
XanthiumLang.put("the", "krif");
XanthiumLang.put("of", "ney");
XanthiumLang.put("to", "dov");
XanthiumLang.put("and", "ahrk");
Scanner scan = new Scanner(System.in);
String sentence = scan.nextLine();
String[] result = sentence.split(" ");
for(int i = 0; i < result.length; i++){
if(XanthiumLang.containsKey(result[i])){
result[i] = XanthiumLang.get(result[i]);
}
System.out.println(result[i]);
}
}
}
I only have a few words in the code as of right now and they are stored in a hashmap. Anyways like I said the output of each word is on a separate line, not on just one line. Any ideas or changes to my code would be helpful!

Use System.out.print();. Doing so will print the entire array on one line. System.out.println(); will print the result on a new line each time (hence the ln at the end).
import java.io.*;
import java.util.*;
public class Translator {
private static Scanner scan;
public static void main(String[] args) {
HashMap <String, String> XanthiumLang = new HashMap <String, String>();
XanthiumLang.put("hello", "fohran");
XanthiumLang.put("the", "krif");
XanthiumLang.put("of", "ney");
XanthiumLang.put("to", "dov");
XanthiumLang.put("and", "ahrk");
Scanner scan = new Scanner(System.in);
String sentence = scan.nextLine();
String[] result = sentence.split(" ");
for(int i = 0; i < result.length; i++){
if(XanthiumLang.containsKey(result[i])){
result[i] = XanthiumLang.get(result[i]);
}
System.out.print(result[i]);
}
}
}
More on the different formats here.

Related

search for multiple strings from a text file in java

I'm trying to search of multiple words given from a user ( i used array to store them in ) from one txt file , and then if that word presented once in the file it will be displayed and if it's not it won't.
also for the words itself , if it's duplicated it will search it once.
the problem now when i search for only one it worked , but with multiple words it keeps repeated that the word isn't present even if it's there.
i would like to know where should i put the for loop and what's the possible changes.
package search;
import java.io.*;
import java.util.Scanner;
public class Read {
public static void main(String[] args) throws IOException
{
Scanner sc = new Scanner(System.in);
String[] words=null;
FileReader fr = new FileReader("java.txt");
BufferedReader br = new BufferedReader(fr);
String s;
System.out.println("Enter the number of words:");
Integer n = sc.nextInt();
String wordsArray[] = new String[n];
System.out.println("Enter words:");
for(int i=0; i<n; i++)
{
wordsArray[i]=sc.next();
}
for (int i = 0; i <n; i++) {
int count=0; //Intialize the word to zero
while((s=br.readLine())!=null) //Reading Content from the file
{
{
words=s.split(" "); //Split the word using space
for (String word : words)
{
if (word.equals(wordsArray[i])) //Search for the given word
{
count++; //If Present increase the count by one
}
}
if(count == 1)
{
System.out.println(wordsArray[i] + " is unique in file ");
}
else if (count == 0)
{
System.out.println("The given word is not present in the file");
}
else
{
System.out.println("The given word is present in the file more than 1 time");
}
}
}
}
fr.close();
}
}
The code which you wrote is error prone and remember always there should be proper break condition when you use while loop.
Try the following code:
public class Read {
public static void main(String[] args)
{
// Declaring the String
String paragraph = "These words can be searched";
// Declaring a HashMap of <String, Integer>
Map<String, Integer> hashMap = new HashMap<>();
// Splitting the words of string
// and storing them in the array.
String[] words = new String[]{"These", "can", "searched"};
for (String word : words) {
// Asking whether the HashMap contains the
// key or not. Will return null if not.
Integer integer = hashMap.get(word);
if (integer == null)
// Storing the word as key and its
// occurrence as value in the HashMap.
hashMap.put(word, 1);
else {
// Incrementing the value if the word
// is already present in the HashMap.
hashMap.put(word, integer + 1);
}
}
System.out.println(hashMap);
}
}
I've tried by hard coding the values, you can take words and paragraph from the file and console.
The 'proper' class to use for extracting words from text is java.text.BreakIterator
You can try the following (reading line-wise in case of large files)
import java.text.BreakIterator;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;
import java.nio.file.Files;
import java.nio.file.Paths;
public class WordFinder {
public static void main(String[] args) {
try {
if (args.length < 2) {
WordFinder.usage();
System.exit(1);
}
ArrayList<String> argv = new ArrayList<>(Arrays.asList(args));
String path = argv.remove(0);
List<String> found = WordFinder.findWords(Files.lines(Paths.get(path)), argv);
System.out.printf("Found the following word(s) in file at %s%n", path);
System.out.println(found);
} catch (Throwable t) {
t.printStackTrace();
}
}
public static List<String> findWords(Stream<String> lines, ArrayList<String> searchWords) {
List<String> result = new ArrayList<>();
BreakIterator boundary = BreakIterator.getWordInstance();
lines.forEach(line -> {
boundary.setText(line);
int start = boundary.first();
for (int end = boundary.next(); end != BreakIterator.DONE; start = end, end = boundary.next()) {
String candidate = line.substring(start, end);
if (searchWords.contains(candidate)) {
result.add(candidate);
searchWords.remove(candidate);
}
}
});
return result;
}
private static void usage() {
System.err.println("Usage: java WordFinder <Path to input file> <Word 1> [<Word 2> <Word 3>...]");
}
}
Sample run:
goose#t410:/tmp$ echo 'the quick brown fox jumps over the lazy dog' >quick.txt
goose#t410:/tmp$ java WordFinder quick.txt dog goose the did quick over
Found the following word(s) in file at quick.txt
[the, quick, over, dog]
goose#t410:/tmp$

Read the contents of the “dictionary.txt” and add all four letter words to an ADT ArrayList<String> words = new ArrayList<>();

import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
public class BuildGraph {
public static void main(String[] args) throws IOException {
Scanner sc=new Scanner((new File("dictionary.txt")));
ArrayList<String> words=new ArrayList<String>();
while(sc.hasNextLine()){
if(sc.next().length()==4){
words.add(sc.next());
//sc.next();
}
System.out.println(words);
}
// sc.close();
for (int i = 0; i < words.size(); i++) {
System.out.println(words.get(i));
}
}
}
This is my code, and I am trying to read only 4 letter words from a dictionary file, but when i run my code it gives me all the words from dictionary files
Thank-you in advance.
You code needs some tweaks. Basically you should not be calling sc.next() twice to get the same element as calling next() moves the pointer to the next element
Rewrite your while loop to this:
while(sc.hasNextLine() && sc.hasNext()){
String word = sc.next();
if(word.length()==4){
words.add(word);
}
System.out.println(words);
}

How to scan a List<String>

I'm trying to make a method in java that will use a scanner to read a List String. I want the program to divide the array word by word using the delineator "//s". I already got each array by the names of the people in the text file, I am just trying to divide the array further so that way i can sort them by there information (Ex. if they have f for female I would be able to call that specific part of the array using arrayList.get(index) and sort it by gender that way) Here is my code:
Sorry for being unclear, here is my full code:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;
public class Filereader {
public static void replaceSlash(List<String> array) {
for (int i = 0; i < array.size(); i++)
{
if (array.get(i).contains("-"))
{
array.set(i, array.get(i).replace("-", "/"));
}
}
}
public static void split( List<String> array ) {
Scanner scanner = new Scanner().useDelimiter("\\s");
for (int i= 0; i < array.size(); i++) {
while (scanner.hasNext()) {
array.set(i, scanner.next());
}
}
}
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
System.out.println("Please type the name of the file you wish to sort: ");
//get the name of the file
Scanner scanner = new Scanner(System.in);
File fileName = new File(scanner.nextLine());
scanner = new Scanner(fileName).useDelimiter("\\s");
List<String> annaK = new ArrayList<String>();
List<String> martinaH = new ArrayList<String>();
List<String> monicaS = new ArrayList<String>();
while (scanner.hasNextLine()) {
annaK.add(scanner.nextLine());
martinaH.add(scanner.nextLine());
monicaS.add(scanner.nextLine());
}
replaceSlash(annaK);
replaceSlash(martinaH);
replaceSlash(monicaS);
split(annaK);
System.out.println(annaK);
System.out.println(martinaH);
System.out.println(monicaS);
}
}
You can make one scanner for every string within the list, then loop over that
for (String a : array) {
Scanner scanner = new Scanner(a).useDelimiter("\\s");
while (scanner.hasNext()) {
System.out.println(scanner.next());
}
}
However, a.split() or StringTokenizer make more sense than a Scanner here
Besides that, array.set won't work because you're assigning the same i value for multiple words within each individual a value... Which will result in only the last value in the scanned string to be assigned to that index of the list
If you're trying to splits all words within a list into a new list, then you'll actually need to create and append values to a separate list object (don't modify your parameters, and don't have methods with side effects)

How to load text file into string array with only import java.io.* and no other library

Basically i added "import java.util.Scanner". but I wanted my code to work without that library and only "import java.io*" . However i want all my words (english word in the dictionary with the total of 109562 words in this case) in my text file to be inside the string array. Hence, in this case, without the scanner. how to do that?
import java.io.*;
import java.util.Scanner;
public class tester{
public static void main (String [] args) throws IOException{
File f = new File("C:/Users/alienware14/Documents/words.txt");
String [] words = new String [109562];
readWords(f , words);
/*
System.out.println("----ALL WORDS IN WORDS.TXT----");
for(int i=0; i<words.length; i++){
System.out.println("");
System.out.print(words[i]);
} */
}
public static String [] readWords(File f , String [] words) throws FileNotFoundException {
Scanner s;
s = new Scanner(f);
for(int i = 0; i < words.length; i++){
while (words[i] == null) {
words[i] = s.next();
}
}
s.close();
return words;
}
}
You could use a java.io.FileReader instead of a Scanner. Just google 'Java read file' to find an example for it (Using a Scanner for reading files is quite exotic). Though I don't really understand why you have a problem with importing a Scanner. Sounds like homework..
Try split method
String[] words = yourtext.split(" "); //user space in split method for words

Need help reading words between <> and saving to array

Hello all I am trying to take a predetermined .txt file as shown below and read only the words between the < >, then add them to an array and write them to an HTML file.
the txt file is as follows.
for some reason its not showing the words in-between the < > on here.
"" = word between < >
A, <""">
B, <""">
C, <""">
etc.
here is my current code for attempting to read from the file, I understand the .split and .delimeter problem is these methods read everything before the chosen character.
import java.io.*;
import java.util.Scanner;
public class TickTacTwice {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(new File("/Users/Muhammed/Documents/tic_tac_twice.txt"));
while(sc.hasNextLine()) {
String S = sc.nextLine();
String [] words = S.split("<");
System.out.println(words[0]);
}
sc.close();
}
}
Any help would be much appreciated.
Thanks
It looks as though you are just printing the first item in the split array "words", is it possible the first occurrence of a <> is empty? To view all items in the array you can use a loop like the following:
var i;
for (i = 0; i < words.length; i++) {
System.out.println(words[i]);
}
Each of those lines will contain all characters until the next "<" so you will then need to trim to the ">"
An easier way to do this is using Regular Expressions - there are some good examples here on StackOverflow.
Edit: Combining your code with TAsks would produce what you are after:
import java.io.*;
import java.util.Scanner;
public class TickTacTwice {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(new File("/Users/Muhammed/Documents/tic_tac_twice.txt"));
while(sc.hasNextLine()) {
String S = sc.nextLine();
Pattern pattern = Pattern.compile("<(.+?)>");
Matcher m = pattern.matcher(S);
while(m.find()) {
System.out.println(m.group(0));
}
}
sc.close();
}
}

Categories