'only null as output stuck
I need null for the if statement in output in the main method but it comes up as only null and not showing the actual string I am lost on what is wrong uri(s) has the string input and when calling methods it is still coming up as null so nothing happens
public class URI {
private String uri, scheme, authority, path, query, fragment, userInfo, host, hp;
private int port;
public URI(String uri) {
uri = "abc://username:[email protected]:123/path/data?key=vale&key2=value2#fradid1";
}
/*
* Method: getScheme
*
* @Param String Purpose : pull out the scheme from uri string
*
* @Return: String
*/
public String getScheme(String uri) {
scheme = uri.substring(0, uri.indexOf(":"));
return scheme;
}
/*
* Method: getAuthority
*
* @param String purpose: to pull out authority from the uri string
*
* @Return: String
*/
public String getAuthority(String uri) {
authority = uri.substring(uri.indexOf("//"), uri.indexOf("/"));
return authority;
}
/*
* Method: getPath
*
* @param String purpose : to pull out path from uri string return string
*/
public String getPath(String uri) {
hp = uri.substring(uri.indexOf("@"),uri.indexOf("?"));
path = hp.substring(hp.indexOf("/"), hp.indexOf("?"));
return path;
}
/*
* Method:getPath
*
* @param String Purpose: tp pull out path from the uri string
*
* @Return: String
*/
public String getQuery(String uri) {
query = uri.substring(uri.indexOf("?"), uri.indexOf("#"));
return query;
}
/*
* Method:getFragment
*
* @param String Purpose: to pull out Fragment from uri String
*
* @Return: String
*/
public String getFragment(String uri) {
fragment = uri.substring(uri.indexOf("#"));
return fragment;
}
/*
* Method:getUserInfo
*
* @Param String Purpose: to pull out user info from uri String
*
* @Return String
*/
public String getUserInfo(String uri) {
userInfo = uri.substring(uri.indexOf("//"), uri.indexOf("@"));
return userInfo;
}
/*
* Method:getHost
*
* @param String Purpose: to pull out host from uri string
*
* @return String
*/
public String getHost(String uri) {
hp = uri.substring(uri.indexOf("@"),uri.indexOf("?"));
host = hp.substring(hp.indexOf("@"), hp.lastIndexOf(":"));
return host;
}
/*
* Method getPort
*
* @param String Purpose: to pull out port from uri
*
* @return String
*/
public int getPort(String uri) {
hp = uri.substring(uri.indexOf("@"),uri.indexOf("?"));
port = Integer.parseInt(hp.substring(hp.lastIndexOf(":"), hp.indexOf(":")));
return port;
}
/*
* Method: toString
*
* @param String Purpose :return uri as a string
*/
public String toString() {
return "The Scheme is: " + getScheme(uri) + "\nThe authority is:" + getAuthority(uri) + "\nThe host is:"
+ getHost(uri) + "\nThe path is:" + getPath(uri) + "\nThe query is: " + getQuery(uri)
+ "\nThe fragment is:" + getFragment(uri) + "\nThe port is:" + getPort(uri)
+ "\nThe user information is:" + getUserInfo(uri);
}
}
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;
public class Project2App {
public static void main(String[] args) throws IOException {
// Create a variable to store each line read from the file
String s = "";
// Connect the scanner to a file in the specified path
Scanner in = new Scanner(new File("uri"));
//As long as there are more lines to read
while (in.hasNextLine()) {
// read a line and store it in a String variable
s = in.nextLine();
// create a URI from the string
URI uri = new URI(s);
/*
// call the output method and pass the URI to it
*/
output(uri);
// Close the Scanner
}
in.close();
}
/*
* This method outputs the components of an URI
*/
System.out.println(uri);
if (uri.getScheme(uri.toString()) != null)
System.out.println("\tThe scheme is: " + uri.getScheme(uri.toString()));
if (uri.getAuthority(uri.toString())!= null)
System.out.println("\tThe authority is: " + uri.getAuthority(uri.toString()));
if (uri.getHost(uri.toString()) != null)
System.out.println("\tThe host is: " + uri.getHost(uri.toString()));
if (uri.getPort(uri.toString()) > 0)
System.out.println("\tThe port is: " + uri.getPort(uri.toString()));
if (uri.getPath(uri.toString()) != null)
System.out.println("\tThe path is: " + uri.getPath(uri.toString()));
if (uri.getUserInfo(uri.toString()) != null)
System.out.println("\tThe user information is: " + uri.getUserInfo(uri.toString()));
if (uri.getQuery(uri.toString()) != null)
System.out.println("\tThe query is: " + uri.getQuery(uri.toString()));
if (uri.getFragment(uri.toString()) != null)
System.out.println("\tThe fragment is: " + uri.getFragment(uri.toString()));
System.out.println();
}
}
TEXT FILE FOR INPUT
uri.txt
is file of different uri's with multiple variations to test each method
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
